gufett0 commited on
Commit
7857d7b
·
1 Parent(s): 8fb9412

simplified chat engine

Browse files
Files changed (1) hide show
  1. backend.py +10 -85
backend.py CHANGED
@@ -154,98 +154,23 @@ def handle_query(query_str: str,
154
  )"""
155
 
156
 
157
- DEFAULT_TEXT_QA_PROMPT_TMPL = (
158
- "Context information is below. \n"
159
- "---------------------\n"
160
- "{context_str}"
161
- "\n---------------------\n"
162
- "Given the context information answer the following question "
163
- "(if you don't know the answer, admit it, and use the best of your knowledge): {query_str}\n"
164
- )
165
- TEXT_QA_TEMPLATE = PromptTemplate(DEFAULT_TEXT_QA_PROMPT_TMPL)
166
-
167
-
168
- DEFAULT_REFINE_PROMPT_TMPL = (
169
- "La domanda originale è la seguente: {query_str}\n"
170
- "Abbiamo fornito una risposta esistente: {existing_answer}\n"
171
- "Abbiamo l'opportunità di affinare la risposta esistente "
172
- "(solo se necessario) con un po' più di contesto qui sotto.\n"
173
- "------------\n"
174
- "{context_msg}\n"
175
- "------------\n"
176
- "Dato il nuovo contesto e utilizzando al meglio le tue conoscenze, migliora la risposta esistente. "
177
- "Se non puoi migliorare la risposta esistente, ripetila semplicemente."
178
- )
179
- DEFAULT_REFINE_PROMPT = PromptTemplate(DEFAULT_REFINE_PROMPT_TMPL)
180
-
181
- CHAT_REFINE_PROMPT_TMPL_MSGS = [
182
- ChatMessage(content="{query_str}", role=MessageRole.USER),
183
- ChatMessage(content="{existing_answer}", role=MessageRole.ASSISTANT),
184
- ChatMessage(
185
- content="We have the opportunity to refine the above answer "
186
- "(only if needed) with some more context below.\n"
187
- "------------\n"
188
- "{context_msg}\n"
189
- "------------\n"
190
- "Given the new context and using the best of your knowledge, improve the existing answer. "
191
- "If you can't improve the existing answer, just repeat it again.",
192
- role=MessageRole.USER,
193
- ),
194
- ]
195
-
196
- CHAT_REFINE_PROMPT = ChatPromptTemplate(CHAT_REFINE_PROMPT_TMPL_MSGS)
197
-
198
-
199
-
200
- query_engine = index.as_query_engine(
201
- streaming=True,
202
- #memory=memory,
203
- similarity_top_k=4,
204
- response_mode= "tree_summarize",
205
- #text_qa_template=TEXT_QA_TEMPLATE,
206
- #refine_template=CHAT_REFINE_PROMPT
207
- )
208
-
209
 
210
- new_summary_tmpl_str = """Text:
211
- "Sei un assistente Q&A italiano di nome Odi, che risponde solo alle domande o richieste pertinenti in modo preciso."
212
- " Quando un utente ti chiede informazioni su di te o sul tuo creatore puoi dire che sei un assistente ricercatore creato dagli Osservatori Digitali e fornire gli argomenti di cui sei esperto."
213
- " Ecco i documenti rilevanti per il contesto:\n"
214
- "{context_str}\n"
215
- "\n Usa la cronologia della chat, o il contesto sopra, per interagire e aiutare l'utente a rispondere alla sua domanda." """
216
-
217
- query_engine.update_prompts(
218
- {"response_synthesizer:summary_template": PromptTemplate(new_summary_tmpl_str)}
219
- )
220
-
221
  #prompts_dict = chat_engine.get_prompts()
222
  #display_prompt_dict(prompts_dict)
223
-
224
- """if query_str == "testing":
225
-
226
- new_summary_tmpl_str = (
227
- "Context information is below.\n"
228
- "---------------------\n"
229
- "{context_str}\n"
230
- "---------------------\n"
231
- "Given the context information and not prior knowledge, "
232
- "answer the query in the style of a Shakespeare play.\n"
233
- #"Query: {query_str}\n"
234
- #"Answer: "
235
- )
236
- new_summary_tmpl = PromptTemplate(new_summary_tmpl_str)
237
-
238
- chat_engine.update_prompts(
239
- {"response_synthesizer:summary_template": new_summary_tmpl}
240
- )
241
- prompts_dict = chat_engine.get_prompts()
242
- display_prompt_dict("prompts_dict", prompts_dict)"""
243
 
244
 
245
  #chat_engine.reset()
246
  outputs = []
247
- response = query_engine.query(query_str)
248
- #response = chat_engine.stream_chat(query_str, chat_history=conversation)
249
 
250
  sources = [] # Use a list to collect multiple sources if present
251
  #response = chat_engine.chat(query_str)
 
154
  )"""
155
 
156
 
157
+ chat_engine = index.as_chat_engine(
158
+ chat_mode="context",
159
+ memory=memory,
160
+ system_prompt=(
161
+ "Sei un assistente Q&A italiano di nome Odi, che risponde solo alle domande o richieste pertinenti in modo preciso."
162
+ " Usa la cronologia della chat, o il contesto fornito, per interagire e aiutare l'utente a rispondere alla sua domanda."
163
+ ),
164
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
 
 
 
 
 
 
 
 
 
 
 
166
  #prompts_dict = chat_engine.get_prompts()
167
  #display_prompt_dict(prompts_dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
 
170
  #chat_engine.reset()
171
  outputs = []
172
+ #response = query_engine.query(query_str)
173
+ response = chat_engine.stream_chat(query_str, chat_history=conversation)
174
 
175
  sources = [] # Use a list to collect multiple sources if present
176
  #response = chat_engine.chat(query_str)