Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
|
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
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
176 |
else:
|
177 |
-
return
|
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 |
|