Detail LLM choice options
This commit is contained in:
parent
e9119c9df5
commit
4a99f8b8d8
|
@ -32,6 +32,9 @@ To initialize a minimal agent, you need at least these two arguments:
|
|||
|
||||
Once you have these two arguments, `tools` and `model`, you can create an agent and run it.
|
||||
|
||||
<hfoptions id="Pick a LLM">
|
||||
<hfoption id="Hugging Face API">
|
||||
|
||||
```python
|
||||
from smolagents import CodeAgent, HfApiModel
|
||||
from huggingface_hub import login
|
||||
|
@ -47,6 +50,54 @@ agent.run(
|
|||
"Could you give me the 118th number in the Fibonacci sequence?",
|
||||
)
|
||||
```
|
||||
</hfoption>
|
||||
<hfoption id="Local Transformers Model">
|
||||
|
||||
```python
|
||||
from smolagents import CodeAgent, TransformersModel
|
||||
|
||||
model_id = "meta-llama/Llama-3.2-3B-Instruct"
|
||||
|
||||
model = TransformersModel(model_id=model_id)
|
||||
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
|
||||
|
||||
agent.run(
|
||||
"Could you give me the 118th number in the Fibonacci sequence?",
|
||||
)
|
||||
```
|
||||
</hfoption>
|
||||
<hfoption id="OpenAI or Anthropic API">
|
||||
|
||||
```python
|
||||
from smolagents import CodeAgent, LiteLLMModel
|
||||
|
||||
model = LiteLLMModel(model_id="anthropic/claude-3-5-sonnet-latest") # Could use 'gpt-4o'
|
||||
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
|
||||
|
||||
agent.run(
|
||||
"Could you give me the 118th number in the Fibonacci sequence?",
|
||||
)
|
||||
```
|
||||
</hfoption>
|
||||
<hfoption id="Ollama">
|
||||
|
||||
```python
|
||||
from smolagents import CodeAgent, LiteLLMModel
|
||||
|
||||
model = LiteLLMModel(
|
||||
model_id="ollama_chat/llama3.2", # This model is a bit weak for agentic behaviours though
|
||||
api_base="http://localhost:11434", # replace with remote open-ai compatible server if necessary
|
||||
api_key="your-api-key" # replace with API key if necessary
|
||||
)
|
||||
|
||||
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
|
||||
|
||||
agent.run(
|
||||
"Could you give me the 118th number in the Fibonacci sequence?",
|
||||
)
|
||||
```
|
||||
</hfoption>
|
||||
</hfoptions>
|
||||
|
||||
#### Code execution
|
||||
|
||||
|
@ -66,82 +117,14 @@ This gives you at the end of the agent run:
|
|||
```text
|
||||
'Hugging Face – Blog'
|
||||
```
|
||||
The execution will stop at any code trying to perform an illegal operation or if there is a regular Python error with the code generated by the agent. You can also use [E2B code executor](https://e2b.dev/docs#what-is-e2-b) instead of a local Python interpreter by first [setting the `E2B_API_KEY` environment variable](https://e2b.dev/dashboard?tab=keys) and then passing `use_e2b_executor=True` upon agent initialization.
|
||||
|
||||
> [!WARNING]
|
||||
> The LLM can generate arbitrary code that will then be executed: do not add any unsafe imports!
|
||||
|
||||
### The system prompt
|
||||
The execution will stop at any code trying to perform an illegal operation or if there is a regular Python error with the code generated by the agent. You can also use [E2B code executor](https://e2b.dev/docs#what-is-e2-b) instead of a local Python interpreter by first [setting the `E2B_API_KEY` environment variable](https://e2b.dev/dashboard?tab=keys) and then passing `use_e2b_executor=True` upon agent initialization.
|
||||
|
||||
Upon initialization of the agent system, a system prompt (attribute `system_prompt`) is built automatically by turning the description extracted from the tools into a predefined system prompt template.
|
||||
|
||||
But you can customize it!
|
||||
|
||||
Let's see how it works. For example, check the system prompt for the [`CodeAgent`] (below version is slightly simplified).
|
||||
|
||||
The prompt and output parser were automatically defined, but you can easily inspect them by calling the `system_prompt_template` on your agent.
|
||||
|
||||
```python
|
||||
print(agent.system_prompt_template)
|
||||
```
|
||||
Here is what you get:
|
||||
```text
|
||||
You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
|
||||
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
|
||||
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
|
||||
|
||||
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
|
||||
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_code>' sequence.
|
||||
During each intermediate step, you can use 'print()' to save whatever important information you will then need.
|
||||
These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
|
||||
In the end you have to return a final answer using the `final_answer` tool.
|
||||
|
||||
Here are a few examples using notional tools:
|
||||
---
|
||||
{examples}
|
||||
|
||||
Above example were using notional tools that might not exist for you. On top of performing computations in the Python code snippets that you create, you only have access to these tools:
|
||||
|
||||
{{tool_descriptions}}
|
||||
|
||||
{{managed_agents_descriptions}}
|
||||
|
||||
Here are the rules you should always follow to solve your task:
|
||||
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
|
||||
2. Use only variables that you have defined!
|
||||
3. Always use the right arguments for the tools. DO NOT pass the arguments as a dict as in 'answer = wiki({'query': "What is the place where James Bond lives?"})', but use the arguments directly as in 'answer = wiki(query="What is the place where James Bond lives?")'.
|
||||
4. Take care to not chain too many sequential tool calls in the same code block, especially when the output format is unpredictable. For instance, a call to search has an unpredictable return format, so do not have another tool call that depends on its output in the same block: rather output results with print() to use them in the next block.
|
||||
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
|
||||
6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.
|
||||
7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.
|
||||
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
|
||||
9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.
|
||||
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
|
||||
|
||||
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
|
||||
```
|
||||
|
||||
The system prompt includes:
|
||||
- An *introduction* that explains how the agent should behave and what tools are.
|
||||
- A description of all the tools that is defined by a `{{tool_descriptions}}` token that is dynamically replaced at runtime with the tools defined/chosen by the user.
|
||||
- The tool description comes from the tool attributes, `name`, `description`, `inputs` and `output_type`, and a simple `jinja2` template that you can refine.
|
||||
- The expected output format.
|
||||
|
||||
You could improve the system prompt, for example, by adding an explanation of the output format.
|
||||
|
||||
For maximum flexibility, you can overwrite the whole system prompt template by passing your custom prompt as an argument to the `system_prompt` parameter.
|
||||
|
||||
```python
|
||||
from smolagents import ToolCallingAgent, PythonInterpreterTool, TOOL_CALLING_SYSTEM_PROMPT
|
||||
|
||||
modified_prompt = TOOL_CALLING_SYSTEM_PROMPT # This is where you can do your modifications
|
||||
|
||||
agent = ToolCallingAgent(tools=[PythonInterpreterTool()], model=model, system_prompt=modified_prompt)
|
||||
```
|
||||
|
||||
> [!WARNING]
|
||||
> Please make sure to define the `{{tool_descriptions}}` string somewhere in the `template` so the agent is aware
|
||||
of the available tools.
|
||||
> [!TIP]
|
||||
> Learn more about code execution [in this tutorial](tutorials/secure_code_execution).
|
||||
|
||||
|
||||
### Inspecting an agent run
|
||||
|
|
|
@ -500,18 +500,18 @@ You have been provided with these additional arguments, that you can access usin
|
|||
Runs the agent in streaming mode, yielding steps as they are executed: should be launched only in the `run` method.
|
||||
"""
|
||||
final_answer = None
|
||||
step = 0
|
||||
while final_answer is None and step < self.max_steps:
|
||||
step_number = 0
|
||||
while final_answer is None and step_number < self.max_steps:
|
||||
step_start_time = time.time()
|
||||
step_log = ActionStep(step=step, start_time=step_start_time)
|
||||
step_log = ActionStep(step=step_number, start_time=step_start_time)
|
||||
try:
|
||||
if (
|
||||
self.planning_interval is not None
|
||||
and step % self.planning_interval == 0
|
||||
and step_number % self.planning_interval == 0
|
||||
):
|
||||
self.planning_step(task, is_first_step=(step == 0), step=step)
|
||||
self.planning_step(task, is_first_step=(step_number == 0), step=step_number)
|
||||
console.print(
|
||||
Rule(f"[bold]Step {step}", characters="━", style=YELLOW_HEX)
|
||||
Rule(f"[bold]Step {step_number}", characters="━", style=YELLOW_HEX)
|
||||
)
|
||||
|
||||
# Run one step!
|
||||
|
@ -524,10 +524,10 @@ You have been provided with these additional arguments, that you can access usin
|
|||
self.logs.append(step_log)
|
||||
for callback in self.step_callbacks:
|
||||
callback(step_log)
|
||||
step += 1
|
||||
step_number += 1
|
||||
yield step_log
|
||||
|
||||
if final_answer is None and step == self.max_steps:
|
||||
if final_answer is None and step_number == self.max_steps:
|
||||
error_message = "Reached max steps."
|
||||
final_step_log = ActionStep(error=AgentMaxStepsError(error_message))
|
||||
self.logs.append(final_step_log)
|
||||
|
@ -547,18 +547,18 @@ You have been provided with these additional arguments, that you can access usin
|
|||
Runs the agent in direct mode, returning outputs only at the end: should be launched only in the `run` method.
|
||||
"""
|
||||
final_answer = None
|
||||
step = 0
|
||||
while final_answer is None and step < self.max_steps:
|
||||
step_number = 0
|
||||
while final_answer is None and step_number < self.max_steps:
|
||||
step_start_time = time.time()
|
||||
step_log = ActionStep(step=step, start_time=step_start_time)
|
||||
step_log = ActionStep(step=step_number, start_time=step_start_time)
|
||||
try:
|
||||
if (
|
||||
self.planning_interval is not None
|
||||
and step % self.planning_interval == 0
|
||||
and step_number % self.planning_interval == 0
|
||||
):
|
||||
self.planning_step(task, is_first_step=(step == 0), step=step)
|
||||
self.planning_step(task, is_first_step=(step_number == 0), step=step_number)
|
||||
console.print(
|
||||
Rule(f"[bold]Step {step}", characters="━", style=YELLOW_HEX)
|
||||
Rule(f"[bold]Step {step_number}", characters="━", style=YELLOW_HEX)
|
||||
)
|
||||
|
||||
# Run one step!
|
||||
|
@ -573,9 +573,9 @@ You have been provided with these additional arguments, that you can access usin
|
|||
self.logs.append(step_log)
|
||||
for callback in self.step_callbacks:
|
||||
callback(step_log)
|
||||
step += 1
|
||||
step_number += 1
|
||||
|
||||
if final_answer is None and step == self.max_steps:
|
||||
if final_answer is None and step_number == self.max_steps:
|
||||
error_message = "Reached max steps."
|
||||
final_step_log = ActionStep(error=AgentMaxStepsError(error_message))
|
||||
self.logs.append(final_step_log)
|
||||
|
|
Loading…
Reference in New Issue