Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,38 +9,35 @@ 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 |
-
</search>
|
24 |
-
|
25 |
-
After receiving results:
|
26 |
-
|
27 |
-
-
|
28 |
-
-
|
29 |
-
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
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 |
-
|
43 |
-
return queries
|
44 |
return None
|
45 |
|
46 |
def search_with_retry(query, max_retries=3, delay=2):
|
@@ -99,7 +96,7 @@ def respond(
|
|
99 |
}
|
100 |
)
|
101 |
except Exception as e:
|
102 |
-
yield f"β οΈ API Error: {str(e)}
|
103 |
return
|
104 |
|
105 |
response = ""
|
@@ -107,7 +104,7 @@ def respond(
|
|
107 |
token = chunk.choices[0].delta.content or ""
|
108 |
response += token
|
109 |
full_response += token
|
110 |
-
yield full_response #
|
111 |
|
112 |
queries = process_searches(response)
|
113 |
|
@@ -119,36 +116,34 @@ def respond(
|
|
119 |
for query in queries:
|
120 |
try:
|
121 |
result = search_with_retry(query)
|
122 |
-
search_results.append(f"
|
123 |
except Exception as e:
|
124 |
-
search_results.append(f"
|
125 |
time.sleep(2)
|
126 |
|
127 |
messages.append({
|
128 |
"role": "user",
|
129 |
-
"content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}
|
130 |
})
|
131 |
-
full_response
|
132 |
-
yield full_response
|
133 |
|
134 |
except Exception as e:
|
135 |
-
yield f"β οΈ
|
136 |
|
137 |
demo = gr.ChatInterface(
|
138 |
respond,
|
139 |
additional_inputs=[
|
140 |
gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", lines=8),
|
141 |
-
gr.Slider(
|
142 |
-
gr.Slider(
|
143 |
-
gr.Slider(
|
144 |
-
gr.Textbox(label="OpenRouter
|
145 |
],
|
146 |
-
title="Web Research Agent
|
147 |
-
description="
|
148 |
examples=[
|
149 |
["Compare COVID-19 mortality rates between US and Sweden with sources"],
|
150 |
-
["What's the current consensus on dark matter composition?"]
|
151 |
-
["Latest advancements in fusion energy 2023-2024"]
|
152 |
],
|
153 |
cache_examples=False
|
154 |
)
|
|
|
9 |
SYSTEM_PROMPT = """
|
10 |
You are an AI research assistant that can search the web. Follow these steps:
|
11 |
|
12 |
+
1. FIRST ANALYZE the user's question:
|
13 |
+
- If information is missing or ambiguous, ask ONE clarifying question
|
14 |
+
- If clear, proceed to search
|
15 |
+
|
16 |
+
2. When searching:
|
17 |
+
- Generate multiple specific search queries wrapped in <search> tags
|
18 |
+
- Focus on factual keywords, one query per line
|
19 |
+
Example:
|
20 |
+
<search>
|
21 |
+
Pont des Arts exact length meters
|
22 |
+
History of Pont des Arts bridge
|
23 |
+
</search>
|
24 |
+
|
25 |
+
3. After receiving results:
|
26 |
+
- Analyze information from multiple sources
|
27 |
+
- Cross-verify facts
|
28 |
+
- If needed, generate follow-up searches
|
29 |
+
- Provide final answer with:
|
30 |
+
- Clear structure
|
31 |
+
- Key facts with sources
|
32 |
+
- Concise explanations
|
33 |
+
|
34 |
+
Never invent information. Cite sources for all facts. Use neutral, academic tone.
|
|
|
|
|
35 |
"""
|
36 |
|
37 |
def process_searches(response):
|
38 |
searches = re.findall(r'<search>(.*?)</search>', response, re.DOTALL)
|
39 |
if searches:
|
40 |
+
return [q.strip() for q in searches[0].split('\n') if q.strip()]
|
|
|
41 |
return None
|
42 |
|
43 |
def search_with_retry(query, max_retries=3, delay=2):
|
|
|
96 |
}
|
97 |
)
|
98 |
except Exception as e:
|
99 |
+
yield f"β οΈ API Error: {str(e)}"
|
100 |
return
|
101 |
|
102 |
response = ""
|
|
|
104 |
token = chunk.choices[0].delta.content or ""
|
105 |
response += token
|
106 |
full_response += token
|
107 |
+
yield full_response # Direct streaming without processing
|
108 |
|
109 |
queries = process_searches(response)
|
110 |
|
|
|
116 |
for query in queries:
|
117 |
try:
|
118 |
result = search_with_retry(query)
|
119 |
+
search_results.append(f"Search results for '{query}':\n{result}")
|
120 |
except Exception as e:
|
121 |
+
search_results.append(f"Search failed for '{query}': {str(e)}")
|
122 |
time.sleep(2)
|
123 |
|
124 |
messages.append({
|
125 |
"role": "user",
|
126 |
+
"content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}"
|
127 |
})
|
128 |
+
yield full_response + "\nπ Analyzing results...\n"
|
|
|
129 |
|
130 |
except Exception as e:
|
131 |
+
yield f"β οΈ Error: {str(e)}"
|
132 |
|
133 |
demo = gr.ChatInterface(
|
134 |
respond,
|
135 |
additional_inputs=[
|
136 |
gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", lines=8),
|
137 |
+
gr.Slider(1000, 15000, 6000, step=500, label="Max Tokens"),
|
138 |
+
gr.Slider(0.1, 1.0, 0.5, step=0.1, label="Temperature"),
|
139 |
+
gr.Slider(0.1, 1.0, 0.85, step=0.05, label="Top-p"),
|
140 |
+
gr.Textbox(label="OpenRouter Key", type="password")
|
141 |
],
|
142 |
+
title="Web Research Agent",
|
143 |
+
description="AI assistant with web search capabilities",
|
144 |
examples=[
|
145 |
["Compare COVID-19 mortality rates between US and Sweden with sources"],
|
146 |
+
["What's the current consensus on dark matter composition?"]
|
|
|
147 |
],
|
148 |
cache_examples=False
|
149 |
)
|