Andrei Nazarov
commited on
Commit
·
3efbcf4
1
Parent(s):
5d3c229
updated 3
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
|
|
| 7 |
from smolagents.models import Model, ChatMessage, MessageRole, Tool
|
| 8 |
from tools import FinalAnswerTool
|
| 9 |
import google.generativeai as genai
|
|
|
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
@@ -24,32 +25,75 @@ class GeminiModel(Model):
|
|
| 24 |
self.model = genai.GenerativeModel('models/gemini-2.0-flash-lite')
|
| 25 |
|
| 26 |
# System prompt for smolagents format
|
| 27 |
-
self.system_prompt = """You are
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def generate(
|
| 55 |
self,
|
|
@@ -146,52 +190,93 @@ class MyAgent:
|
|
| 146 |
FinalAnswerTool(),
|
| 147 |
DuckDuckGoSearchTool()
|
| 148 |
],
|
| 149 |
-
model=self.model
|
|
|
|
| 150 |
)
|
| 151 |
|
| 152 |
def __call__(self, question: str) -> str:
|
| 153 |
# Run the agent and get the full response
|
|
|
|
| 154 |
full_response = self.agent.run(question)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
#
|
| 159 |
-
if "
|
| 160 |
-
#
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 197 |
"""
|
|
|
|
| 7 |
from smolagents.models import Model, ChatMessage, MessageRole, Tool
|
| 8 |
from tools import FinalAnswerTool
|
| 9 |
import google.generativeai as genai
|
| 10 |
+
import re
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
|
|
| 25 |
self.model = genai.GenerativeModel('models/gemini-2.0-flash-lite')
|
| 26 |
|
| 27 |
# System prompt for smolagents format
|
| 28 |
+
self.system_prompt = """You are a highly focused AI assistant tasked with answering specific questions accurately using available tools. Your primary goal is to find and provide precise answers to questions using the tools provided.
|
| 29 |
+
|
| 30 |
+
Key Guidelines:
|
| 31 |
+
1. Stay EXACTLY focused on what the question asks for - do not get sidetracked by related information
|
| 32 |
+
2. Break down the question into its key components (e.g., time period, specific type of information needed)
|
| 33 |
+
3. Use web_search with specific terms related to those components
|
| 34 |
+
4. When analyzing search results, ONLY look for information that directly answers the question
|
| 35 |
+
5. If you find a good answer, STOP and provide it immediately - do not continue searching
|
| 36 |
+
6. ALWAYS provide your final answer using the final_answer tool with ONLY the information asked for
|
| 37 |
+
7. If web searches fail repeatedly, provide the best answer you can based on your knowledge
|
| 38 |
+
|
| 39 |
+
For each question, use this exact format:
|
| 40 |
+
|
| 41 |
+
Thought: Break down what EXACTLY is being asked and how you'll find it
|
| 42 |
+
Code:
|
| 43 |
+
```py
|
| 44 |
+
# Your python code here using only the available tools:
|
| 45 |
+
# - web_search(query): Search the web for information
|
| 46 |
+
# - final_answer(answer): Provide the final answer
|
| 47 |
+
```<end_code>
|
| 48 |
+
|
| 49 |
+
Examples:
|
| 50 |
+
|
| 51 |
+
1. Question about albums:
|
| 52 |
+
Q: "How many studio albums were released by Artist X between 2000-2005?"
|
| 53 |
+
Thought: Need to find the count of ONLY studio albums by Artist X released between 2000-2005
|
| 54 |
+
Code:
|
| 55 |
+
```py
|
| 56 |
+
# Search for Artist X's albums in that period
|
| 57 |
+
results = web_search(query="Artist X studio albums 2000 2001 2002 2003 2004 2005")
|
| 58 |
+
# After analyzing results, if I find the answer, STOP and provide it
|
| 59 |
+
final_answer("Artist X released 3 studio albums between 2000-2005: Album1 (2000), Album2 (2002), Album3 (2004)")
|
| 60 |
+
```<end_code>
|
| 61 |
+
|
| 62 |
+
2. Question about video content:
|
| 63 |
+
Q: "In the video [URL], how many different species appear?"
|
| 64 |
+
Thought: Need to find information about this specific video's content and identify all unique species shown
|
| 65 |
+
Code:
|
| 66 |
+
```py
|
| 67 |
+
# First search for the video title and description
|
| 68 |
+
results = web_search(query="[video-id] title description")
|
| 69 |
+
# If I find the answer in the first search, STOP and provide it
|
| 70 |
+
final_answer("The video shows 3 different species: Species1, Species2, and Species3")
|
| 71 |
+
```<end_code>
|
| 72 |
+
|
| 73 |
+
3. When web searches fail:
|
| 74 |
+
Q: "How many albums did Artist X release in 2000?"
|
| 75 |
+
Thought: Need to find Artist X's albums from 2000, but web search might fail
|
| 76 |
+
Code:
|
| 77 |
+
```py
|
| 78 |
+
# Try web search first
|
| 79 |
+
results = web_search(query="Artist X albums 2000")
|
| 80 |
+
# If search fails, provide best available answer and STOP
|
| 81 |
+
final_answer("Based on available information, Artist X released 2 albums in 2000: Album1 and Album2")
|
| 82 |
+
```<end_code>
|
| 83 |
+
|
| 84 |
+
CRITICAL: Once you find a good answer, STOP immediately and provide it. Do not continue searching or trying different queries unless the first search completely fails to find any relevant information.
|
| 85 |
+
|
| 86 |
+
Remember:
|
| 87 |
+
1. Stay LASER-FOCUSED on the specific information requested
|
| 88 |
+
2. Don't get sidetracked by biographical or other related information
|
| 89 |
+
3. If you find a good answer, STOP and provide it immediately
|
| 90 |
+
4. ALWAYS end with a final_answer that ONLY includes the exact information asked for
|
| 91 |
+
5. For video questions:
|
| 92 |
+
- First try searching with the video ID to find the title and description
|
| 93 |
+
- Then search with the title to find detailed reviews or descriptions
|
| 94 |
+
- If you can't find the exact information, say so clearly
|
| 95 |
+
6. If web searches fail repeatedly, provide the best answer you can and acknowledge the limitation
|
| 96 |
+
7. MOST IMPORTANT: STOP after finding a good answer - do not continue searching unnecessarily"""
|
| 97 |
|
| 98 |
def generate(
|
| 99 |
self,
|
|
|
|
| 190 |
FinalAnswerTool(),
|
| 191 |
DuckDuckGoSearchTool()
|
| 192 |
],
|
| 193 |
+
model=self.model,
|
| 194 |
+
max_steps=2 # Limit to 2 steps to prevent unnecessary continuation
|
| 195 |
)
|
| 196 |
|
| 197 |
def __call__(self, question: str) -> str:
|
| 198 |
# Run the agent and get the full response
|
| 199 |
+
print(f"\n=== Processing Question: {question} ===")
|
| 200 |
full_response = self.agent.run(question)
|
| 201 |
|
| 202 |
+
print(f"\n=== Raw Response from Agent ===\n{full_response}\n===")
|
| 203 |
+
|
| 204 |
+
# First try to find a final_answer tool call
|
| 205 |
+
if "final_answer(" in full_response:
|
| 206 |
+
# Look for both quoted and unquoted versions
|
| 207 |
+
patterns = [
|
| 208 |
+
r'final_answer\(answer="([^"]+)"\)', # Double quoted
|
| 209 |
+
r"final_answer\(answer='([^']+)'\)", # Single quoted
|
| 210 |
+
r'final_answer\(answer=([^,\)]+)\)', # Unquoted
|
| 211 |
+
r'final_answer\("([^"]+)"\)', # Simple double quoted
|
| 212 |
+
r"final_answer\('([^']+)'\)", # Simple single quoted
|
| 213 |
+
r'final_answer\(([^,\)]+)\)', # Simple unquoted
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
for pattern in patterns:
|
| 217 |
+
match = re.search(pattern, full_response)
|
| 218 |
+
if match:
|
| 219 |
+
answer = match.group(1).strip()
|
| 220 |
+
print(f"Found answer via final_answer tool: {answer}")
|
| 221 |
+
return answer
|
| 222 |
+
|
| 223 |
+
# Look for explicit final answer markers
|
| 224 |
+
markers = [
|
| 225 |
+
"Out - Final answer:",
|
| 226 |
+
"Final answer:",
|
| 227 |
+
"Answer:",
|
| 228 |
+
"The answer is:"
|
| 229 |
+
]
|
| 230 |
+
|
| 231 |
+
for marker in markers:
|
| 232 |
+
if marker in full_response:
|
| 233 |
+
parts = full_response.split(marker)
|
| 234 |
+
answer = parts[-1].strip()
|
| 235 |
+
# Clean up the answer - remove any following sections
|
| 236 |
+
answer = answer.split("\n")[0].strip()
|
| 237 |
+
print(f"Found answer via marker '{marker}': {answer}")
|
| 238 |
+
return answer
|
| 239 |
+
|
| 240 |
+
# If the raw response is just a simple answer (like a number or short text)
|
| 241 |
+
# and doesn't contain execution logs or other markers, use it directly
|
| 242 |
+
clean_response = full_response.strip()
|
| 243 |
+
if (len(clean_response) < 100 and
|
| 244 |
+
not any(marker in clean_response for marker in [
|
| 245 |
+
'[', '─', '╭', '╰', 'Out:', 'Execution logs:',
|
| 246 |
+
'Code parsing', 'Error:', '```', 'Thought:',
|
| 247 |
+
'Code:', '<end_code>', 'Observation:', 'Step', 'Duration',
|
| 248 |
+
'New run', 'Executing parsed code'
|
| 249 |
+
])):
|
| 250 |
+
print(f"Using raw response as answer: {clean_response}")
|
| 251 |
+
return clean_response
|
| 252 |
+
|
| 253 |
+
# If we get here, we need to try to extract a meaningful answer from the response
|
| 254 |
+
print("No explicit answer format found, analyzing response content...")
|
| 255 |
+
|
| 256 |
+
# Split into lines and look for meaningful content
|
| 257 |
+
lines = full_response.strip().split('\n')
|
| 258 |
+
|
| 259 |
+
# First look for lines that look like direct answers (not prefixed with common markers)
|
| 260 |
+
for line in lines:
|
| 261 |
+
line = line.strip()
|
| 262 |
+
if (line and
|
| 263 |
+
not any(line.startswith(x) for x in [
|
| 264 |
+
'[', '─', '╭', '╰', 'Out:', 'Execution logs:',
|
| 265 |
+
'Code parsing', 'Error:', '```', 'Thought:',
|
| 266 |
+
'Code:', '<end_code>', 'Observation:', 'Step', 'Duration'
|
| 267 |
+
]) and
|
| 268 |
+
not any(line.endswith(x) for x in ['seconds', 'seconds)']) and
|
| 269 |
+
len(line) > 1 and
|
| 270 |
+
not line.startswith('─') and
|
| 271 |
+
not line.startswith('╭') and
|
| 272 |
+
not line.startswith('╰')):
|
| 273 |
+
print(f"Found potential answer from content: {line}")
|
| 274 |
+
return line
|
| 275 |
+
|
| 276 |
+
# If we still haven't found anything, return a clear error
|
| 277 |
+
error_msg = "Could not extract a clear answer from the agent's response"
|
| 278 |
+
print(error_msg)
|
| 279 |
+
return error_msg
|
| 280 |
|
| 281 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 282 |
"""
|