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 Ollama

Settings.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 * b

multiply_tool = FunctionTool.from_defaults(fn=multiply)

def add(a: float, b: float) -> float:
    """Add two numbers and returns the sum"""
    return a + b

add_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

相关推荐
Bdygsl几秒前
数字图像处理总结 Day 2 —— 数字化
图像处理·人工智能·计算机视觉
LDG_AGI5 分钟前
【推荐系统】深度学习训练框架(九):推荐系统与LLM在Dataset、Tokenizer阶段的异同
人工智能·深度学习·算法·机器学习·推荐算法
智谱开放平台6 分钟前
让 AI 真正懂仓库:如何用 CLAUDE.md 将 Claude Code 的工作效率发挥到极致
人工智能·claude
糯米酒7 分钟前
不想使用docker部署n8n的看过来,你可以这样做
人工智能
roman_日积跬步-终至千里9 分钟前
【模式识别与机器学习(17)】聚类分析教程【2】:高级方法与离群点分析
人工智能·机器学习·支持向量机
后台开发者Ethan10 分钟前
py文件被初始化执行了2次
python
小殊小殊10 分钟前
重磅!DeepSeek发布V3.2系列模型!
论文阅读·人工智能·算法
a31582380610 分钟前
Linux部署Python Django工程和Node工程,使用宝塔面板
linux·服务器·python·django·node·strapi·宝塔面板
B站计算机毕业设计之家12 分钟前
机器学习:python智能电商推荐平台 大数据 spark(Django后端+Vue3前端+协同过滤 毕业设计/实战 源码)✅
大数据·python·spark·django·推荐算法·电商
丝斯201115 分钟前
AI学习笔记整理(19)—— AI核心技术(深度学习3)
人工智能·笔记·学习