当前位置: 首页 > web >正文

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:
1264937032042997393488322

Out: 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

http://www.xdnf.cn/news/15694.html

相关文章:

  • Ollama使用指南-更改默认安装路径和Model路径(安装到非C盘)
  • 【46】MFC入门到精通——MFC显示实时时间,获取系统当前时间GetCurrentTime()、获取本地时间GetLocalTime()
  • HTML 极简个人介绍卡片(侧重语义化标签和响应式布局)
  • HCIE - 云计算方向考什么?一文全解
  • LIN通信驱动代码开发注意事项
  • 木马和Webshell和内存马的区别!
  • 电商行业如何做好网络安全工作?
  • 如何选择合规的上门按摩系统
  • Java中excel字典转换
  • SWD和JTAG区别
  • Kotlin 属性委托 observable 的实现原理
  • 快慢指针的应用
  • CPP学习之list使用及模拟实现
  • Java程序设计学习笔记
  • 玖[9],相机/镜头/光源
  • Spring Boot 监控:AOP vs Filter vs Java Agent
  • Cadence SPB 2024软件下载及安装教程|Cadence软件安装详细步骤附下载链接
  • java如何实现打印list对象占用多大内存
  • Web开发 01
  • 【Java开发日记】详细地讲解一下如何保证线程安全性呢?
  • vue svg实现一个环形进度条组件
  • VSCODE常规设置
  • 代码随想录算法训练营65期第22天
  • 【专题十二】栈
  • 从现场出发:能源系统中的智能设备与实际落地工具解读
  • 【Java开发日记】我们来说说 LockSupport 的 park 和 unpark
  • docker--安装--原理
  • RabbitMQ概述和工作模式
  • 60个功能OfficeBox 万彩办公大师:PDF 格式转换 OCR识别免费无广告
  • mac电脑无法阅读runc源码