入门教程(使用 OpenAI)#
本教程将向您展示如何开始使用 LlamaIndex 构建代理。我们将从一个基本示例开始,然后展示如何添加 RAG(检索增强生成)功能。
提示
请确保您已首先按照安装步骤操作。
提示
想要使用本地模型?如果您想仅使用本地模型完成入门教程,请查看此教程。
设置您的 OpenAI API 密钥#
LlamaIndex 默认使用 OpenAI 的 gpt-3.5-turbo
。请将 API 密钥设置为环境变量,确保您的代码可以使用它。
# MacOS/Linux
export OPENAI_API_KEY=XXXXX
# Windows
set OPENAI_API_KEY=XXXXX
提示
如果您使用与 OpenAI 兼容的 API,可以使用 OpenAILike
LLM 类。您可以在 OpenAILike LLM 集成和 OpenAILike 嵌入集成中找到更多信息。
基本代理示例#
让我们从一个简单示例开始,该示例使用一个可以通过调用工具执行基本乘法运算的代理。创建一个名为 starter.py
的文件。
import asyncio
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
# Define a simple calculator tool
def multiply(a: float, b: float) -> float:
"""Useful for multiplying two numbers."""
return a * b
# Create an agent workflow with our calculator tool
agent = FunctionAgent(
tools=[multiply],
llm=OpenAI(model="gpt-4o-mini"),
system_prompt="You are a helpful assistant that can multiply two numbers.",
)
async def main():
# Run the agent
response = await agent.run("What is 1234 * 4567?")
print(str(response))
# Run the agent
if __name__ == "__main__":
asyncio.run(main())
输出将类似于:The result of \( 1234 \times 4567 \) is \( 5,678,678 \).
发生了什么:
- 代理被提问:
What is 1234 * 4567?
- 在底层,此问题以及工具的 schema(名称、文档字符串和参数)被传递给 LLM。
- 代理选择了
multiply
工具并写入了工具的参数。 - 代理接收到工具的结果并将其插入到最终响应中。
提示
如您所见,我们使用了 async
Python 函数。许多 LLM 和模型支持异步调用,推荐使用异步代码来提高应用程序的性能。要了解更多关于异步代码和 Python 的信息,我们推荐阅读关于 async + Python 的简短章节。
添加聊天历史记录#
AgentWorkflow
也能记住之前的消息。这包含在 AgentWorkflow
的 Context
中。
如果传入了 Context
,代理将使用它来继续对话。
from llama_index.core.workflow import Context
# create context
ctx = Context(agent)
# run agent with context
response = await agent.run("My name is Logan", ctx=ctx)
response = await agent.run("What is my name?", ctx=ctx)
添加 RAG 功能#
现在,让我们通过添加搜索文档的功能来增强我们的代理。首先,让我们使用终端获取一些示例数据。
mkdir data
wget https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt -O data/paul_graham_essay.txt
您的目录结构现在应该像这样:
├── starter.py └── data └── paul_graham_essay.txt
现在我们可以使用 LlamaIndex 创建一个搜索文档的工具。默认情况下,我们的 VectorStoreIndex
将使用 OpenAI 的 text-embedding-ada-002
嵌入来嵌入和检索文本。
修改后的 starter.py
应该如下所示:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
import asyncio
import os
# Create a RAG tool using LlamaIndex
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
def multiply(a: float, b: float) -> float:
"""Useful for multiplying two numbers."""
return a * b
async def search_documents(query: str) -> str:
"""Useful for answering natural language questions about an personal essay written by Paul Graham."""
response = await query_engine.aquery(query)
return str(response)
# Create an enhanced workflow with both tools
agent = FunctionAgent(
tools=[multiply, search_documents],
llm=OpenAI(model="gpt-4o-mini"),
system_prompt="""You are a helpful assistant that can perform calculations
and search through documents to answer questions.""",
)
# Now we can ask questions about the documents or do calculations
async def main():
response = await agent.run(
"What did the author do in college? Also, what's 7 * 8?"
)
print(response)
# Run the agent
if __name__ == "__main__":
asyncio.run(main())
代理现在可以在使用计算器和搜索文档之间无缝切换来回答问题。
存储 RAG 索引#
为了避免每次都重新处理文档,您可以将索引持久化到磁盘。
# Save the index
index.storage_context.persist("storage")
# Later, load the index
from llama_index.core import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir="storage")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
接下来是什么?#
这仅仅是使用 LlamaIndex 代理能做的事情的开始!您可以:
- 为您的代理添加更多工具
- 使用不同的 LLM
- 使用系统提示自定义代理的行为
- 添加流处理功能
- 实现人工参与的工作流
- 使用多个代理协同完成任务
一些有用的后续链接: