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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -33
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
- 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
  queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
41
- return queries, response # Return both queries and original response
42
- return None, response
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 every token immediately
109
 
110
- # Process searches without modifying original response
111
- queries, original_response = process_searches(response)
112
 
113
  if queries:
114
  search_cycle = True
115
- # Preserve original response with <search> tags
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