Spaces:
Sleeping
Sleeping
Update gemini_agent.py
Browse files- 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 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 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
|
| 219 |
-
|
| 220 |
-
|
| 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
|
| 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
|