Kackle commited on
Commit
006dac3
·
verified ·
1 Parent(s): be2fed4

Update gemini_agent.py

Browse files
Files changed (1) hide show
  1. gemini_agent.py +25 -18
gemini_agent.py CHANGED
@@ -163,20 +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
  wiki_context = ""
167
  ddg_context = ""
168
- # Wikipedia retrieval
169
- if 'wikipedia' in question.lower():
170
- try:
171
- wiki_context = self.wiki_tool.run(question)
172
- except Exception as e:
173
- print(f"Wikipedia tool failed: {e}")
174
- # DuckDuckGo retrieval
175
- if 'duckduckgo' in question.lower() or 'web search' in question.lower():
176
- try:
177
- ddg_context = self.ddg_tool.run(question)
178
- except Exception as e:
179
- print(f"DuckDuckGo tool failed: {e}")
180
  # Handle attached file questions with enhanced prompts
181
  if 'attached' in question.lower():
182
  if 'python code' in question.lower():
@@ -215,13 +224,11 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
215
  prompt = f"""Answer this question with only the essential information requested:\n\n{question}\n\nAnswer:"""
216
 
217
  # Prepend context to the prompt if available and likely relevant
218
- def is_relevant_context(context, question):
219
- # Only use context if it contains a main keyword from the question and is not a generic error
220
- keywords = [w.lower().strip(',.?') for w in question.split() if len(w) > 3]
221
- return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"]) and any(kw in context.lower() for kw in keywords)
222
- if wiki_context and is_relevant_context(wiki_context, question):
223
  prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\n{prompt}"
224
- elif ddg_context and is_relevant_context(ddg_context, question):
225
  prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\n{prompt}"
226
 
227
  # Use the constructed prompt for all cases
 
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():
 
224
  prompt = f"""Answer this question with only the essential information requested:\n\n{question}\n\nAnswer:"""
225
 
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