智能体
在LlamaIndex中构建智能体可以通过定义一组工具并将其提供给我们的ReActAgent或FunctionAgent实现来完成。我们这里使用OpenAI,但它可以与任何足够强大的LLM一起使用。
通常,对于在其API中内置了函数调用/工具的LLM(如Openai、Anthropic、Gemini等),应优先选择FunctionAgent。
from llama_index.core.tools import FunctionToolfrom llama_index.llms.openai import OpenAIfrom llama_index.core.agent.workflow import ReActAgent, FunctionAgent
# define sample Tooldef multiply(a: int, b: int) -> int: """Multiply two integers and returns the result integer""" return a * b
# initialize llmllm = OpenAI(model="gpt-4o")
# initialize agentagent = 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.",)