File size: 2,066 Bytes
98bf598
10e9b7d
98bf598
e80aab9
98bf598
 
31243f4
98bf598
31243f4
98bf598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, tool

# --- TOOL DEFINITIONS ---

@tool
def summarize_query(query: str) -> str:
    """
    Provides a structured summary to reframe a query if the search results are unclear or poor.
    """
    return f"Summarize and reframe: {query}"

# Live DuckDuckGo search tool
search_tool = DuckDuckGoSearchTool()

# --- SYSTEM PROMPT WITH RETRY STRATEGY ---

system_prompt = """
You are a ReACT agent with scratchpad memory and a retry mechanism.

For every question:
1. Thought: Think what is needed.
2. Action: (Optional) Use a tool with a clear query.
3. Observation: Record what tool returned.

**If the first Observation seems empty, irrelevant, or very weak:**
4. Thought: "The result was unclear. I should reframe and try again."
5. Action: Use summarize_query to improve the search query.
6. Action: Retry DuckDuckGoSearchTool with the improved query.
7. Observation: Record result.
8. Thought: Reflect carefully across both Observations.
9. FINAL ANSWER: Provide the final answer.

**Final Output Rules:**
- Always start with FINAL ANSWER: [your answer].
- Numbers: plain (no commas unless list).
- Strings: no articles unless inside proper names.
- Lists: comma-separated without extra punctuation.

**Scratchpad Example:**

Thought: Find fruits in Embroidery painting.
Action: DuckDuckGoSearchTool('fruits in Embroidery from Uzbekistan painting')
Observation: (empty result)
Thought: Result was unclear. I should reframe and retry.
Action: summarize_query('fruits in painting Embroidery from Uzbekistan')
Observation: fruits: pomegranate, apple, grape
Thought: Now get the breakfast fruits.
Action: DuckDuckGoSearchTool('breakfast menu October 1949 SS Ile de France')
Observation: grapes, apples, oranges
Thought: Matching fruits are grapes and apples.
FINAL ANSWER: grapes, apples

Be methodical, careful, and retry only once if needed.
"""

# --- BUILD THE SMART AGENT ---

smart_agent = CodeAgent(
    tools=[search_tool, summarize_query],
    model=HfApiModel(system_prompt=system_prompt)
)