通用的多模型智能体框架,支持工具调用和多种LLM模型
Project description
SHB Agent 🤖
一个通用的多模型智能体框架,支持灵活的工具调用和多种 LLM 模型。
✨ 特性
- 🔧 灵活的工具系统: 支持
@tool装饰器和传统 Tool 对象 - 🤖 多模型支持: 支持 OpenAI、Anthropic 等多种 LLM 模型
- 📝 通用工具调用: 基于文本解析,不依赖特定模型的 function calling
- 📊 丰富的日志: 显示智能体思考过程和执行步骤
- 🔄 智能检测: 自动检测最终答案并结束任务
- 🎯 易于使用: 简洁的 API 设计
🚀 快速开始
安装
pip install shbagents
基本使用
from shbagents import Agent, OpenAIModel, tool
# 使用 @tool 装饰器定义工具
@tool
def add_numbers(a: float, b: float) -> float:
"""两数相加"""
return a + b
@tool
def compare_numbers(a: float, b: float) -> str:
"""比较两个数的大小"""
if a > b:
return f"{a} 比 {b} 大"
elif a < b:
return f"{a} 比 {b} 小"
else:
return f"{a} 和 {b} 相等"
# 创建模型
model = OpenAIModel(model_id="gpt-4")
# 创建智能体,直接传递装饰过的函数
agent = Agent(
model=model,
tools=[add_numbers, compare_numbers]
)
# 运行任务
result = agent.run("计算 3.5 + 2.7,然后比较结果与 6 的大小")
print(result)
高级使用
混合工具定义
from shb_agent import Agent, Tool, tool
# 方式1: @tool 装饰器
@tool
def calculate(expression: str) -> float:
"""计算数学表达式"""
return eval(expression) # 注意:实际使用中应该用更安全的计算方法
# 方式2: 传统 Tool 对象
def format_number(number: float) -> str:
"""格式化数字"""
return f"{number:.2f}"
format_tool = Tool(
name="format_number",
description="格式化数字为两位小数",
func=format_number,
arguments=[("number", "float")],
outputs="str"
)
# 混合使用两种方式
agent = Agent(
model=model,
tools=[
calculate, # @tool 装饰的函数
format_tool # Tool 对象
]
)
自定义提示模板
from shb_agent import PromptTemplates
custom_templates = {
"system_prompt": """你是一个专业的数学助手。
使用以下工具来帮助解决数学问题:{{ tools }}
调用工具格式:
思考: [你的推理过程]
调用工具: tool_name(参数1=值1, 参数2=值2)
""",
# ... 其他模板
}
agent = Agent(
model=model,
tools=[calculate],
prompt_templates=custom_templates
)
📖 详细文档
工具定义
使用 @tool 装饰器(推荐)
@tool
def search_web(query: str, max_results: int = 5) -> list:
"""搜索网页"""
# 实际的搜索逻辑
return ["结果1", "结果2", "结果3"]
使用 Tool 类
def search_web(query: str, max_results: int = 5) -> list:
"""搜索网页"""
return ["结果1", "结果2", "结果3"]
search_tool = Tool(
name="search_web",
description="搜索网页",
func=search_web,
arguments=[
("query", "str"),
("max_results", "int")
],
outputs="list"
)
模型支持
目前支持的模型:
- OpenAI:
OpenAIModel(model_id="gpt-4") - 更多模型支持即将推出...
日志系统
智能体提供详细的执行日志:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 步骤 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🤖 智能体正在思考和决策...
💭 智能体思考: 用户需要我计算两个数字的和
🔧 调用工具: add_numbers
参数: a=3.5, b=2.7
📤 工具结果: 6.2
🛠️ 开发
从源码安装
git clone https://github.com/huangbaocheng/shb-agent.git
cd shb-agent
pip install -e .
运行示例
make run-example
📋 要求
- Python 3.8+
- openai>=1.0.0
- rich>=13.0.0
- 其他依赖见
requirements.txt
🤝 贡献
欢迎贡献!请查看 CONTRIBUTING.md 了解详情。
📄 许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
🔗 链接
📈 路线图
- 支持更多 LLM 模型 (Anthropic, Gemini, etc.)
- 添加流式输出支持
- 改进工具调用性能
- 添加更多内置工具
- Web UI 界面
如果这个项目对你有帮助,请给个 ⭐️ Star!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shbagents-0.1.0.tar.gz
(25.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
shbagents-0.1.0-py3-none-any.whl
(27.0 kB
view details)
File details
Details for the file shbagents-0.1.0.tar.gz.
File metadata
- Download URL: shbagents-0.1.0.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26ffb41067e38b75dc57dd9b9b8069f07e1ce8f74c55005797518165d01298e4
|
|
| MD5 |
efb06a2f21bbb74337ba6bf20cec648d
|
|
| BLAKE2b-256 |
4c9f3020424f08509a9a329068347f9ad7fc4d715d931ff3c5eb7d056faa1aed
|
File details
Details for the file shbagents-0.1.0-py3-none-any.whl.
File metadata
- Download URL: shbagents-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2832ce0235d7557252f18d583bb59f22d46920260f217b57669d55ffb84c1627
|
|
| MD5 |
b47a2f2af9f5ee2355681848045e3cf2
|
|
| BLAKE2b-256 |
f05b5622b700886e446d128a9c8dff7c077002db13cb714aa4f6ef90b389209a
|