Spaces:
Sleeping
Sleeping
Update gemini_agent.py
Browse files- gemini_agent.py +22 -43
gemini_agent.py
CHANGED
|
@@ -19,7 +19,7 @@ class GeminiAgent:
|
|
| 19 |
api_key = os.getenv('GOOGLE_API_KEY')
|
| 20 |
genai.configure(api_key=api_key)
|
| 21 |
|
| 22 |
-
self.model = genai.GenerativeModel('gemini-2.
|
| 23 |
self.last_request_time = 0
|
| 24 |
self.min_request_interval = 8.0 # 7 seconds between requests (10 per minute limit, with margin)
|
| 25 |
|
|
@@ -163,43 +163,27 @@ 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 |
-
#
|
| 167 |
def is_explicit_retrieval_question(question):
|
| 168 |
q = question.lower()
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
"
|
| 172 |
-
"
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
"who played", "actor who", "actress who", "cast of", "starred in",
|
| 177 |
-
"tv show", "television", "movie", "film", "character in",
|
| 178 |
-
"version of", "adaptation of", "remake of",
|
| 179 |
-
# New triggers for entity-relationship/role questions
|
| 180 |
-
"who did the actor", "who did the actress", "role in", "portrayed in", "played in", "give only the first name"
|
| 181 |
-
]
|
| 182 |
-
# Also trigger if question starts with 'who' and mentions 'actor', 'actress', 'role', or 'character'
|
| 183 |
-
if q.startswith('who') and any(x in q for x in ["actor", "actress", "role", "character"]):
|
| 184 |
-
return True
|
| 185 |
-
return any(trigger in q for trigger in explicit_triggers + factual_triggers)
|
| 186 |
wiki_context = ""
|
| 187 |
ddg_context = ""
|
| 188 |
if is_explicit_retrieval_question(question):
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
# Use DuckDuckGo as fallback if Wikipedia context is poor or for web search requests
|
| 196 |
-
def is_poor_context(context):
|
| 197 |
-
return not context or any(x in context.lower() for x in ["not found", "no results", "does not contain", "disambiguation"])
|
| 198 |
-
|
| 199 |
-
if "duckduckgo" in question.lower() or "web search" in question.lower() or is_poor_context(wiki_context):
|
| 200 |
try:
|
| 201 |
ddg_context = self.ddg_tool.run(question)
|
| 202 |
-
print(f"DDG context: {ddg_context[:200]}...") # Debug
|
| 203 |
except Exception as e:
|
| 204 |
print(f"DuckDuckGo tool failed: {e}")
|
| 205 |
# Handle attached file questions with enhanced prompts
|
|
@@ -241,12 +225,9 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 241 |
|
| 242 |
# Prepend context to the prompt if available and likely relevant
|
| 243 |
def is_good_context(context):
|
| 244 |
-
return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain
|
| 245 |
if wiki_context and is_good_context(wiki_context):
|
| 246 |
-
|
| 247 |
-
prompt = f"Based on this Wikipedia information, answer the question and give only the first name as the answer.\n{wiki_context}\n\n{question}\n\nAnswer:"
|
| 248 |
-
else:
|
| 249 |
-
prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\n{prompt}"
|
| 250 |
elif ddg_context and is_good_context(ddg_context):
|
| 251 |
prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\n{prompt}"
|
| 252 |
|
|
@@ -279,13 +260,11 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 279 |
answer = sentences[0] + '.'
|
| 280 |
|
| 281 |
# If the question expects a single value, extract it
|
| 282 |
-
if any(kw in question.lower() for kw in ["how many", "what is the", "who", "where", "give only", "provide only"
|
| 283 |
-
#
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
if name_match:
|
| 288 |
-
answer = name_match.group(1)
|
| 289 |
|
| 290 |
# Post-processing for chess move extraction
|
| 291 |
if 'chess position' in question.lower() and 'image' in question.lower():
|
|
|
|
| 19 |
api_key = os.getenv('GOOGLE_API_KEY')
|
| 20 |
genai.configure(api_key=api_key)
|
| 21 |
|
| 22 |
+
self.model = genai.GenerativeModel('gemini-2.0-flash')
|
| 23 |
self.last_request_time = 0
|
| 24 |
self.min_request_interval = 8.0 # 7 seconds between requests (10 per minute limit, with margin)
|
| 25 |
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 260 |
answer = sentences[0] + '.'
|
| 261 |
|
| 262 |
# If the question expects a single value, extract it
|
| 263 |
+
if any(kw in question.lower() for kw in ["how many", "what is the", "who", "where", "give only", "provide only"]):
|
| 264 |
+
# Extract the first number, word, or phrase (tweak regex as needed)
|
| 265 |
+
match = re.search(r'^[A-Za-z0-9 ,+-]+', answer)
|
| 266 |
+
if match:
|
| 267 |
+
answer = match.group(0).strip()
|
|
|
|
|
|
|
| 268 |
|
| 269 |
# Post-processing for chess move extraction
|
| 270 |
if 'chess position' in question.lower() and 'image' in question.lower():
|