Agente local con Ollama+LlamaIndex

breve

Este documento describe cómo utilizar el LlamaIndex en el ReActAgent combinando Ollama Implantar un Agente local sencillo.

El LLM utilizado en este documento es el modelo qwen2:0.5b. Dado que diferentes modelos tienen diferente capacidad para llamar a herramientas, puede intentar implementar Agente con diferentes modelos.

Nota: Este documento contiene fragmentos de código básico y explicaciones detalladas. El código completo se encuentra en cuaderno .

 

1. Importación de dependencias

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

 

2. Inicializar la herramienta Agente

# 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. Inicializar LLM y Agente

# 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. Conducción del diálogo

  • Utiliza directamente el diálogo LLM.
# direct response
res_llm = llm.complete("What is 2.3 × 4.8 ? Calculate step by step")
print(res_llm)

Resultados:

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 llama al diálogo Agente.
# use agent
response = agent.chat("What is 2.3 × 4.8 ? Calculate step by step")
response.response

Resultados:

> 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

 

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

© declaración de copyright

Artículos relacionados

Sin comentarios

Debe iniciar sesión para participar en los comentarios.
Acceder ahora
ninguno
Sin comentarios...