Prof-Reza commited on
Commit
a977462
·
verified ·
1 Parent(s): 7fca00d

Normalize web search results and handle Tavily results dict; ensure search uses TAVILY_API_KEY

Browse files
Files changed (1) hide show
  1. app.py +31 -11
app.py CHANGED
@@ -34,17 +34,29 @@ def chat(user_message, chat_history, chat_pairs, sources, plan):
34
  try:
35
  # Perform web search using the entire user message as the query
36
  results = run_web_search(user_message, num_results=5, domain_filter="")
 
 
 
 
 
 
 
 
 
 
37
  # Ensure the sources list is initialised
38
  if sources is None:
39
  sources = []
40
- sources.extend(results)
41
  # Summarise results into a simple string with title and URL
42
  summary_lines = []
43
- for r in results:
44
- title = r.get("title", "")
45
- url = r.get("url", "")
46
- if title or url:
47
- summary_lines.append(f"{title} - {url}")
 
 
48
  if summary_lines:
49
  assistant_reply = "Here are some resources I found:\n" + "\n".join(summary_lines)
50
  else:
@@ -133,15 +145,23 @@ def run_search(query, chat_history, chat_pairs, sources, plan, num_results=5, do
133
  f"(Error: {e})"
134
  )
135
  return summary, chat_history, chat_pairs, sources or [], plan
 
 
 
 
 
 
 
136
  if sources is None:
137
  sources = []
138
- sources.extend(results)
139
  # summarise results into a string for display
140
  summary_lines = []
141
- for r in results:
142
- title = r.get("title", "")
143
- url = r.get("url", "")
144
- summary_lines.append(f"{title} - {url}")
 
145
  summary = "\n".join(summary_lines)
146
  return summary, chat_history, chat_pairs, sources, plan
147
 
 
34
  try:
35
  # Perform web search using the entire user message as the query
36
  results = run_web_search(user_message, num_results=5, domain_filter="")
37
+ # Normalize results:
38
+ # Tavily may return a dictionary with a "results" key containing
39
+ # the list of search results. If so, extract that list. If it's a
40
+ # list already, use it directly. Otherwise, default to an empty list.
41
+ if isinstance(results, dict):
42
+ normalized_results = results.get("results", [])
43
+ elif isinstance(results, list):
44
+ normalized_results = results
45
+ else:
46
+ normalized_results = []
47
  # Ensure the sources list is initialised
48
  if sources is None:
49
  sources = []
50
+ sources.extend(normalized_results)
51
  # Summarise results into a simple string with title and URL
52
  summary_lines = []
53
+ for r in normalized_results:
54
+ # Defensive: ensure r is a dict
55
+ if isinstance(r, dict):
56
+ title = r.get("title", "")
57
+ url = r.get("url", "")
58
+ if title or url:
59
+ summary_lines.append(f"{title} - {url}")
60
  if summary_lines:
61
  assistant_reply = "Here are some resources I found:\n" + "\n".join(summary_lines)
62
  else:
 
145
  f"(Error: {e})"
146
  )
147
  return summary, chat_history, chat_pairs, sources or [], plan
148
+ # Normalize the results: Tavily may return a dict with 'results'
149
+ if isinstance(results, dict):
150
+ normalized_results = results.get("results", [])
151
+ elif isinstance(results, list):
152
+ normalized_results = results
153
+ else:
154
+ normalized_results = []
155
  if sources is None:
156
  sources = []
157
+ sources.extend(normalized_results)
158
  # summarise results into a string for display
159
  summary_lines = []
160
+ for r in normalized_results:
161
+ if isinstance(r, dict):
162
+ title = r.get("title", "")
163
+ url = r.get("url", "")
164
+ summary_lines.append(f"{title} - {url}")
165
  summary = "\n".join(summary_lines)
166
  return summary, chat_history, chat_pairs, sources, plan
167