代理
代理会自动创建工作流来响应多方面的用户请求。代理通过迭代地提示和/或与工具交互来逐步执行一个过程,最终得到请求的答案。
代理擅长需要多种工具和/或方法的复杂任务。它们融入了一定程度的随机性,类似于不同的人处理同一任务。当请求简单和/或存在基于规则的过程时,应考虑 RAG 和工作流等其他方法。
以下代码片段定义了一个基本代理。
from datetime import datetime
from txtai import Agent
wikipedia = {
"name": "wikipedia",
"description": "Searches a Wikipedia database",
"provider": "huggingface-hub",
"container": "neuml/txtai-wikipedia"
}
arxiv = {
"name": "arxiv",
"description": "Searches a database of scientific papers",
"provider": "huggingface-hub",
"container": "neuml/txtai-arxiv"
}
def today() -> str:
"""
Gets the current date and time
Returns:
current date and time
"""
return datetime.today().isoformat()
agent = Agent(
model="hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4",
tools=[today, wikipedia, arxiv, "websearch"],
max_steps=10,
)
上面的代理可以访问两个嵌入数据库(Wikipedia 和 ArXiv)以及网络。根据用户的输入请求,代理决定解决任务的最佳工具。
示例
第一个示例将解决涉及多个数据点的问题。参见下方。
agent("Which city has the highest population, Boston or New York?")
这需要在知道如何回答问题之前查找每个城市的人口。需要运行多次搜索请求才能生成最终答案。
代理式 RAG
标准检索增强生成(RAG)运行一次向量搜索以获取上下文,然后使用上下文 + 输入问题构建提示。代理式 RAG 是一个更复杂的过程,需要经过多次迭代。它还可以利用多个数据库来得出最终结论。
下面的示例聚合了来自多个来源的信息,并就某个主题构建报告。
researcher = """
You're an expert researcher looking to write a paper on {topic}.
Search for websites, scientific papers and Wikipedia related to the topic.
Write a report with summaries and references (with hyperlinks).
Write the text as Markdown.
"""
agent(researcher.format(topic="alien life"))
代理团队
代理也可以成为工具。这使得构建“代理团队”来解决问题的概念成为可能。之前的示例可以重写为代理列表。
from txtai import Agent, LLM
llm = LLM("hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4")
websearcher = Agent(
model=llm,
tools=["websearch"],
)
wikiman = Agent(
model=llm,
tools=[{
"name": "wikipedia",
"description": "Searches a Wikipedia database",
"provider": "huggingface-hub",
"container": "neuml/txtai-wikipedia"
}],
)
researcher = Agent(
model=llm,
tools=[{
"name": "arxiv",
"description": "Searches a database of scientific papers",
"provider": "huggingface-hub",
"container": "neuml/txtai-arxiv"
}],
)
agent = Agent(
model=llm,
tools=[{
"name": "websearcher",
"description": "I run web searches, there is no answer a web search can't solve!",
"target": websearcher
}, {
"name": "wikiman",
"description": "Wikipedia has all the answers, I search Wikipedia and answer questions",
"target": wikiman
}, {
"name": "researcher",
"description": "I'm a science guy. I search arXiv to get all my answers.",
"target": researcher
}],
max_steps=10
)
这为过程提供了另一层智能。每个代理与工具的组合都有自己的推理引擎,而不仅仅是单次工具执行。
agent("""
Work with your team and build a comprehensive report on fundamental
concepts about Signal Processing.
Write the output in Markdown.
""")
更多示例
参见下方链接了解更多。
笔记本 | 描述 | |
---|---|---|
txtai 8.0 的新功能 | 使用 txtai 的代理 | |
使用图和代理分析 Hugging Face 帖子 | 使用图分析和代理探索丰富的数据集 | |
赋予代理自主权 | 根据情况迭代解决问题的代理 | |
使用图和代理分析 LinkedIn 公司帖子 | 探索如何使用 AI 提高社交媒体参与度 | |
使用 txtai 解析星体 | 探索已知恒星、行星、星系的知识图谱 |