AI个人学习
和实操指南
豆包Marscode1

使用 Ollama+LlamaIndex 实现本地 Agent

本文于 2025-03-14 23:35 更新,部分内容具有时效性,如有失效,请留言

简介

本文档介绍了如何使用 LlamaIndex 中的 ReActAgent 结合 Ollama 实现一个简单的本地 Agent。

本文档中使用的 LLM 为 qwen2:0.5b 模型,由于不同模型调用 tools 的能力不同,可以自行尝试使用不同的模型实现 Agent。

注: 本文档包含核心代码片段和详细解释。完整代码可见 notebook 。

 

1. 导入依赖

from llama_index.core.tools import FunctionTool
from llama_index.core.agent import ReActAgent
from llama_index.llms.ollama import Ollama

 

2. 初始化 Agent 工具

# Define tools
def multiply(a: float, b: float) -> float:
"""Multiply two integers and return the result integer"""
return a * b
# Create FunctionTool instances
multiply_tool = FunctionTool.from_defaults(
fn=multiply,
name="MultiplyTool",
description="A tool that multiplies two floats.",
return_direct=True
)

 

3. 初始化 LLM 和 Agent

# Initialize LLM
llm = Ollama(model="qwen2:0.5b", request_timeout=360.0)
# Initialize ReAct agent with tools
agent = ReActAgent.from_tools([multiply_tool], llm=llm, verbose=True)

 

4. 进行对话

  • 直接使用 LLM 对话。
# direct response
res_llm = llm.complete("What is 2.3 × 4.8 ? Calculate step by step")
print(res_llm)

输出结果:

To calculate \( 2.3 \times 4.8 \), you can follow these steps:
1. **Perform the multiplication:** When multiplying decimals, simply multiply the numerators (the top numbers) to get the numerator of the product.
\[
2.3 \times 4.8 = 9.44
\]
2. **Multiply the denominators (bottom numbers)**
The denominator of \(4.8\) is not affected by the multiplication because it does not contain a factor that can affect its value or determine the result.
3. **Calculate the product**  
Since there are no common factors between the numerator and the denominator, the calculation is:
\[
9.44 = 2.3 \times 2.3
\]
This multiplication does not give you a new number because \(2.3\) and \(2.3\) are already multiplied to get 5.6.
So, \(2.3 \times 4.8 = 9.44\).
  • LLM 调用 Agent 对话。
# use agent
response = agent.chat("What is 2.3 × 4.8 ? Calculate step by step")
response.response

输出结果:

> Running step 9227846e-d630-4ce2-a760-c8e90366dc6c. Step input: What is 2.3 × 4.8 ? Calculate step by step
Thought: The task is asking to multiply two numbers, 2.3 and 4.8, then to calculate this multiplication step by step.
Action: MultiplyTool
Action Input: {'a': 2.3, 'b': 4.8}
Observation: 11.04

 


参考资料:https://docs.llamaindex.ai/en/stable/examples/agent/react_agent/

CDN1
未经允许不得转载:首席AI分享圈 » 使用 Ollama+LlamaIndex 实现本地 Agent

首席AI分享圈

首席AI分享圈专注于人工智能学习,提供全面的AI学习内容、AI工具和实操指导。我们的目标是通过高质量的内容和实践经验分享,帮助用户掌握AI技术,一起挖掘AI的无限潜能。无论您是AI初学者还是资深专家,这里都是您获取知识、提升技能、实现创新的理想之地。

联系我们
zh_CN简体中文