Update app.py
Browse files
app.py
CHANGED
|
@@ -120,20 +120,20 @@ model_kwargs = {"max_length": max_length, "pad_token_id": 50256, "pad_token": "[
|
|
| 120 |
# "padding": 'longest' #for open end generation
|
| 121 |
# })
|
| 122 |
llm = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], model_name=model_name, temperature=0.9)
|
| 123 |
-
prompt_template = """
|
| 124 |
-
receive a context with information about related works, papers and tools for the PhD of Martín Dévora Pajares
|
| 125 |
-
also appearing as M. Dévora-Pajares.
|
| 126 |
-
You can only answer things based on the context and
|
| 127 |
with the user.
|
| 128 |
Context: {context}
|
| 129 |
Question: {question}
|
| 130 |
-
Memory: {
|
| 131 |
|
| 132 |
PROMPT = PromptTemplate(
|
| 133 |
-
template=prompt_template, input_variables=["context", "question", "
|
| 134 |
)
|
| 135 |
chain_type_kwargs = {"prompt": PROMPT}
|
| 136 |
-
memory = ConversationBufferWindowMemory(k=
|
| 137 |
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever(),
|
| 138 |
chain_type_kwargs=chain_type_kwargs, return_source_documents=True,
|
| 139 |
memory=memory)
|
|
@@ -146,14 +146,15 @@ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_s
|
|
| 146 |
# print(f"Sources: {response['source_documents']}")
|
| 147 |
|
| 148 |
def chat(message, history):
|
| 149 |
-
|
|
|
|
| 150 |
# print(f"Answer: {response['result']}")
|
| 151 |
# print(f"Sources: {response['source_documents']}")
|
| 152 |
|
| 153 |
-
gr.ChatInterface(chat, chatbot=gr.Chatbot(
|
| 154 |
-
textbox=gr.Textbox(placeholder="Ask me about
|
| 155 |
-
title="
|
| 156 |
-
description="Ask",
|
| 157 |
theme="soft",
|
| 158 |
examples=["What is SHERLOCK?", "What is MATRIX?", "What are the foundational papers of SHERLOCK?"],
|
| 159 |
cache_examples=True,
|
|
|
|
| 120 |
# "padding": 'longest' #for open end generation
|
| 121 |
# })
|
| 122 |
llm = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], model_name=model_name, temperature=0.9)
|
| 123 |
+
prompt_template = """You are going to
|
| 124 |
+
receive a context with information about related works, papers and tools for the PhD of Martín Dévora Pajares
|
| 125 |
+
(exoplanets field), also appearing as M. Dévora-Pajares.
|
| 126 |
+
You can only answer things based on the question, context and a memory section to remember previous interactions
|
| 127 |
with the user.
|
| 128 |
Context: {context}
|
| 129 |
Question: {question}
|
| 130 |
+
Memory: {chat_history}"""
|
| 131 |
|
| 132 |
PROMPT = PromptTemplate(
|
| 133 |
+
template=prompt_template, input_variables=["context", "question", "chat_history"]
|
| 134 |
)
|
| 135 |
chain_type_kwargs = {"prompt": PROMPT}
|
| 136 |
+
memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history", question_key="question")
|
| 137 |
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever(),
|
| 138 |
chain_type_kwargs=chain_type_kwargs, return_source_documents=True,
|
| 139 |
memory=memory)
|
|
|
|
| 146 |
# print(f"Sources: {response['source_documents']}")
|
| 147 |
|
| 148 |
def chat(message, history):
|
| 149 |
+
result = qa({"query": message})
|
| 150 |
+
yield result['result'] + '\n\n' + result['source_documents']
|
| 151 |
# print(f"Answer: {response['result']}")
|
| 152 |
# print(f"Sources: {response['source_documents']}")
|
| 153 |
|
| 154 |
+
gr.ChatInterface(chat, chatbot=gr.Chatbot(),
|
| 155 |
+
textbox=gr.Textbox(placeholder="Ask me about SHERLOCK, MATRIX or WATSON!", container=False, scale=7),
|
| 156 |
+
title="PlanetHunters informative chatbot",
|
| 157 |
+
description="Ask anything related to SHERLOCK, MATRIX, WATSON and their related papers!",
|
| 158 |
theme="soft",
|
| 159 |
examples=["What is SHERLOCK?", "What is MATRIX?", "What are the foundational papers of SHERLOCK?"],
|
| 160 |
cache_examples=True,
|