Skip to main content

A FastAPI-first AI agent toolkit with AG-UI protocol support for building modern streaming AI applications.

Project description

YAI Nexus AgentKit

一个以 FastAPI 为核心、支持 AG-UI 协议的 AI 代理工具包,专为构建现代流式 AI 应用而设计。

Python 3.9+ License: MIT FastAPI AG-UI


🚀 快速开始

# 安装工具包
pip install yai-nexus-agentkit

# 运行示例应用
python -m examples.fast_api_app.main

现在,在浏览器中打开 http://localhost:8000/docs,即可探索交互式的 API 文档。


🎯 项目特色

YAI Nexus AgentKit 专为希望使用 FastAPI 构建现代流式 AI 应用的开发者而设计。与其他通用型 AI 框架不同,我们提供:

  • 🔥 FastAPI 优先:专为 Web 应用场景从零开始设计,与 FastAPI 无缝集成。
  • 📡 默认即流式:默认使用 SSE (Server-Sent Events) 提供真正的实时 AI 交互体验。
  • 🎨 高保真 AG-UI 协议:我们对 AG-UI 协议 提供了高保真实现,能够完整、准确地将 Agent 的内部活动(包括工具调用细节)映射到标准事件,实现完全透明、可观察、可调试的前端交互界面。
  • 🔧 三层渐进式 API:提供从简单到高级的三种 API 模式,满足不同复杂度的需求。
  • 📊 支持多种 LLM:无缝切换 OpenAI, Anthropic, ZhipuAI, Tongyi, OpenRouter 等主流供应商。
  • 🏗️ 生产就绪:基于 LangChain 和 LangGraph 的坚实基础构建,稳定可靠。

🏛️ 架构概览

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   前端 UI 应用    │◄──►│ AG-UI 标准事件流 │◄──►│ YAI AgentKit    │
│ (React/Vue/...) │    │   (SSE)         │    │ (AGUIAdapter)   │
└─────────────────┘    └─────────────────┘    └─────────────────┘
                                                        │
                       ┌─────────────────┐    ┌─────────────────┐
                       │   LangGraph     │◄──►│   FastAPI 应用  │
                       │ (业务流程编排)  │    │   (Web 服务层)  │
                       └─────────────────┘    └─────────────────┘
                                │
                       ┌─────────────────┐
                       │   多LLM后端     │
                       │ (OpenAI, 等)    │
                       └─────────────────┘

🎨 三种复杂度的 API

第一层:简单模式

适合快速上手——以最少的设置直接调用 LLM。

from yai_nexus_agentkit import create_llm

# 创建 LLM 客户端
llm = create_llm({
    "provider": "openai",
    "model": "gpt-4o-mini",
    "api_key": "sk-..."
})

# 简单问答
response = llm.invoke("你好,世界!")
print(response.content)

第二层:流式模式

通过 SSE 增加实时流式响应。

from yai_nexus_agentkit.adapter import BasicSSEAdapter
from sse_starlette.sse import EventSourceResponse

# 创建基础的 SSE 适配器
adapter = BasicSSEAdapter(llm)

# FastAPI 端点
@app.post("/chat/stream")
async def chat_stream(request: ChatRequest):
    return EventSourceResponse(
        adapter.stream_response(request.message),
        media_type="text/event-stream"
    )

第三层:高级模式

使用 AGUIAdapter,提供完整的 AG-UI 协议支持和 LangGraph 流程编排,是构建复杂 Agent 的理想选择。

from yai_nexus_agentkit.adapter import AGUIAdapter
from yai_nexus_agentkit.adapter.sse_advanced import Task

# 使用你的 LangGraph Agent 创建高级适配器
# Agent 的内部思考、工具调用等过程将被自动转换为 AG-UI 事件
adapter = AGUIAdapter(your_langgraph_agent)

# 创建兼容 AG-UI 的 FastAPI 端点
@app.post("/chat/agent")
async def chat_advanced(task: Task):
    # Task 模型支持 thread_id,用于实现多轮对话
    # task = Task(id="run-123", query="搜索一下今天的天气", thread_id="thread-abc")
    return EventSourceResponse(
        adapter.event_stream_adapter(task),
        ping=15,
        media_type="text/event-stream"
    )

🔧 安装与配置

基础安装

pip install yai-nexus-agentkit

带可选依赖项的安装

# 安装特定的 LLM 供应商支持
pip install yai-nexus-agentkit[openai,anthropic]

# 安装持久化支持
pip install yai-nexus-agentkit[persistence]

# 安装开发所需全部依赖
pip install yai-nexus-agentkit[dev]

环境配置

在项目根目录创建 .env 文件:

OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
OPENROUTER_API_KEY=sk-or-...
# ... 其他供应商的 API Key

🏗️ 项目结构

yai-nexus-agentkit/
├── src/yai_nexus_agentkit/
│   ├── llm/                 # 多LLM支持层
│   │   ├── factory.py       # LLM 创建工厂
│   │   └── ...
│   ├── adapter/             # 适配器层 (协议转换)
│   │   ├── sse_basic.py     # 基础 SSE 适配器
│   │   ├── sse_advanced.py  # 高级 AG-UI 适配器
│   │   ├── langgraph_events.py # LangGraph 事件枚举
│   │   └── errors.py        # 自定义异常
│   ├── core/                # 核心抽象与业务事件
│   │   └── events.py        # EventEmitter 定义
│   └── persistence/         # 可选的持久化层
├── examples/
│   └── fast_api_app/        # 一个完整的 FastAPI 示例应用
├── tests/
│   ├── unit/                # 单元测试
│   └── integration/         # 集成测试
└── pyproject.toml           # 项目配置文件

🤝 贡献代码

我们非常欢迎社区贡献!请参考我们的 贡献指南

开发环境设置

# 克隆仓库
git clone https://github.com/yai-nexus/yai-nexus-agentkit.git
cd yai-nexus-agentkit

# 以可编辑模式安装,并包含开发依赖
pip install -e ".[dev]"

# 运行测试
pytest

# 代码格式化与检查
black .
ruff check .

📄 许可证

本项目基于 MIT 许可证开源 - 详情请见 LICENSE 文件。


由 YAI Nexus 团队 ❤️ 倾情打造

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

yai_nexus_agentkit-0.0.1.tar.gz (47.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yai_nexus_agentkit-0.0.1-py3-none-any.whl (29.9 kB view details)

Uploaded Python 3

File details

Details for the file yai_nexus_agentkit-0.0.1.tar.gz.

File metadata

  • Download URL: yai_nexus_agentkit-0.0.1.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for yai_nexus_agentkit-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ae30c3200ca32e36951930986f650fb0ba65ae54665cc6f18ac266c7e6988b98
MD5 ed850cce8502d80aa4ea9605955a8f28
BLAKE2b-256 fda4085dd7914747863817c8a3ab7869fab7d516aa6d48083e769c97f8540c12

See more details on using hashes here.

Provenance

The following attestation bundles were made for yai_nexus_agentkit-0.0.1.tar.gz:

Publisher: pypi-publish.yml on yai-nexus/yai-nexus-agentkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yai_nexus_agentkit-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for yai_nexus_agentkit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5d149b5ffd34d28d8cfd144dafadc658f6c3ed55c30779f0d20c09375849519
MD5 f1ea4800890be525bd36c5c131050f0b
BLAKE2b-256 07db4e97c9cba5202132f36fa45fccc22be22ff38fb94b629a5394a9deae30ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for yai_nexus_agentkit-0.0.1-py3-none-any.whl:

Publisher: pypi-publish.yml on yai-nexus/yai-nexus-agentkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page