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) )