本文通過簡單數學計算,示例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: 6Thought: Now multiply the result (6) by 4.
Action: multiply
Action Input: {"a": 6, "b": 4}
Observation: 24Thought: 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