smolagents - agent迭代式解题过程示例
对于有些计算问题,大模型首次生成的程序可能不能运行。这时agent作用就得到体现,agent将报错信息作为对话历史反馈给大模型,大模型结合报错信息,有可能生成能运行的程序代码。
示例任务和代码如下:
task: Could you give me the 118th number in the Fibonacci sequence?
import os
os.environ['HF_ENDPOINT'] = "https://hf-mirror.com"from smolagents import CodeAgent, WebSearchTool, InferenceClientModel, MLXModelmodel = MLXModel(model_id="mlx-community/Qwen2.5-Coder-3B-Instruct-4bit")
agent = CodeAgent(tools=[WebSearchTool()], model=model, stream_outputs=False)agent.run("Could you give me the 118th number in the Fibonacci sequence?")
step1: agent将任务拼接到prompt提交给llm
{sys_prompt}
Now Begin!"
role: MessageRole.USER, content = "New task:
Could you give me the 118th number in the Fibonacci sequence?"
llm返回:
Thought: To find the 118th number in the Fibonacci sequence, I will use the `fibonacci_sequence` tool, which generates the Fibonacci sequence up to a specified number of terms.
<code>
fibonacci_sequence(118)
显然fibonacci_sequence(118)是不能实际运行,smolagent报错如下
─ Executing parsed code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────
fibonacci_sequence(118)
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Code execution failed at line 'fibonacci_sequence(118)' due to: InterpreterError: Forbidden function evaluation: 'fibonacci_sequence' is
not among the explicitly allowed tools or defined/imported in the preceding code
[Step 1: Duration 9.10 seconds| Input tokens: 2,006 | Output tokens: 54]
step2: agent拼接报错到prompt,并提醒大模型尝试其他不同方法
{sys_prompt}
Now Begin!"
role: MessageRole.USER, content = "New task:
Could you give me the 118th number in the Fibonacci sequence?"
role: MessageRole.ASSISTANT, content = "Thought: To find the 118th number in the Fibonacci sequence, I will use the `fibonacci_sequence` tool, which generates the Fibonacci sequence up to a specified number of terms.
<code>
fibonacci_sequence(118)
</code>
Calling tools:
[{'id': 'call_1', 'type': 'function', 'function': {'name': 'python_interpreter', 'arguments': 'fibonacci_sequence(118)'}}]"
role: MessageRole.USER, content = "Call id: call_1
Error:
Code execution failed at line 'fibonacci_sequence(118)' due to: InterpreterError: Forbidden function evaluation: 'fibonacci_sequence' is not among the explicitly allowed tools or defined/imported in the preceding code
Now let's retry: take care not to repeat previous errors! If you have retried several times, try a completely different approach.
"
llm返回采用其他方法的程序
Thought: I need to generate the 118th number in the Fibonacci sequence without using the `fibonacci_sequence` tool. I will use Python code to calculate the Fibonacci sequence iteratively.
<code>
a, b = 0, 1
for _ in range(117):
a, b = b, a + b
print(a)
显然这个版本的python代码是可以运行的,以下是运行日志。
─ Executing parsed code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────
a, b = 0, 1
for _ in range(117):
a, b = b, a + b
print(a)
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Execution logs:
1264937032042997393488322Out: None
[Step 2: Duration 9.45 seconds| Input tokens: 4,200 | Output tokens: 133]
step3: agent拼接运行日志到prompt,并提示大模型提取最终答案
{sys_prompt}
Now Begin!"
role: MessageRole.USER, content = "New task:
Could you give me the 118th number in the Fibonacci sequence?"
role: MessageRole.ASSISTANT, content = "Thought: To find the 118th number in the Fibonacci sequence, I will use the `fibonacci_sequence` tool, which generates the Fibonacci sequence up to a specified number of terms.
<code>
fibonacci_sequence(118)
</code>
Calling tools:
[{'id': 'call_1', 'type': 'function', 'function': {'name': 'python_interpreter', 'arguments': 'fibonacci_sequence(118)'}}]"
role: MessageRole.USER, content = "Call id: call_1
Error:
Code execution failed at line 'fibonacci_sequence(118)' due to: InterpreterError: Forbidden function evaluation: 'fibonacci_sequence' is not among the explicitly allowed tools or defined/imported in the preceding code
Now let's retry: take care not to repeat previous errors! If you have retried several times, try a completely different approach.
"
role: MessageRole.ASSISTANT, content = "Thought: I need to generate the 118th number in the Fibonacci sequence without using the `fibonacci_sequence` tool. I will use Python code to calculate the Fibonacci sequence iteratively.
<code>
a, b = 0, 1
for _ in range(117):
a, b = b, a + b
print(a)
</code>
Calling tools:
[{'id': 'call_2', 'type': 'function', 'function': {'name': 'python_interpreter', 'arguments': 'a, b = 0, 1\nfor _ in range(117):\n a, b = b, a + b\nprint(a)'}}]"
role: MessageRole.USER, content = "Observation:
Execution logs:
1264937032042997393488322
Last output from code snippet:
None"
llm返回最终答案。
Thought: The code executed successfully, and the 118th number in the Fibonacci sequence is 1264937032042997393488322.
<code>
final_answer(1264937032042997393488322)
smolagent显示最终答案
─ Executing parsed code: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────
final_answer(1264937032042997393488322)
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Final answer: 1264937032042997393488322
[Step 3: Duration 10.27 seconds| Input tokens: 6,591 | Output tokens: 215]
reference
---
smolagents - 如何在mac用agents做简单算术题
https://blog.csdn.net/liliang199/article/details/149281317
smolagents
https://github.com/huggingface/smolagents