跳转到内容

智能体

在LlamaIndex中构建智能体可以通过定义一组工具并将其提供给我们的ReActAgent或FunctionAgent实现来完成。我们这里使用OpenAI,但它可以与任何足够强大的LLM一起使用。

通常,对于在其API中内置了函数调用/工具的LLM(如Openai、Anthropic、Gemini等),应优先选择FunctionAgent。

from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import ReActAgent, FunctionAgent
# define sample Tool
def multiply(a: int, b: int) -> int:
"""Multiply two integers and returns the result integer"""
return a * b
# initialize llm
llm = OpenAI(model="gpt-4o")
# initialize agent
agent = FunctionAgent(
tools=[multiply],
system_prompt="You are an agent that can invoke a tool for multiplication when assisting a user.",
)

这些工具可以是如上所示的Python函数,也可以是LlamaIndex查询引擎:

from llama_index.core.tools import QueryEngineTool
query_engine_tools = [
QueryEngineTool.from_defaults(
query_engine=sql_agent,
name="sql_agent",
description="Agent that can execute SQL queries.",
),
]
agent = FunctionAgent(
tools=query_engine_tools,
system_prompt="You are an agent that can invoke an agent for text-to-SQL execution.",
)

您可以在我们的智能体模块指南端到端智能体教程中了解更多信息。