Spaces:
Sleeping
Sleeping
Update gemini_agent.py
Browse files- gemini_agent.py +22 -18
gemini_agent.py
CHANGED
|
@@ -76,7 +76,7 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 76 |
video_prompt,
|
| 77 |
generation_config=genai.types.GenerationConfig(
|
| 78 |
max_output_tokens=50,
|
| 79 |
-
temperature=0.
|
| 80 |
)
|
| 81 |
)
|
| 82 |
answer = response.text.strip()
|
|
@@ -144,7 +144,7 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 144 |
excel_prompt,
|
| 145 |
generation_config=genai.types.GenerationConfig(
|
| 146 |
max_output_tokens=150,
|
| 147 |
-
temperature=0.
|
| 148 |
)
|
| 149 |
)
|
| 150 |
answer = response.text.strip()
|
|
@@ -163,24 +163,28 @@ 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 |
-
"from wikipedia"
|
| 172 |
-
"
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
wiki_context = ""
|
| 177 |
ddg_context = ""
|
| 178 |
if is_explicit_retrieval_question(question):
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 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)
|
|
@@ -189,11 +193,11 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 189 |
# Handle attached file questions with enhanced prompts
|
| 190 |
if 'attached' in question.lower():
|
| 191 |
if 'python code' in question.lower():
|
| 192 |
-
prompt = f"""
|
| 193 |
elif '.mp3' in question.lower():
|
| 194 |
-
prompt = f"""This question refers to an attached audio file.
|
| 195 |
else:
|
| 196 |
-
prompt = f"""
|
| 197 |
# Handle chess position question
|
| 198 |
elif 'chess position' in question.lower() and 'image' in question.lower():
|
| 199 |
prompt = f"""This is a chess question with an attached image. Provide the best chess move in algebraic notation:\n\n{question}\n\nAnswer:"""
|
|
|
|
| 76 |
video_prompt,
|
| 77 |
generation_config=genai.types.GenerationConfig(
|
| 78 |
max_output_tokens=50,
|
| 79 |
+
temperature=0.0
|
| 80 |
)
|
| 81 |
)
|
| 82 |
answer = response.text.strip()
|
|
|
|
| 144 |
excel_prompt,
|
| 145 |
generation_config=genai.types.GenerationConfig(
|
| 146 |
max_output_tokens=150,
|
| 147 |
+
temperature=0.0
|
| 148 |
)
|
| 149 |
)
|
| 150 |
answer = response.text.strip()
|
|
|
|
| 163 |
async def _handle_text_question(self, question: str) -> str:
|
| 164 |
"""Handle regular text-based questions"""
|
| 165 |
prompt = ""
|
| 166 |
+
# Use retrieval for explicit web/Wikipedia questions and factual queries
|
| 167 |
def is_explicit_retrieval_question(question):
|
| 168 |
q = question.lower()
|
| 169 |
+
# Explicit retrieval requests
|
| 170 |
+
explicit_triggers = [
|
| 171 |
+
"according to wikipedia", "from wikipedia", "search the web",
|
| 172 |
+
"duckduckgo", "web search"
|
| 173 |
+
]
|
| 174 |
+
# Factual question patterns that benefit from Wikipedia
|
| 175 |
+
factual_triggers = [
|
| 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 |
+
]
|
| 180 |
+
return any(trigger in q for trigger in explicit_triggers + factual_triggers)
|
| 181 |
wiki_context = ""
|
| 182 |
ddg_context = ""
|
| 183 |
if is_explicit_retrieval_question(question):
|
| 184 |
+
try:
|
| 185 |
+
wiki_context = self.wiki_tool.run(question)
|
| 186 |
+
except Exception as e:
|
| 187 |
+
print(f"Wikipedia tool failed: {e}")
|
|
|
|
| 188 |
if "duckduckgo" in question.lower() or "web search" in question.lower():
|
| 189 |
try:
|
| 190 |
ddg_context = self.ddg_tool.run(question)
|
|
|
|
| 193 |
# Handle attached file questions with enhanced prompts
|
| 194 |
if 'attached' in question.lower():
|
| 195 |
if 'python code' in question.lower():
|
| 196 |
+
prompt = f"""This question refers to attached Python code. Based on typical code execution patterns, provide the most likely numeric output:\n\n{question}\n\nAnswer:"""
|
| 197 |
elif '.mp3' in question.lower():
|
| 198 |
+
prompt = f"""This question refers to an attached audio file. Provide the most likely answer based on the context:\n\n{question}\n\nAnswer:"""
|
| 199 |
else:
|
| 200 |
+
prompt = f"""This question refers to an attached file. Provide the most likely answer:\n\n{question}\n\nAnswer:"""
|
| 201 |
# Handle chess position question
|
| 202 |
elif 'chess position' in question.lower() and 'image' in question.lower():
|
| 203 |
prompt = f"""This is a chess question with an attached image. Provide the best chess move in algebraic notation:\n\n{question}\n\nAnswer:"""
|