mdevora commited on
Commit
d82c6f1
·
1 Parent(s): e00e24d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -119,24 +119,38 @@ model_kwargs = {"max_length": max_length, "pad_token_id": 50256, "pad_token": "[
119
  # "pad_token_id": 50256, "pad_token": "[PAD]",
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # while True:
141
  # query = input("Question: ")
142
  # #docs = vector_store.similarity_search(query)
@@ -144,10 +158,18 @@ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_s
144
  # response = qa({"query": query})
145
  # print(f"Answer: {response['result']}")
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
 
@@ -156,8 +178,10 @@ gr.ChatInterface(chat, chatbot=gr.Chatbot(),
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,
 
161
  retry_btn=None,
162
  undo_btn="Delete Previous",
163
  clear_btn="Clear").queue().launch()
 
 
119
  # "pad_token_id": 50256, "pad_token": "[PAD]",
120
  # "padding": 'longest' #for open end generation
121
  # })
122
+ llm = ChatOpenAI(streaming=True, 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 and a context to answer it.
 
127
  Context: {context}
128
+ Question: {question}"""
129
+ #Memory: {chat_history}"""
130
 
131
  PROMPT = PromptTemplate(
132
+ template=prompt_template, input_variables=["context", "question"]
133
  )
134
  chain_type_kwargs = {"prompt": PROMPT}
135
+ memory = ConversationBufferWindowMemory(k=4, memory_key="chat_history", input_key="question", output_key='answer',
136
+ return_messages=True)
137
+
138
+ retriever = vector_store.as_retriever()
139
+ # memory = VectorStoreRetrieverMemory(
140
+ # memory_key="chat_history",
141
+ # return_docs=False,
142
+ # retriever=retriever,
143
+ # return_messages=True,
144
+ # )
145
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever,
146
+ chain_type_kwargs=chain_type_kwargs, return_source_documents=True)
147
+ #memory=memory)
148
+ # qa = ConversationalRetrievalChain.from_llm(
149
+ # llm=llm, retriever=retriever, memory=memory,
150
+ # return_source_documents=True,
151
+ # combine_docs_chain_kwargs=chain_type_kwargs)
152
+
153
+
154
  # while True:
155
  # query = input("Question: ")
156
  # #docs = vector_store.similarity_search(query)
 
158
  # response = qa({"query": query})
159
  # print(f"Answer: {response['result']}")
160
  # print(f"Sources: {response['source_documents']}")
161
+
162
  def chat(message, history):
163
  result = qa({"query": message})
164
+ sources_list = []
165
+ for item in result['source_documents']:
166
+ if 'http' in item.metadata['source']:
167
+ sources_list = sources_list + [item.metadata['source']]
168
+ sources_list = list(set(sources_list))
169
+ response = result['result']
170
+ if len(sources_list) > 0:
171
+ response = response + '\n\nSources:' + '\n\n'.join(sources_list)
172
+ yield response
173
  # print(f"Answer: {response['result']}")
174
  # print(f"Sources: {response['source_documents']}")
175
 
 
178
  title="PlanetHunters informative chatbot",
179
  description="Ask anything related to SHERLOCK, MATRIX, WATSON and their related papers!",
180
  theme="soft",
181
+ examples=["What is SHERLOCK?", "What is MATRIX?", "What are the foundational papers of SHERLOCK?",
182
+ "How should I cite SHERLOCK?", "How should I cite MATRIX?"],
183
+ cache_examples=False,
184
  retry_btn=None,
185
  undo_btn="Delete Previous",
186
  clear_btn="Clear").queue().launch()
187
+