tatianija commited on
Commit
e73a565
·
verified ·
1 Parent(s): 291f4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -27
app.py CHANGED
@@ -122,26 +122,30 @@ Answer:"""
122
  return "No search results found. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
123
 
124
  # Format search results - handle different result formats
125
- formatted_results = []
126
- for i, result in enumerate(search_results[:3]): # Use top 3 results
127
- if isinstance(result, dict):
128
- title = result.get("title", "No title")
129
- snippet = result.get("snippet", "").strip()
130
- link = result.get("link", "")
131
- elif isinstance(result, str):
132
- # If result is a string, use it directly
133
- title = f"Result {i+1}"
134
- snippet = result
135
- link = ""
136
- else:
137
- # Handle other formats
138
- title = f"Result {i+1}"
139
- snippet = str(result)
140
- link = ""
 
 
 
 
 
 
141
 
142
- formatted_results.append(f"Result {i+1}:\nTitle: {title}\nContent: {snippet}\nSource: {link}")
143
-
144
- search_context = "\n\n".join(formatted_results)
145
 
146
  # Generate answer using search context
147
  answer_prompt = f"""You are a helpful AI assistant. Use the provided search results to answer the question accurately. Synthesize information from multiple sources when relevant, and cite sources when appropriate.
@@ -165,16 +169,23 @@ Answer:"""
165
  return response.strip()
166
 
167
  except Exception as e:
 
 
168
  # Fallback to simple search result formatting
169
- if search_results and len(search_results) > 0:
170
- top_result = search_results[0]
171
- if isinstance(top_result, dict):
172
- title = top_result.get("title", "No title")
173
- snippet = top_result.get("snippet", "").strip()
174
- link = top_result.get("link", "")
175
- return f"**{title}**\n\n{snippet}\n\nSource: {link}"
 
 
 
 
 
176
  else:
177
- return f"Search result: {str(top_result)}"
178
  else:
179
  return "Search completed but no usable results found."
180
 
 
122
  return "No search results found. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
123
 
124
  # Format search results - handle different result formats
125
+ if self.debug:
126
+ print(f"First result type: {type(search_results[0]) if search_results else 'None'}")
127
+ print(f"First result: {search_results[0] if search_results else 'None'}")
128
+
129
+ # If search_results is a string, use it directly
130
+ if isinstance(search_results, str):
131
+ search_context = search_results
132
+ else:
133
+ # Handle list of results
134
+ formatted_results = []
135
+ for i, result in enumerate(search_results[:3]): # Use top 3 results
136
+ if isinstance(result, dict):
137
+ title = result.get("title", "No title")
138
+ snippet = result.get("snippet", "").strip()
139
+ link = result.get("link", "")
140
+ formatted_results.append(f"Title: {title}\nContent: {snippet}\nSource: {link}")
141
+ elif isinstance(result, str):
142
+ # If result is a string, use it directly
143
+ formatted_results.append(result)
144
+ else:
145
+ # Handle other formats
146
+ formatted_results.append(str(result))
147
 
148
+ search_context = "\n\n".join(formatted_results)
 
 
149
 
150
  # Generate answer using search context
151
  answer_prompt = f"""You are a helpful AI assistant. Use the provided search results to answer the question accurately. Synthesize information from multiple sources when relevant, and cite sources when appropriate.
 
169
  return response.strip()
170
 
171
  except Exception as e:
172
+ if self.debug:
173
+ print(f"LLM generation error: {e}")
174
  # Fallback to simple search result formatting
175
+ if search_results:
176
+ if isinstance(search_results, str):
177
+ return search_results
178
+ elif isinstance(search_results, list) and len(search_results) > 0:
179
+ first_result = search_results[0]
180
+ if isinstance(first_result, dict):
181
+ title = first_result.get("title", "Search Result")
182
+ snippet = first_result.get("snippet", "").strip()
183
+ link = first_result.get("link", "")
184
+ return f"**{title}**\n\n{snippet}\n\n{f'Source: {link}' if link else ''}"
185
+ else:
186
+ return str(first_result)
187
  else:
188
+ return str(search_results)
189
  else:
190
  return "Search completed but no usable results found."
191