Kackle commited on
Commit
4661344
·
verified ·
1 Parent(s): ebea668

Update gemini_agent.py

Browse files
Files changed (1) hide show
  1. gemini_agent.py +15 -36
gemini_agent.py CHANGED
@@ -163,40 +163,29 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
163
  async def _handle_text_question(self, question: str) -> str:
164
  """Handle regular text-based questions"""
165
  prompt = ""
166
- # Improved: Use retrieval for Wikipedia-style fact questions
167
  def is_explicit_retrieval_question(question):
168
  q = question.lower()
169
- # Add more patterns for Wikipedia-relevant fact questions
170
- if (
171
  "according to wikipedia" in q or
172
  "from wikipedia" in q or
173
  "search the web" in q or
174
  "duckduckgo" in q or
175
  "web search" in q
176
- ):
177
- return True
178
- # Factoid pattern: how many/who/what/when/where + entity + year/range
179
- if re.search(r"(how many|who|what|when|where)", q) and re.search(r"\\b(19|20)\\d{2}\\b", q):
180
- return True
181
- # If question mentions Wikipedia or is about a person/entity and a year
182
- if "wikipedia" in q:
183
- return True
184
- # Mercedes Sosa/artist/album/year pattern (custom for your use case)
185
- if ("mercedes sosa" in q and ("album" in q or "discography" in q)):
186
- return True
187
- return False
188
  wiki_context = ""
189
  ddg_context = ""
190
  if is_explicit_retrieval_question(question):
191
- try:
192
- wiki_context = self.wiki_tool.run(question)
193
- except Exception as e:
194
- print(f"Wikipedia tool failed: {e}")
195
- # Optionally, also try DuckDuckGo for broader coverage
196
- try:
197
- ddg_context = self.ddg_tool.run(question)
198
- except Exception as e:
199
- print(f"DuckDuckGo tool failed: {e}")
 
200
  # Handle attached file questions with enhanced prompts
201
  if 'attached' in question.lower():
202
  if 'python code' in question.lower():
@@ -237,11 +226,10 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
237
  # Prepend context to the prompt if available and likely relevant
238
  def is_good_context(context):
239
  return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"])
240
- # Improved: Prefer Wikipedia context if available
241
  if wiki_context and is_good_context(wiki_context):
242
- prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\nProvide only the direct answer. If it's a number, give just the number. If it's a name, give just the name. No explanations.\n\n{question}\n\nAnswer:"
243
  elif ddg_context and is_good_context(ddg_context):
244
- prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\nProvide only the direct answer. If it's a number, give just the number. If it's a name, give just the name. No explanations.\n\n{question}\n\nAnswer:"
245
 
246
  # Use the constructed prompt for all cases
247
  await self._rate_limit()
@@ -253,15 +241,6 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
253
  )
254
  )
255
  answer = response.text.strip()
256
- # Improved: If expecting a number, extract from Wikipedia context if not found in answer
257
- if (re.search(r"how many", question.lower()) or re.search(r"number", question.lower())):
258
- # Try to extract a number from the answer
259
- num_match = re.search(r"\\b\\d+\\b", answer)
260
- if not num_match and wiki_context:
261
- # Try to extract from Wikipedia context
262
- num_match = re.search(r"\\b\\d+\\b", wiki_context)
263
- if num_match:
264
- answer = num_match.group(0)
265
 
266
  # Extract the core answer
267
  if ':' in answer:
 
163
  async def _handle_text_question(self, question: str) -> str:
164
  """Handle regular text-based questions"""
165
  prompt = ""
166
+ # Only use retrieval for explicit web/Wikipedia questions
167
  def is_explicit_retrieval_question(question):
168
  q = question.lower()
169
+ return (
 
170
  "according to wikipedia" in q or
171
  "from wikipedia" in q or
172
  "search the web" in q or
173
  "duckduckgo" in q or
174
  "web search" in q
175
+ )
 
 
 
 
 
 
 
 
 
 
 
176
  wiki_context = ""
177
  ddg_context = ""
178
  if is_explicit_retrieval_question(question):
179
+ if "wikipedia" in question.lower():
180
+ try:
181
+ wiki_context = self.wiki_tool.run(question)
182
+ except Exception as e:
183
+ print(f"Wikipedia tool failed: {e}")
184
+ if "duckduckgo" in question.lower() or "web search" in question.lower():
185
+ try:
186
+ ddg_context = self.ddg_tool.run(question)
187
+ except Exception as e:
188
+ print(f"DuckDuckGo tool failed: {e}")
189
  # Handle attached file questions with enhanced prompts
190
  if 'attached' in question.lower():
191
  if 'python code' in question.lower():
 
226
  # Prepend context to the prompt if available and likely relevant
227
  def is_good_context(context):
228
  return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"])
 
229
  if wiki_context and is_good_context(wiki_context):
230
+ prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\n{prompt}"
231
  elif ddg_context and is_good_context(ddg_context):
232
+ prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\n{prompt}"
233
 
234
  # Use the constructed prompt for all cases
235
  await self._rate_limit()
 
241
  )
242
  )
243
  answer = response.text.strip()
 
 
 
 
 
 
 
 
 
244
 
245
  # Extract the core answer
246
  if ':' in answer: