⚡ 创建 LLM 智能体所需的一切尽在于此。访问全面的工具、提示词、框架以及 LLM 模型知识库——一站式简化您的智能体开发流程。
DemoGPT AgentHub 是一个功能强大的库,允许您使用各种工具创建、定制和使用 AI 智能体。
要使用 DemoGPT AgentHub,只需安装主包:
pip install demogpt
创建自定义工具非常简单:
from demogpt_agenthub.tools import BaseTool
class MyCustomTool(BaseTool):
def __init__(self):
self.name = "MyCustomTool"
self.description = "这个工具能做很棒的事情!"
super().__init__()
def run(self, query):
# 在此处实现工具的功能
return f"查询结果:{query}"
使用示例:
my_tool = MyCustomTool()
agent = ToolCallingAgent(tools=[my_tool], llm=llm, verbose=True)
agent.run("你能使用我的自定义工具吗?")
示例输出:
推理:
用户要求使用名为 MyCustomTool 的自定义工具。该工具被描述为能做“很棒的事情”,因此我将使用它来处理查询。
工具调用:
MyCustomTool
工具结果:
查询结果:你能使用我的自定义工具吗?
回答:
是的,我成功使用了您的自定义工具!该工具处理了您的查询并返回了以下结果:“查询结果:你能使用我的自定义工具吗?”
DemoGPT AgentHub 附带多个内置工具:
创建智能体:
from demogpt_agenthub.agents import ToolCallingAgent
from demogpt_agenthub.llms import OpenAIChatModel
from demogpt_agenthub.tools import DuckDuckGoSearchTool, WeatherTool
search_tool = DuckDuckGoSearchTool()
weather_tool = WeatherTool()
llm = OpenAIChatModel(model_name="gpt-4o-mini")
agent = ToolCallingAgent(tools=[search_tool, weather_tool], llm=llm, verbose=True)
创建智能体后,使用它来提问或执行任务:
query = "纽约今天的天气怎么样?"
response = agent.run(query)
print(response)
示例输出:
正在移除 rag_chroma 处的现有向量存储
决策:
False
推理:
要查找纽约今天的天气,我首先需要获取当前的天气信息。获取天气信息后,我就可以为您提供当前的天气状况。RAG 工具将帮助我查找纽约当前的天气。
工具调用:
RAG
工具参数:
{'query': 'weather in New York today'}
工具结果:
纽约当前的天气情况如下:
详细状态:晴朗
风速:2.57 米/秒,方向:240°
湿度:56%
温度:
- 当前:24.36°C
- 最高:25.74°C
- 最低:22.05°C
- 体感:24.46°C
降雨:{}
热指数:无
云量:0%
回答:
纽约当前天气晴朗,温度为 24.36°C(体感 24.46°C)。湿度为 56%,风速为 2.57 米/秒,方向 240°。云量为 0%。
此示例展示了智能体如何:
1. 通过 RAG 访问文档信息
2. 使用 Python 进行计算
3. 结合多种工具回答复杂问题
智能体将:
1. 使用 RAG 查找纽约当前天气信息
2. 使用 Python 工具计算当前天气状况
3. 结合两方面的信息提供全面的答案
以下是创建自定义幂运算工具的示例:
from demogpt_agenthub.tools import BaseTool
class MyPowerTool(BaseTool):
def __init__(self):
self.name = "MyPowerTool"
self.description = "此工具用于计算数字的幂"
super().__init__()
def run(self, a: int, b: int):
# 在此处实现工具的功能
return a**b
power_tool = MyPowerTool()
agent = ToolCallingAgent(tools=[search_tool, weather_tool, power_tool], llm=llm, verbose=True)
agent.run("34 的 26 次方是多少?")
示例输出:
推理:
任务是计算一个数的幂。最适合此计算的是 MyPowerTool,它是专门为幂运算设计的。
工具调用:
MyPowerTool
工具结果:
6583424253569334549714045134721532297216
回答:
34 的 26 次方是 6583424253569334549714045134721532297216。
ReactAgent 提供了详细的推理过程:
from demogpt_agenthub.tools import DuckDuckGoSearchTool, WeatherTool, PythonTool
from demogpt_agenthub.llms import OpenAIChatModel
from demogpt_agenthub.agents import ReactAgent
search_tool = DuckDuckGoSearchTool()
weather_tool = WeatherTool()
python_tool = PythonTool()
agent = ReactAgent(
tools=[search_tool, weather_tool, python_tool],
llm=OpenAIChatModel(model_name="gpt-4o-mini"),
verbose=True
)
query = "Cristiano Ronaldo 目前所在国家的天气温度的平方根是多少?"
print(agent.run(query))
示例输出:
决策:
False
推理:
首先,我需要确定 Christiano Ronaldo 目前所在的位置,以便获取当前温度。然后,我将使用 OpenWeatherMap 工具获取温度信息。最后,我将使用 Python 解释器工具精确计算温度的平方根。
工具调用:
duckduckgo_search
工具参数:
{'inp': 'Where is Christiano Ronaldo currently playing?'}
工具结果:
Ronaldo started Al Nassr's home match against Saudi Pro League leaders Al Ittihad on May 7. Stefano Pioli's men went into a 2-0 first-half lead but were beaten 3-2 when Houssem Aouar stuck in ... Cristiano Ronaldo and his Al Nassr team endured disappointment in 2023/24 as they failed to win the Saudi Pro League or the AFC Champions League. After Sadio Mane, Seko Fofana, Marcelo Brozovic ... Yes, Cristiano Ronaldo will be in the lineup to play tonight. Now that they once again failed to win the AFC Champions League Elite, Ronaldo will still have some optimism that they can win the league despite being eight points behind Al Ittihad. Despite the club's poor season, Ronaldo has maintained his goalscoring form, netting 23 goals in ... Cristiano Ronaldo currently plays for Al Nassr in the Saudi Pro League. The Portugal international transferred from Manchester United in December 2022 and inked a two-and-a-half-year contract with ... Is Cristiano Ronaldo playing Today? Cristiano Ronaldo is expected to start in today's crucial match. The Portuguese star remains Al Nassr's top scorer in the AFC Champions League, with six goals to his name. His outstanding form has played a pivotal role in the team's journey to the semifinals—now just one win away from reaching a continental final for the first time since 1995, when ...
决策:
False
推理:
我需要使用 OpenWeatherMap 工具获取 Cristiano Ronaldo 所在国家(沙特阿拉伯)的当前温度。获取温度后,我将使用 Python 解释器工具计算其平方根。
工具调用:
open_weather_map
工具参数:
{'city': 'Riyadh,SA'}
工具结果:
在 Riyadh,SA,当前天气情况如下:
详细状态:晴朗
风速:5.44 米/秒,方向:150°
湿度:7%
温度:
当前:39.16°C
最高:39.16°C
最低:39.16°C
体感:35.82°C
降雨:{}
热指数:无
云量:0%
决策:
False
推理:
我需要计算沙特阿拉伯当前温度(39.16°C)的平方根。我将使用 Python 解释器工具进行精确计算。
工具调用:
Python Interpreter
工具参数:
{'code': 'import math\nresult = math.sqrt(39.16)\nprint(result)'}
工具结果:
6.257795138864806
决策:
True
回答:
Cristiano Ronaldo 所在国家沙特阿拉伯当前温度的平方根约为 6.26。
from demogpt_agenthub.tools import YoloTool
from demogpt_agenthub.llms import OpenAIChatModel
from demogpt_agenthub.agents import ReactAgent
yolo_tool = YoloTool()
agent = ReactAgent(tools=[yolo_tool], llm=OpenAIChatModel(model_name="gpt-4o-mini"), verbose=True)
query = "告诉我图片 https://ultralytics.com/images/bus.jpg 中的物体"
print(agent.run(query))
示例输出:
决策:
False
推理:
任务是识别并列出给定图片中的物体。可用工具“YOLO 目标检测”专为此目的设计,可以分析图片并输出检测到的物体的详细描述。
工具调用:
YOLO Object Detection
工具参数:
{'path': 'https://ultralytics.com/images/bus.jpg'}
在本地 bus.jpg 找到 https://ultralytics.com/images/bus.jpg
图片 1/1 /home/melih/Desktop/projects/DemoGPT/bus.jpg: 640x480 4 人, 1 公交车, 59.1ms
速度:每张图片预处理 2.0ms,推理 59.1ms,后处理 38.9ms,形状 (1, 3, 640, 480)
工具结果:
在图片中。
图片中有 1 辆公交车,4 个人。
公交车[1] 从 (3, 229) 到 (796, 728)。
人[1] 从 (671, 394) 到 (809, 878)。
人[2] 从 (47, 399) 到 (239, 904)。
人[3] 从 (223, 408) 到 (344, 860)。
人[4] 从 (0, 556) 到 (68, 872)。
决策:
True
回答:
在您提供的图片中,检测到以下物体:
- 1 辆公交车
- 4 个人
公交车位于坐标 (3, 229) 到 (796, 728)。人的位置坐标如下:
- 人 1: (671, 394) 到 (809, 878)
- 人 2: (47, 399) 到 (239, 904)
- 人 3: (223, 408) 到 (344, 860)
- 人 4: (0, 556) 到 (68, 872)
这些示例展示了 ReactAgent 如何:
1. 使用多种工具处理复杂的多步骤查询
2. 通过 YOLO 目标检测执行计算机视觉任务
3. 展示详细的推理和决策过程
4. 提供结构化和信息丰富的响应
BaseRAG 提供了一种简单的方法,可以使用各种向量存储实现检索增强生成:
from demogpt_agenthub.rag import BaseRAG
from demogpt_agenthub.llms import OpenAIChatModel
# 初始化 RAG 系统
rag = BaseRAG(
llm=OpenAIChatModel(model_name="gpt-4o-mini"),
vectorstore="chroma", # 支持 "chroma"、"pinecone"、"faiss"
persistent_path="rag_chroma", # 向量数据库存储位置
index_name="rag_index",
reset_vectorstore=True, # 是否重置现有向量存储
embedding_model_name="sentence-transformers/all-mpnet-base-v2", # 或使用 OpenAI 模型
filter={"search_kwargs": {"score_threshold": 0.5}}
)
# 添加文档
rag.add_files(["~/Downloads/Resume.pdf"]) # 支持 PDF、TXT、CSV、JSON
# 查询 RAG 系统
response = rag.run("这个人的 GitHub 仓库是什么?")
print(response)
**示例输出: