Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,37 +9,39 @@ web_search = DuckDuckGoSearchTool()
|
|
9 |
SYSTEM_PROMPT = """
|
10 |
You are an AI research assistant that can search the web. Follow these steps:
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
"""
|
36 |
|
37 |
def process_searches(response):
|
38 |
searches = re.findall(r'<search>(.*?)</search>', response, re.DOTALL)
|
39 |
if searches:
|
40 |
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
|
41 |
-
return queries
|
42 |
-
return None
|
43 |
|
44 |
def search_with_retry(query, max_retries=3, delay=2):
|
45 |
for attempt in range(max_retries):
|
@@ -105,15 +107,13 @@ def respond(
|
|
105 |
token = chunk.choices[0].delta.content or ""
|
106 |
response += token
|
107 |
full_response += token
|
108 |
-
yield full_response # Stream
|
109 |
|
110 |
-
|
111 |
-
queries, original_response = process_searches(response)
|
112 |
|
113 |
if queries:
|
114 |
search_cycle = True
|
115 |
-
|
116 |
-
messages.append({"role": "assistant", "content": original_response})
|
117 |
|
118 |
search_results = []
|
119 |
for query in queries:
|
@@ -124,13 +124,10 @@ def respond(
|
|
124 |
search_results.append(f"β οΈ Search Error: {str(e)}\nQuery: {query}")
|
125 |
time.sleep(2)
|
126 |
|
127 |
-
# Add search results to conversation
|
128 |
messages.append({
|
129 |
"role": "user",
|
130 |
"content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}\nAnalyze these results..."
|
131 |
})
|
132 |
-
|
133 |
-
# Show search status without breaking response flow
|
134 |
full_response += "\nπ Analyzing search results...\n"
|
135 |
yield full_response
|
136 |
|
|
|
9 |
SYSTEM_PROMPT = """
|
10 |
You are an AI research assistant that can search the web. Follow these steps:
|
11 |
|
12 |
+
<thinking>
|
13 |
+
1. Analyze the user's question:
|
14 |
+
- Identify missing information or ambiguities
|
15 |
+
- Determine if clarification is needed
|
16 |
+
- Plan search strategy if required
|
17 |
+
</thinking>
|
18 |
+
|
19 |
+
If clarification is needed, ask ONE question. Otherwise:
|
20 |
+
|
21 |
+
<search>
|
22 |
+
[list of search queries]
|
23 |
+
</search>
|
24 |
+
|
25 |
+
After receiving results:
|
26 |
+
<thinking>
|
27 |
+
- Analyze information from multiple sources
|
28 |
+
- Cross-verify facts
|
29 |
+
- Determine if additional searches are needed
|
30 |
+
</thinking>
|
31 |
+
|
32 |
+
Final answer:
|
33 |
+
- Clear structure with headings
|
34 |
+
- Key facts with sources
|
35 |
+
- Concise explanations
|
36 |
+
- Academic tone
|
37 |
"""
|
38 |
|
39 |
def process_searches(response):
|
40 |
searches = re.findall(r'<search>(.*?)</search>', response, re.DOTALL)
|
41 |
if searches:
|
42 |
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
|
43 |
+
return queries
|
44 |
+
return None
|
45 |
|
46 |
def search_with_retry(query, max_retries=3, delay=2):
|
47 |
for attempt in range(max_retries):
|
|
|
107 |
token = chunk.choices[0].delta.content or ""
|
108 |
response += token
|
109 |
full_response += token
|
110 |
+
yield full_response # Stream raw output with all tags
|
111 |
|
112 |
+
queries = process_searches(response)
|
|
|
113 |
|
114 |
if queries:
|
115 |
search_cycle = True
|
116 |
+
messages.append({"role": "assistant", "content": response})
|
|
|
117 |
|
118 |
search_results = []
|
119 |
for query in queries:
|
|
|
124 |
search_results.append(f"β οΈ Search Error: {str(e)}\nQuery: {query}")
|
125 |
time.sleep(2)
|
126 |
|
|
|
127 |
messages.append({
|
128 |
"role": "user",
|
129 |
"content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}\nAnalyze these results..."
|
130 |
})
|
|
|
|
|
131 |
full_response += "\nπ Analyzing search results...\n"
|
132 |
yield full_response
|
133 |
|