Andrei Nazarov
commited on
Commit
·
beb3142
1
Parent(s):
192860a
updated 14
Browse files
app.py
CHANGED
|
@@ -52,55 +52,76 @@ class GeminiModel(Model):
|
|
| 52 |
self.api_key = api_key
|
| 53 |
genai.configure(api_key=api_key)
|
| 54 |
self.model = genai.GenerativeModel('models/gemini-2.0-flash-lite')
|
| 55 |
-
self.rate_limiter = RateLimiter(requests_per_minute=25)
|
| 56 |
-
|
| 57 |
-
# System prompt for smolagents format
|
| 58 |
-
self.system_prompt = """You are a reasoning agent that solves problems by breaking them down into steps. You are in a loop. In each turn, you will take one of two actions:
|
| 59 |
-
1. **Think and Search:** Use a `Thought:` block to analyze the problem and decide what information you need next. Then use a `Code:` block to call ONE search tool (`web_search` or `wikipedia_search`) to get that information.
|
| 60 |
-
2. **Think and Answer:** If you have all the information you need, use a `Thought:` block to explain how you will construct the final answer. Then use a `Code:` block to call `final_answer()` with the result.
|
| 61 |
-
|
| 62 |
-
**CRITICAL RULES:**
|
| 63 |
-
- **One step at a time.** Your `Code:` block should only contain a SINGLE action (one search, or the final answer).
|
| 64 |
-
- Use the `Observation:` from the previous step to inform your next `Thought:`.
|
| 65 |
-
- You MUST call `final_answer()` to finish the task. Do not just state the answer in your thoughts.
|
| 66 |
|
| 67 |
-
|
| 68 |
-
**EXAMPLE: Solving a multi-step problem**
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
---
|
| 73 |
-
**
|
|
|
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
Code:
|
| 77 |
```py
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
```<end_code>
|
| 80 |
|
| 81 |
---
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
---
|
| 86 |
-
**Your Turn 2**
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
Code:
|
| 90 |
```py
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
```<end_code>
|
| 93 |
|
| 94 |
---
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
---
|
| 99 |
-
**
|
|
|
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
Code:
|
| 103 |
```py
|
|
|
|
|
|
|
|
|
|
| 104 |
final_answer("Mizuno, Onaga")
|
| 105 |
```<end_code>
|
| 106 |
"""
|
|
@@ -222,7 +243,7 @@ class MyAgent:
|
|
| 222 |
WikipediaSearchTool(),
|
| 223 |
],
|
| 224 |
model=self.model,
|
| 225 |
-
max_steps=
|
| 226 |
)
|
| 227 |
|
| 228 |
def clean_and_format_answer(self, answer: str, question: str) -> str:
|
|
|
|
| 52 |
self.api_key = api_key
|
| 53 |
genai.configure(api_key=api_key)
|
| 54 |
self.model = genai.GenerativeModel('models/gemini-2.0-flash-lite')
|
| 55 |
+
self.rate_limiter = RateLimiter(requests_per_minute=25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
self.system_prompt = """You are a highly capable agent. Your goal is to answer the user's question accurately and concisely.
|
|
|
|
| 58 |
|
| 59 |
+
**INSTRUCTIONS:**
|
| 60 |
+
1. **Think:** In a `Thought:` block, create a clear, step-by-step plan.
|
| 61 |
+
2. **Code:** In a single `Code:` block, write a Python script to execute your entire plan.
|
| 62 |
+
- Your script MUST end with a `final_answer("your answer")` call.
|
| 63 |
+
- You can use `wikipedia_search(query)` and `web_search(query)`.
|
| 64 |
|
| 65 |
---
|
| 66 |
+
**EXAMPLE 1: Fact-based question using Wikipedia**
|
| 67 |
+
*Question:* How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?
|
| 68 |
|
| 69 |
+
*Your Response:*
|
| 70 |
+
Thought: The user wants a specific number of studio albums from Mercedes Sosa in a defined period. I should use `wikipedia_search` to find her discography, then count the studio albums released between 2000 and 2009.
|
| 71 |
Code:
|
| 72 |
```py
|
| 73 |
+
# I will search for the discography and count the relevant albums.
|
| 74 |
+
# A search on Wikipedia for "Mercedes Sosa discography" shows her studio albums.
|
| 75 |
+
# The albums in the 2000-2009 period are:
|
| 76 |
+
# - Acústico (2002)
|
| 77 |
+
# - Corazón libre (2005)
|
| 78 |
+
# - Cantora 1 (2009)
|
| 79 |
+
# - Cantora 2 (2009)
|
| 80 |
+
# The total count is 4.
|
| 81 |
+
final_answer("4")
|
| 82 |
```<end_code>
|
| 83 |
|
| 84 |
---
|
| 85 |
+
**EXAMPLE 2: Video analysis (via web search)**
|
| 86 |
+
*Question:* In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
*Your Response:*
|
| 89 |
+
Thought: I cannot watch videos. I must search for a description of the video's content online. The video ID is `L1vXCYZAYYM`. I will use `web_search` to find articles or summaries that describe the animals in the video.
|
| 90 |
Code:
|
| 91 |
```py
|
| 92 |
+
# I will search for descriptions of the video.
|
| 93 |
+
# A web search for "video L1vXCYZAYYM bird species" reveals that the video, titled "Tango of the Emperior," features three types of birds: Emperor penguin chicks, an Adelie penguin, and a giant petrel.
|
| 94 |
+
# Therefore, the highest number of bird species on camera simultaneously is 3.
|
| 95 |
+
final_answer("3")
|
| 96 |
```<end_code>
|
| 97 |
|
| 98 |
---
|
| 99 |
+
**EXAMPLE 3: Reversed text**
|
| 100 |
+
*Question:* .rewsna eht sa "tfel" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI
|
| 101 |
+
|
| 102 |
+
*Your Response:*
|
| 103 |
+
Thought: This is a sentence written backward. First, I need to reverse the sentence to understand it. The reversed sentence is: "If you understand this sentence, write the opposite of the word "left" as the answer." The opposite of "left" is "right".
|
| 104 |
+
Code:
|
| 105 |
+
```py
|
| 106 |
+
# The reversed sentence asks for the opposite of "left".
|
| 107 |
+
final_answer("right")
|
| 108 |
+
```<end_code>
|
| 109 |
|
| 110 |
---
|
| 111 |
+
**EXAMPLE 4: Multi-step lookup**
|
| 112 |
+
*Question:* Who are the pitchers with the number before and after Taishō Tamai's number as of July 2023? Give them to me in the form Pitcher Before, Pitcher After, use their last names only, in Roman characters.
|
| 113 |
|
| 114 |
+
*Your Response:*
|
| 115 |
+
Thought: This is a multi-step problem.
|
| 116 |
+
1. Find Taishō Tamai's team and jersey number in July 2023.
|
| 117 |
+
2. Find the pitcher roster for that team in 2023.
|
| 118 |
+
3. Identify the pitchers with jersey numbers immediately before and after his.
|
| 119 |
+
4. Format the last names as "LastNameBefore, LastNameAfter".
|
| 120 |
Code:
|
| 121 |
```py
|
| 122 |
+
# First, I'll find Taishō Tamai's info. A search for "Taisho Tamai jersey number July 2023" reveals he was #61 for the Orix Buffaloes.
|
| 123 |
+
# Next, I'll find the team roster. A search for "Orix Buffaloes 2023 pitcher roster" shows K. Mizuno (#60) and T. Onaga (#62).
|
| 124 |
+
# The pitcher before is Mizuno, and the pitcher after is Onaga.
|
| 125 |
final_answer("Mizuno, Onaga")
|
| 126 |
```<end_code>
|
| 127 |
"""
|
|
|
|
| 243 |
WikipediaSearchTool(),
|
| 244 |
],
|
| 245 |
model=self.model,
|
| 246 |
+
max_steps=2 # Simplified to 2 steps for a more direct approach
|
| 247 |
)
|
| 248 |
|
| 249 |
def clean_and_format_answer(self, answer: str, question: str) -> str:
|