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

mac llama_index agent算术式子计算示例

本文通过简单数学计算,示例llama_index使用agent解决复杂任务过程。

假设mac本地llama_index环境已安装,过程参考

mac测试ollama llamaindex-CSDN博客

测试mac笔记本内存8G,所以使用较小LLM完成示例。

ollama pull qwen3:1.7b

qwen3:1.7b能力较弱,需要prompt明确要求使用agent工具,遵守计算前后顺序。

prompt示例如下

"使用agent工具计算,遵守计算的先后顺序, 20 + (2 x 3) * 4?"

程序示例如下

from llama_index.core.agent import ReActAgent
from llama_index.core.tools import FunctionTool
from llama_index.core import Settings
from llama_index.llms.ollama import OllamaSettings.embed_model = OllamaEmbedding(model_name="yxl/m3e:latest")  
Settings.llm = Ollama(model="qwen3:1.7b", request_timeout=360) # 定义数学计算工具
def multiply(a: float, b: float) -> float:"""Multiply two numbers and returns the product"""return a * bmultiply_tool = FunctionTool.from_defaults(fn=multiply)def add(a: float, b: float) -> float:"""Add two numbers and returns the sum"""return a + badd_tool = FunctionTool.from_defaults(fn=add)# 实例化 ReActAgent
agent = ReActAgent.from_tools([multiply_tool, add_tool], verbose=True
)# 测试代理
response = agent.chat("使用agent工具计算,遵守计算的先后顺序, 20 + (2 x 3) * 4?")
print(response)

如下所示,llama_index将示例式子计算过程分解为agent工具的调用过程。

首先是红色部分,调用multiply完成2*3=6。

然后是棕色部分,调用multiply完成6*4=24。

最后是紫色部分,调用add完成20+24=44。

> Running step 91dcc552-f7b2-4cc3-9af1-cf07812cc8b8. Step input: 请使用agent工具计算,遵守计算的先后顺序, 20 + (2 x 3) * 4?
Thought: <think>
Okay, let's see. The user wants to calculate 20 + (2 x 3) * 4 using the agent tool. Hmm, first I need to break down the expression according to the order of operations. Parentheses come first, so I should calculate 2 x 3 first. That's 6. Then multiply by 4, which gives 24. Now add that to 20. So 20 + 24 equals 44. Wait, but the tools available are multiply and add. Let me check the tools again. The multiply tool takes two numbers, and the add tool adds two numbers. So I need to do the multiplication first. Let me do the steps: first multiply 2 and 3, then multiply the result by 4, then add 20. So the steps would be: multiply 2 and 3 to get 6, then multiply by 4 to get 24, then add 20. So the final answer should be 44. I need to use the tools in the correct order. Let me use the multiply tool first for 2 and 3, then multiply the result by 4, then add 20. Let's see how that works.
</think>

Thought: I need to calculate 20 + (2 x 3) * 4 using the available tools. First, multiply 2 and 3, then multiply the result by 4, and finally add 20.
Action: multiply
Action Input: {"a": 2, "b": 3}
Observation: 6

Thought: Now multiply the result (6) by 4.
Action: multiply
Action Input: {"a": 6, "b": 4}
Observation: 24

Thought: Add 24 to 20.
Action: add
Action Input: {'a': 24, 'b': 20}
Observation: 44

> Running step ab11a064-bdde-42df-9fcf-3aa409e18eab. Step input: None
Thought: I can answer without using any more tools. I'll use the user's language to answer
Answer: 44
44

对于更复杂任务和能力更强的LLM,处理过程是类似的。

更强的LLM能更有效的将复杂问题分解为多个agent调用过程,通过agent协同解决复杂问题。

reference

---

mac测试ollama llamaindex

https://blog.csdn.net/liliang199/article/details/149542926

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

相关文章:

  • AUTOSAR进阶图解==>AUTOSAR_SWS_BSWGeneral
  • [202103][Docker 实战][第2版][耿苏宁][译]
  • Vue3实现视频播放弹窗组件,支持全屏播放,音量控制,进度条自定义样式,适配浏览器小窗播放,视频大小自适配,缓冲loading,代码复制即用
  • 机器学习入门与经典knn算法表文解析
  • USRP X440
  • C++抽象类完全指南
  • 加密算法-----BCrypt
  • 负载均衡-LoadBalance
  • 【数组的定义与使用】
  • 排序查找算法,Map集合,集合的嵌套,Collections工具类
  • 【独立工具】小红书图片采集软件
  • pytest官方Tutorial所有示例详解(二)
  • Python循环结构
  • 【数据结构】二叉树进阶算法题
  • AI服务器给一体成型电感带来多大的市场空间
  • C++开发---奥比中光(Orbbec)的ob库
  • 【CMake】CMake 常用语法总结
  • 到底可不可以用jion?jion如何优化?
  • 力扣-152.乘积最大子数组
  • 自己开发VUE之web网页打包技术毕业论文——仙盟创梦IDE
  • 【科研绘图系列】R语言绘制双侧坐标轴的条形图
  • NPM/Yarn完全指南:前端开发的“基石“与“加速器“
  • 第4章唯一ID生成器——4.1 分布式唯一ID
  • 电脑录屏软件推荐:如何使用oCam录制游戏、教程视频
  • ue4 优化
  • 完成多项问题修复,MaxKB开源企业级智能体平台v1.10.9 LTS版本发布
  • Webpack/Vite 终极指南:前端开发的“涡轮增压引擎“
  • 侯捷---c++面向对象高级编程
  • 制造工厂高效出图新技术——共享云桌面
  • JavaSE:学习输入输出编写简单的程序