AI Personal Learning
and practical guidance
Beanbag Marscode1

Implementing a Local Agent with LlamaIndex

summary

This document describes how to use the LlamaIndex in the ReActAgent combining Ollama Implement a simple local Agent.

The LLM used in this document is the qwen2:0.5b model. Since different models have different ability to call tools, you can try to implement Agent with different models.

Note: This document contains core code snippets and detailed explanations. The full code can be found at notebook The

 

1. Importing dependencies

from llama_index.core.tools.import FunctionTool
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import FunctionTool

 

2. Initialize the Agent tool

# 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
fn=multiply, name="MultiplyTool", description="A tool that multiplies two floats.", return_direct=True
return_direct=True
)

 

3. Initialize LLM and 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. Conducting dialogues

  • Use the LLM dialog directly.
# direct response
res_llm = llm.complete("What is 2.3 × 4.8 ? Calculate step by step")
print(res_llm)

Output results:

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
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 denominator of \(4.8\).
3. **Calculate the product** Since there are no common factors between the product and its value, it is not possible to determine the result.
Since there are no common factors between the numerator and the denominator, the calculation is: **Calculate the product**.
**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
times 2.3 \times]
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 calls the Agent dialog.
# use agent
response = agent.chat("What is 2.3 × 4.8 ? Calculate step by step")
response.response

Output results:

> 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

 


Reference: https://docs.llamaindex.ai/en/stable/examples/agent/react_agent/

CDN1
May not be reproduced without permission:Chief AI Sharing Circle " Implementing a Local Agent with LlamaIndex

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish