Update handler.py
Browse files- handler.py +22 -64
handler.py
CHANGED
|
@@ -93,85 +93,42 @@ class EndpointHandler():
|
|
| 93 |
loader = WebBaseLoader(urls)
|
| 94 |
data = loader.load()
|
| 95 |
|
| 96 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap =
|
| 97 |
all_splits = text_splitter.split_documents(data)
|
| 98 |
|
| 99 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=embedding_function)
|
| 100 |
-
retriever = vectorstore.as_retriever()
|
| 101 |
|
| 102 |
-
compressor = LLMChainExtractor.from_llm(chat)
|
| 103 |
-
compression_retriever = ContextualCompressionRetriever(
|
| 104 |
-
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
Standalone question: [/INST]"""
|
| 112 |
-
|
| 113 |
-
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
|
| 114 |
|
| 115 |
-
template = """[INST] Answer the question based only on the following context:
|
| 116 |
{context}
|
| 117 |
|
| 118 |
-
Question: {question}
|
| 119 |
-
"""
|
| 120 |
-
|
| 121 |
-
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
|
| 122 |
-
|
| 123 |
-
self.memory = ConversationBufferMemory(
|
| 124 |
-
return_messages=True, output_key="answer", input_key="question"
|
| 125 |
-
)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
standalone_question = {
|
| 134 |
-
"standalone_question": {
|
| 135 |
-
"question": lambda x: x["question"],
|
| 136 |
-
"chat_history": lambda x: get_buffer_string(x["chat_history"]),
|
| 137 |
-
}
|
| 138 |
-
| CONDENSE_QUESTION_PROMPT
|
| 139 |
| chat
|
| 140 |
-
| StrOutputParser()
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
| 144 |
-
|
| 145 |
-
def _combine_documents(
|
| 146 |
-
docs, document_prompt=DEFAULT_DOCUMENT_PROMPT, document_separator="\n\n"
|
| 147 |
-
):
|
| 148 |
-
doc_strings = [format_document(doc, document_prompt) for doc in docs]
|
| 149 |
-
return document_separator.join(doc_strings)
|
| 150 |
-
|
| 151 |
-
# Now we retrieve the documents
|
| 152 |
-
retrieved_documents = {
|
| 153 |
-
"docs": itemgetter("standalone_question") | retriever,
|
| 154 |
-
"question": lambda x: x["standalone_question"],
|
| 155 |
-
}
|
| 156 |
-
# Now we construct the inputs for the final prompt
|
| 157 |
-
final_inputs = {
|
| 158 |
-
"context": lambda x: _combine_documents(x["docs"]),
|
| 159 |
-
"question": itemgetter("question"),
|
| 160 |
-
}
|
| 161 |
-
# And finally, we do the part that returns the answers
|
| 162 |
-
answer = {
|
| 163 |
-
"answer": final_inputs | ANSWER_PROMPT | chat,
|
| 164 |
-
"docs": itemgetter("docs"),
|
| 165 |
-
}
|
| 166 |
-
# And now we put it all together!
|
| 167 |
-
self.final_chain = loaded_memory | standalone_question | retrieved_documents | answer
|
| 168 |
|
| 169 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 170 |
# get inputs
|
| 171 |
inputs = data.pop("inputs",data)
|
| 172 |
date = data.pop("date", None)
|
| 173 |
|
| 174 |
-
result = self.
|
| 175 |
|
| 176 |
answer = result['answer']
|
| 177 |
|
|
@@ -179,7 +136,8 @@ class EndpointHandler():
|
|
| 179 |
# This will be improved in the future
|
| 180 |
# For now you need to save it yourself
|
| 181 |
# self.memory.save_context(inputs, {"answer": answer})
|
| 182 |
-
|
|
|
|
| 183 |
|
| 184 |
return answer
|
| 185 |
|
|
|
|
| 93 |
loader = WebBaseLoader(urls)
|
| 94 |
data = loader.load()
|
| 95 |
|
| 96 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
|
| 97 |
all_splits = text_splitter.split_documents(data)
|
| 98 |
|
| 99 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=embedding_function)
|
| 100 |
+
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
| 101 |
|
| 102 |
+
# compressor = LLMChainExtractor.from_llm(chat)
|
| 103 |
+
# compression_retriever = ContextualCompressionRetriever(
|
| 104 |
+
# base_compressor=compressor, base_retriever=retriever
|
| 105 |
+
# )
|
| 106 |
|
| 107 |
+
template = """Use the following pieces of context to answer the question at the end.
|
| 108 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 109 |
+
Use three sentences maximum and keep the answer as concise as possible.
|
| 110 |
+
Always say "thanks for asking!" at the end of the answer.
|
|
|
|
|
|
|
|
|
|
| 111 |
|
|
|
|
| 112 |
{context}
|
| 113 |
|
| 114 |
+
Question: {question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
Helpful Answer:"""
|
| 117 |
+
custom_rag_prompt = PromptTemplate.from_template(template)
|
| 118 |
+
|
| 119 |
+
self.rag_chain = (
|
| 120 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
| 121 |
+
| custom_rag_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
| chat
|
| 123 |
+
| StrOutputParser()
|
| 124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 127 |
# get inputs
|
| 128 |
inputs = data.pop("inputs",data)
|
| 129 |
date = data.pop("date", None)
|
| 130 |
|
| 131 |
+
result = self.rag_chain.invoke(inputs)
|
| 132 |
|
| 133 |
answer = result['answer']
|
| 134 |
|
|
|
|
| 136 |
# This will be improved in the future
|
| 137 |
# For now you need to save it yourself
|
| 138 |
# self.memory.save_context(inputs, {"answer": answer})
|
| 139 |
+
|
| 140 |
+
#self.memory.load_memory_variables({})
|
| 141 |
|
| 142 |
return answer
|
| 143 |
|