Any efficient system using AI will need to provide LLMs some kind of access to the real world: for instance the possibility to call a search tool to get external information, or to act on certain programs in order to solve a task. In other words, LLMs should have agency. Agentic programs are the gateway to the outside world for LLMs.
AI Agents are programs where LLM outputs control the workflow.
This Python code demonstrates the use of a CodeAgent from the smolagents library to solve a problem (finding the 20th Fibonacci number) using a language model and a web search tool. Here's a step-by-step explanation:
-
Imports and Setup:
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel import os from dotenv import load_dotenv load_dotenv()
- The code imports necessary classes from the
smolagentslibrary:CodeAgent(an agent that can execute tasks),DuckDuckGoSearchTool(a tool for web searches), andHfApiModel(a model wrapper for Hugging Face API). osanddotenvare used to load environment variables (e.g., API keys) from a.envfile usingload_dotenv(). This is typically for securely accessing credentials like a Hugging Face API token.
- The code imports necessary classes from the
-
Initialize the Model:
model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct")
- A Hugging Face API model (
Qwen/Qwen2.5-72B-Instruct, a 72-billion-parameter instruction-tuned model) is initialized. This model will power the agent's reasoning and code generation.
- A Hugging Face API model (
-
Initialize the Web Search Tool:
search_tool = DuckDuckGoSearchTool()
- A
DuckDuckGoSearchToolis created, allowing the agent to perform web searches using DuckDuckGo if needed to solve the task.
- A
-
Create the CodeAgent:
agent = CodeAgent( tools=[search_tool], model=model, max_steps=10, verbosity_level=1 )
- A
CodeAgentis instantiated with:tools: A list containing theDuckDuckGoSearchToolfor web searches.model: The Hugging Face model for reasoning and task execution.max_steps=10: Limits the agent to 10 steps to prevent infinite loops or excessive processing.verbosity_level=1: Enables step-by-step logging so you can see the agent's reasoning process.
- A
-
Define the Task:
task = "What is the 20th Fibonacci number?"
- The task is defined as a string: finding the 20th number in the Fibonacci sequence (where each number is the sum of the two preceding ones, starting with 0 and 1).
-
Run the Agent:
result = agent.run(task)
- The
CodeAgentprocesses the task. It may:- Use the model to reason about the Fibonacci sequence.
- Generate code to compute the 20th Fibonacci number.
- Optionally use the search tool if it needs external information (though for this task, it likely won't, as Fibonacci is a well-defined mathematical problem).
- The agent iterates up to
max_stepsto arrive at a solution, logging its steps due toverbosity_level=1.
- The
-
Print the Result:
print(f"Final Answer: {result}")
- The final result (the 20th Fibonacci number) is printed. For reference, the Fibonacci sequence starts as 0, 1, 1, 2, 3, 5, 8, ..., and the 20th number is 6765.
- The
CodeAgentuses the Qwen model to interpret the task and decide how to solve it. It might generate a Python function to compute the Fibonacci number (e.g., using recursion, iteration, or dynamic programming). - If the agent lacks information or needs clarification, it could use the
DuckDuckGoSearchToolto search the web, but for a straightforward mathematical task like this, it likely relies on the model's internal knowledge. - The
max_stepsandverbosity_levelensure the process is controlled and transparent.
Assuming the agent correctly computes the 20th Fibonacci number, the output would be:
Final Answer: 6765
You might also see intermediate logs (due to verbosity_level=1) showing the agent's reasoning, such as the code it generates or steps it takes.
- The code assumes a
.envfile with a Hugging Face API key (e.g.,HF_API_TOKEN=your_token) for theHfApiModel. - The Qwen model is powerful but requires significant computational resources or API access.
- The
DuckDuckGoSearchToolis included but likely unused for this specific task, as Fibonacci is a self-contained problem.