Skip to content

Commit

Permalink
cleanup and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Nov 22, 2023
1 parent e7425e5 commit 9e4289a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ We target only the full-text using [Grobid](https://github.com/kermitt2/grobid)

Additionally, this frontend provides the visualisation of named entities on LLM responses to extract <span stype="color:yellow">physical quantities, measurements</span> (with [grobid-quantities](https://github.com/kermitt2/grobid-quantities)) and <span stype="color:blue">materials</span> mentions (with [grobid-superconductors](https://github.com/lfoppiano/grobid-superconductors)).

The conversation is backed up by a sliding window memory (top 4 more recent messages) that help refers to information previously discussed in the chat.
The conversation is kept in memory up by a buffered sliding window memory (top 4 more recent messages) and the messages are injected in the context as "previous messages".

**Demos**:
- (on HuggingFace spaces): https://lfoppiano-document-qa.hf.space/
Expand Down
10 changes: 4 additions & 6 deletions document_qa/document_qa_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def __init__(self,
):
self.embedding_function = embedding_function
self.llm = llm
# if memory:
# prompt = self.default_prompts[qa_chain_type].PROMPT_SELECTOR.get_prompt(llm)
# self.chain = load_qa_chain(llm, chain_type=qa_chain_type, prompt=prompt, memory=memory)
# else:
self.memory = memory
self.chain = load_qa_chain(llm, chain_type=qa_chain_type)

Expand Down Expand Up @@ -161,7 +157,7 @@ def _parse_json(self, response, output_parser):
def _run_query(self, doc_id, query, context_size=4):
relevant_documents = self._get_context(doc_id, query, context_size)
response = self.chain.run(input_documents=relevant_documents,
question=query)
question=query)

if self.memory:
self.memory.save_context({"input": query}, {"output": response})
Expand All @@ -172,7 +168,9 @@ def _get_context(self, doc_id, query, context_size=4):
retriever = db.as_retriever(search_kwargs={"k": context_size})
relevant_documents = retriever.get_relevant_documents(query)
if self.memory and len(self.memory.buffer_as_messages) > 0:
relevant_documents.append(Document(page_content="Previous conversation:\n{}\n\n".format(self.memory.buffer_as_str)))
relevant_documents.append(
Document(page_content="Previous conversation:\n{}\n\n".format(self.memory.buffer_as_str))
)
return relevant_documents

def get_all_context_by_document(self, doc_id):
Expand Down
1 change: 0 additions & 1 deletion streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import dotenv
from grobid_quantities.quantities import QuantitiesAPI
from langchain.callbacks import PromptLayerCallbackHandler
from langchain.llms.huggingface_hub import HuggingFaceHub
from langchain.memory import ConversationBufferWindowMemory

Expand Down

0 comments on commit 9e4289a

Please sign in to comment.