一句话版本:一个
Agent类、一个protocol字段,几行代码对接 OpenAI / Anthropic / 自家网关。
仓库地址:github.com/secret-tangyuan/tangyuanAI
为什么做这个项目
在做 Agent 应用时,常常会被这些问题绊住:
- 换一个 provider,要重写一遍调用层;
- 不同框架之间的抽象层级差太多,从一个迁移到另一个要改很多;
- 为了用上某个特性,被迫引入一整套运行时。
secret-tangyuan/tangyuanAI 想做的是一个非常薄的核心 —— 让"创建一个能聊天的 Agent"这件事变成几行代码,扩展的部分交给用户自己写,而不是被框架裹挟。
设计目标
- 一个核心类:
Agent。所有能力围绕它展开。 - 一个协议字段:
protocol,告诉框架要按哪种规范去调用上游。 - 零隐式魔法:不替你做 prompt 注入,不替你做工具注册。
- 类型友好:提供完整 type hint,IDE 里写起来不掉链子。
快速上手
python
from tangyuan import Agent
agent = Agent(
protocol="openai", # openai | anthropic | custom
model="gpt-4o-mini",
api_key="sk-...",
system="你是一个简洁、严谨的助理。",
)
async def main():
reply = await agent.chat("用一句话介绍 Python")
print(reply)
import asyncio
asyncio.run(main())切到 Anthropic 只需改一个字段:
python
agent = Agent(
protocol="anthropic",
model="claude-3-5-sonnet-latest",
api_key="sk-ant-...",
)接自家网关也很直接 —— 实现一个 protocol="custom" 的适配器,注册进去就行:
python
from tangyuan import Agent, register_protocol
@register_protocol("internal")
class InternalProtocol:
async def complete(self, messages, **kwargs): ...
async def stream(self, messages, **kwargs): ...
agent = Agent(protocol="internal", endpoint="https://llm.internal/v1")适用场景
- 个人 / 小团队的 chatbot、自动化脚本;
- 教学场景:把"Agent 的核心循环"讲清楚;
- 想做自研框架的起点:fork 一份再按需修改。
不太适合:
- 需要复杂工作流可视化编辑器的产品;
- 已经在用 LangChain / AutoGen 并已深度依赖其生态的项目(迁移成本可能不划算)。
与其它框架的对比(简版)
| 维度 | tangyuanAI | LangChain | AutoGen |
|---|---|---|---|
| 学习成本 | 极低 | 中 | 中高 |
| 抽象层级 | 薄 | 厚 | 厚 |
| Provider 切换 | 改一个字段 | 重写 chain | 部分重写 |
| 隐式行为 | 几乎没有 | 较多 | 较多 |
| 适合 fork | ✓ | △ | △ |
Roadmap
- 更多
protocol适配(Gemini、DeepSeek、Qwen 等); - 内置简单工具注册器(仍保持薄);
- 多 Agent 编排示例;
- 一份手把手的迁移指南。