配置
一个代理需要两个主要参数:一个 LLM 和一个工具列表。
txtai 代理框架是基于 smolagents 构建的。可以在 Agent
构造函数中传递附加选项。
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"],
)
模型
model: string|llm instance
LLM 模型路径或 LLM 管道实例。为了向后兼容,也支持 llm
参数。
有关更多信息,请参见LLM 管道。
工具
tools: list
提供给代理的工具列表。支持以下配置。
函数
函数工具需要以下字典字段。
字段 | 描述 |
---|---|
名称 | 工具名称 |
描述 | 工具描述 |
目标 | 目标方法/可调用对象 |
函数或可调用方法也可以直接在 tools
列表中提供。在这种情况下,字段会从方法的文档中推断出来。
嵌入
嵌入索引具有内置支持。提供以下字典配置可以将嵌入索引添加为工具。
字段 | 描述 |
---|---|
名称 | 嵌入索引名称 |
描述 | 嵌入索引描述 |
**kwargs | 传递给 embeddings.load 的参数 |
工具
可以提供一个工具实例。此外,以下字符串可以直接加载工具。
工具 | 描述 |
---|---|
http.* | Model Context Protocol (MCP) 服务器的 HTTP 路径 |
python | 运行一个 Python 动作 |
websearch | 使用内置的 websearch 工具运行网页搜索 |
webview | 从网页提取内容 |
方法
method: code|tool
设置代理方法。支持 code
或 tool
调用代理(默认)。code
代理生成并执行 Python 代码。tool
调用代理生成 JSON 块并在这些块中调用代理。