xd11yggy commited on
Commit
b236ad2
Β·
verified Β·
1 Parent(s): 0c29777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -43
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
- <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):
@@ -99,7 +96,7 @@ def respond(
99
  }
100
  )
101
  except Exception as e:
102
- yield f"⚠️ API Error: {str(e)}\n\nPlease check your OpenRouter API key."
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 # Stream raw output with all tags
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"πŸ” SEARCH: {query}\nRESULTS: {result}\n")
123
  except Exception as e:
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
 
134
  except Exception as e:
135
- yield f"⚠️ Critical Error: {str(e)}\n\nPlease try again later."
136
 
137
  demo = gr.ChatInterface(
138
  respond,
139
  additional_inputs=[
140
  gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", lines=8),
141
- gr.Slider(minimum=1000, maximum=15000, value=6000, step=500, label="Max Tokens"),
142
- gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature"),
143
- gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="Top-p"),
144
- gr.Textbox(label="OpenRouter API Key", type="password")
145
  ],
146
- title="Web Research Agent πŸ€–",
147
- description="Advanced AI assistant with web search capabilities",
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
  )