Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -151,7 +151,7 @@ Recent History:
|
|
151 |
Available Actions and their descriptions:
|
152 |
{tool_descriptions_for_prompt}
|
153 |
|
154 |
-
Based on the query and the action descriptions, select the single best action. Output the corresponding JSON.
|
155 |
Example for web search: {{"action": "search_duckduckgo_and_report", "action_input": {{"search_engine_query": "latest AI research"}}}}
|
156 |
Example for memory recall: {{"action": "answer_using_conversation_memory", "action_input": {{}}}}
|
157 |
"""
|
@@ -190,33 +190,28 @@ Example for memory recall: {{"action": "answer_using_conversation_memory", "acti
|
|
190 |
final_user_prompt_content_str = f"History:\n{history_str_for_prompt}\nGuidelines:\n{initial_insights_ctx_str}\nQuery: \"{user_input}\"\nResponse:"
|
191 |
|
192 |
elif action_type == "answer_using_conversation_memory":
|
193 |
-
# --- MODIFIED: Query Transformation Step ---
|
194 |
yield "status", "<i>[Optimizing query for long-term memory search...]</i>"
|
195 |
|
196 |
-
|
197 |
-
optimized_query = user_input # Fallback
|
198 |
try:
|
199 |
query_gen_system_prompt = "You are an expert at reformulating a user's question into a concise, effective search query for a vector database. Given the conversation history and the user's latest query, extract the key topics and entities to create a self-contained query. The query should be a short phrase or question. Output ONLY the query text, nothing else."
|
200 |
query_gen_user_prompt = f"Conversation History:\n{history_str_for_prompt}\n\nUser's Latest Query: \"{user_input}\"\n\nOptimized Search Query:"
|
201 |
query_gen_messages = [{"role":"system", "content":query_gen_system_prompt}, {"role":"user", "content":query_gen_user_prompt}]
|
202 |
|
203 |
-
# Use the same fast model as the tool decider
|
204 |
query_gen_chunks = list(call_model_stream(provider=tool_provider, model_display_name=tool_model_display, messages=query_gen_messages, temperature=0.0, max_tokens=50))
|
205 |
generated_query = "".join(query_gen_chunks).strip()
|
206 |
|
207 |
if generated_query:
|
208 |
-
optimized_query = generated_query.replace('"', '')
|
209 |
logger.info(f"PUI_GRADIO [{request_id}]: Original query: '{user_input}'. Optimized memory search query: '{optimized_query}'")
|
210 |
else:
|
211 |
logger.warning(f"PUI_GRADIO [{request_id}]: Query generation returned empty. Using original input.")
|
212 |
except Exception as e:
|
213 |
logger.error(f"PUI_GRADIO [{request_id}]: Error during query generation: {e}. Using original input.")
|
214 |
|
215 |
-
# 2. Use the optimized query to search the unified memory
|
216 |
yield "status", f"<i>[Searching all memories with query: '{optimized_query[:40]}...']</i>"
|
217 |
retrieved_mems = retrieve_memories_semantic(optimized_query, k=3)
|
218 |
|
219 |
-
# 3. Build the context for the final response
|
220 |
if retrieved_mems:
|
221 |
logger.info(f"PUI_GRADIO [{request_id}]: Found {len(retrieved_mems)} relevant memories.")
|
222 |
memory_context = "Relevant Past Interactions (for your context only, do not repeat verbatim):\n" + "\n".join([f"- User asked: '{m.get('user_input','')}'. You responded: '{m.get('bot_response','')}'. (Key takeaway: {m.get('metrics',{}).get('takeaway','N/A')})" for m in retrieved_mems])
|
@@ -226,7 +221,6 @@ Example for memory recall: {{"action": "answer_using_conversation_memory", "acti
|
|
226 |
|
227 |
final_system_prompt_str += " You MUST use the provided 'Memory Context' to inform your answer. Synthesize the information from the memory with the current conversation history to respond to the user's query."
|
228 |
final_user_prompt_content_str = f"History:\n{history_str_for_prompt}\n\nGuidelines:\n{initial_insights_ctx_str}\n\nMemory Context:\n{memory_context}\n\nUser's Query: \"{user_input}\"\n\nResponse (use the Memory Context to answer the query):"
|
229 |
-
# --- END OF MODIFICATION ---
|
230 |
|
231 |
elif action_type in ["search_duckduckgo_and_report", "scrape_url_and_report"]:
|
232 |
query_or_url = action_input_dict.get("search_engine_query") if "search" in action_type else action_input_dict.get("url")
|
@@ -264,7 +258,8 @@ Example for memory recall: {{"action": "answer_using_conversation_memory", "acti
|
|
264 |
logger.info(f"PUI_GRADIO [{request_id}]: Finished. Total: {time.time() - process_start_time:.2f}s. Resp len: {len(final_bot_text)}")
|
265 |
yield "final_response_and_insights", {"response": final_bot_text, "insights_used": parsed_initial_insights_list}
|
266 |
|
267 |
-
# The rest of the app.py file
|
|
|
268 |
def perform_post_interaction_learning(user_input: str, bot_response: str, provider: str, model_disp_name: str, insights_reflected: list[dict], api_key_override: str = None):
|
269 |
task_id = os.urandom(4).hex()
|
270 |
logger.info(f"POST_INTERACTION_LEARNING [{task_id}]: START User='{user_input[:40]}...', Bot='{bot_response[:40]}...'")
|
|
|
151 |
Available Actions and their descriptions:
|
152 |
{tool_descriptions_for_prompt}
|
153 |
|
154 |
+
Based on the query and the action descriptions, select the single best action to take. Output the corresponding JSON.
|
155 |
Example for web search: {{"action": "search_duckduckgo_and_report", "action_input": {{"search_engine_query": "latest AI research"}}}}
|
156 |
Example for memory recall: {{"action": "answer_using_conversation_memory", "action_input": {{}}}}
|
157 |
"""
|
|
|
190 |
final_user_prompt_content_str = f"History:\n{history_str_for_prompt}\nGuidelines:\n{initial_insights_ctx_str}\nQuery: \"{user_input}\"\nResponse:"
|
191 |
|
192 |
elif action_type == "answer_using_conversation_memory":
|
|
|
193 |
yield "status", "<i>[Optimizing query for long-term memory search...]</i>"
|
194 |
|
195 |
+
optimized_query = user_input
|
|
|
196 |
try:
|
197 |
query_gen_system_prompt = "You are an expert at reformulating a user's question into a concise, effective search query for a vector database. Given the conversation history and the user's latest query, extract the key topics and entities to create a self-contained query. The query should be a short phrase or question. Output ONLY the query text, nothing else."
|
198 |
query_gen_user_prompt = f"Conversation History:\n{history_str_for_prompt}\n\nUser's Latest Query: \"{user_input}\"\n\nOptimized Search Query:"
|
199 |
query_gen_messages = [{"role":"system", "content":query_gen_system_prompt}, {"role":"user", "content":query_gen_user_prompt}]
|
200 |
|
|
|
201 |
query_gen_chunks = list(call_model_stream(provider=tool_provider, model_display_name=tool_model_display, messages=query_gen_messages, temperature=0.0, max_tokens=50))
|
202 |
generated_query = "".join(query_gen_chunks).strip()
|
203 |
|
204 |
if generated_query:
|
205 |
+
optimized_query = generated_query.replace('"', '')
|
206 |
logger.info(f"PUI_GRADIO [{request_id}]: Original query: '{user_input}'. Optimized memory search query: '{optimized_query}'")
|
207 |
else:
|
208 |
logger.warning(f"PUI_GRADIO [{request_id}]: Query generation returned empty. Using original input.")
|
209 |
except Exception as e:
|
210 |
logger.error(f"PUI_GRADIO [{request_id}]: Error during query generation: {e}. Using original input.")
|
211 |
|
|
|
212 |
yield "status", f"<i>[Searching all memories with query: '{optimized_query[:40]}...']</i>"
|
213 |
retrieved_mems = retrieve_memories_semantic(optimized_query, k=3)
|
214 |
|
|
|
215 |
if retrieved_mems:
|
216 |
logger.info(f"PUI_GRADIO [{request_id}]: Found {len(retrieved_mems)} relevant memories.")
|
217 |
memory_context = "Relevant Past Interactions (for your context only, do not repeat verbatim):\n" + "\n".join([f"- User asked: '{m.get('user_input','')}'. You responded: '{m.get('bot_response','')}'. (Key takeaway: {m.get('metrics',{}).get('takeaway','N/A')})" for m in retrieved_mems])
|
|
|
221 |
|
222 |
final_system_prompt_str += " You MUST use the provided 'Memory Context' to inform your answer. Synthesize the information from the memory with the current conversation history to respond to the user's query."
|
223 |
final_user_prompt_content_str = f"History:\n{history_str_for_prompt}\n\nGuidelines:\n{initial_insights_ctx_str}\n\nMemory Context:\n{memory_context}\n\nUser's Query: \"{user_input}\"\n\nResponse (use the Memory Context to answer the query):"
|
|
|
224 |
|
225 |
elif action_type in ["search_duckduckgo_and_report", "scrape_url_and_report"]:
|
226 |
query_or_url = action_input_dict.get("search_engine_query") if "search" in action_type else action_input_dict.get("url")
|
|
|
258 |
logger.info(f"PUI_GRADIO [{request_id}]: Finished. Total: {time.time() - process_start_time:.2f}s. Resp len: {len(final_bot_text)}")
|
259 |
yield "final_response_and_insights", {"response": final_bot_text, "insights_used": parsed_initial_insights_list}
|
260 |
|
261 |
+
# The rest of the app.py file is unchanged and correct.
|
262 |
+
# ... (perform_post_interaction_learning, handle_gradio_chat_submit, UI definitions, etc.)
|
263 |
def perform_post_interaction_learning(user_input: str, bot_response: str, provider: str, model_disp_name: str, insights_reflected: list[dict], api_key_override: str = None):
|
264 |
task_id = os.urandom(4).hex()
|
265 |
logger.info(f"POST_INTERACTION_LEARNING [{task_id}]: START User='{user_input[:40]}...', Bot='{bot_response[:40]}...'")
|