Harness 与 Agent 工程 · Agent架构与协作 · 2025-09-27

LlamaIndex 开发智能体 Agents 要点解析

深入解析LlamaIndex框架如何构建高效智能代理系统,掌握核心组件与工具集成技巧。

核心内容:

  1. LlamaIndex框架的核心组件与智能代理特性
  2. 各类工具(检索/函数/API工具)的功能与实现方式
  3. 工作流设计与检索工具集成实战案例

一、核心概念解析

1.1 LlamaIndex框架概述

LlamaIndex是一个强大的数据框架,专为构建基于大型语言模型(LLM)的应用程序而设计。它提供了从数据摄取、索引构建到查询执行的全套工具,特别适合构建智能代理系统。

核心组件包括:

1.2 智能代理(Agent)核心概念

智能代理是能够理解目标、规划步骤并使用工具执行任务的系统。在LlamaIndex中,智能代理具有以下特点:

LlamaIndex提供了多种代理类型,如FunctionAgent、ReActAgent等,每种类型适用于不同的场景。

1.3 工具(Tools)概念

工具是智能代理执行特定任务的能力扩展。LlamaIndex支持多种类型的工具:

1.4 工作流(Workflow)概念

工作流是定义多个步骤执行顺序和条件的框架。在LlamaIndex中,工作流由以下元素组成:

二、工具集成

2.1 检索工具

检索工具允许代理从知识库中获取信息:

from llama_index.core.tools import RetrieverTooldef create_retriever_tool(index: VectorStoreIndex, name: str, description: str): """创建检索工具""" # 创建检索器 retriever = index.as_retriever(similarity_top_k=3) # 创建检索工具 retriever_tool = RetrieverTool.from_defaults( retriever=retriever, name=name, description=description ) return retriever_tool

2.2 函数工具

函数工具允许代理执行特定计算或操作:

from llama_index.core.tools import FunctionTool# 定义计算函数def multiply(a: float, b: float) -> float: """两个数相乘""" return a * bdef add(a: float, b: float) -> float: """两个数相加""" return a + bdef get_weather(city: str) -> str: """获取指定城市的天气信息(模拟)""" # 这里可以接入真实的天气API weather_data = { "北京": "晴天,温度25°C", "上海": "多云,温度28°C", "广州": "雨天,温度30°C" } return weather_data.get(city, f"抱歉,没有{city}的天气信息")# 创建函数工具multiply_tool = FunctionTool.from_defaults(fn=multiply)add_tool = FunctionTool.from_defaults(fn=add)weather_tool = FunctionTool.from_defaults(fn=get_weather)

2.3 API工具

API工具允许代理与外部服务交互:

import requestsfrom llama_index.core.tools import FunctionTooldef search_web(query: str) -> str: """使用网络搜索API搜索信息(模拟)""" # 这里可以接入真实的搜索API,如Google Search API、Bing Search API等 # 以下是模拟实现 return f"关于'{query}'的搜索结果:这是一个模拟的搜索结果。"# 创建API工具search_tool = FunctionTool.from_defaults(fn=search_web)

三、智能代理创建

导入一些包,可自行删减。

pip install llama-index-corepip install llama-index-llms-openaipip install llama-index-embeddings-huggingfacepip install llama-index-vector-stores-chromapip install chromadb

3.1 基础智能代理

现在,我们可以创建一个基础的智能代理:

from llama_index.core.agent.workflow import FunctionAgentdef create_basic_agent(tools, system_prompt=None): """创建基础智能代理""" if system_prompt is None: system_prompt = "你是一个有用的助手,可以使用提供的工具来回答问题和执行任务。" agent = FunctionAgent( tools=tools, llm=Settings.llm, system_prompt=system_prompt, verbose=True # 启用详细输出,便于调试 ) return agent

3.2 高级智能代理

我们可以创建一个更高级的智能代理,结合检索和计算能力:

def create_advanced_agent(index: VectorStoreIndex): """创建高级智能代理""" # 创建工具 retriever_tool = create_retriever_tool( index, name="knowledge_retriever", description="用于从知识库中检索相关信息" ) # 添加函数工具 tools = [ retriever_tool, multiply_tool, add_tool, weather_tool, search_tool ] # 创建系统提示 system_prompt = """ 你是一个智能助手,可以使用多种工具来回答问题和执行任务。 当用户提问时,你应该: 1. 首先尝试使用knowledge_retriever工具从知识库中查找相关信息 2. 如果需要计算,使用multiply或add工具 3. 如果需要天气信息,使用get_weather工具 4. 如果需要网络搜索,使用search_web工具 请根据用户需求选择合适的工具,并综合所有信息给出准确、有用的回答。 """ # 创建代理 agent = FunctionAgent( tools=tools, llm=Settings.llm, system_prompt=system_prompt, verbose=True ) return agent

3.3 代理与工作流结合

我们可以将代理集成到工作流中,创建更复杂的系统:

class AgentEvent(Event): response: strclass AgentWorkflow(Workflow): def __init__(self, agent, **kwargs): super().__init__(**kwargs) s…
— 以上为试看内容(约 40%),开通会员后阅读全文 —

原文出处:https://www.53ai.com/news/llamaindex/2025092784731.html