Skip to content

Commit 0ad17c6

Browse files
authored
feat: 新增 Xyzen 完整文档 - 包括架构、API 参考、高级功能、部署指南等 (#40)
1 parent 7a4264f commit 0ad17c6

10 files changed

Lines changed: 1367 additions & 15 deletions

File tree

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
# 高级功能
2+
3+
Xyzen 提供了一系列强大的高级功能,使你能够集成多种模型提供商、
4+
管理用户消费记录,以及创建可重用的 Agent 框架。
5+
本文档将详细介绍这些高级功能的使用方法。
6+
7+
## 1. 多模型提供商管理
8+
9+
### 概述
10+
11+
Xyzen 支持与多个 LLM 提供商集成,包括:
12+
13+
- **OpenAI** - GPT-4, GPT-4 Turbo, GPT-3.5 等
14+
- **Google Gemini** - Gemini Pro 等
15+
- **Claude (Anthropic)** - Claude 2, Claude Instant 等
16+
- **本地模型** - 通过 Ollama 或其他本地部署
17+
18+
### 提供商配置
19+
20+
#### 创建新提供商
21+
22+
```bash
23+
POST /api/v1/providers
24+
Content-Type: application/json
25+
26+
{
27+
"name": "My OpenAI Account",
28+
"type": "openai",
29+
"config": {
30+
"api_key": "sk-...",
31+
"base_url": "https://api.openai.com/v1",
32+
"model_list": ["gpt-4", "gpt-3.5-turbo"]
33+
},
34+
"is_default": true
35+
}
36+
```
37+
38+
#### 支持的提供商配置
39+
40+
| 提供商 | 必填字段 | 可选字段 |
41+
|--------|---------|---------|
42+
| **OpenAI** | `api_key` | `base_url`, `organization_id` |
43+
| **Google Gemini** | `api_key` | `base_url` |
44+
| **Claude** | `api_key` | `base_url` |
45+
| **本地 (Ollama)** | `base_url` ||
46+
47+
### 动态模型选择
48+
49+
在 Agent 中动态选择模型:
50+
51+
```python
52+
# 服务端示例
53+
from core.providers import get_user_provider_manager
54+
55+
async def execute_with_model_selection(user_id: str, query: str):
56+
provider_manager = await get_user_provider_manager(user_id, db)
57+
58+
# 根据查询复杂度选择模型
59+
if len(query) > 1000:
60+
# 复杂查询使用高级模型
61+
llm = provider_manager.get_llm("gpt-4")
62+
else:
63+
# 简单查询使用低成本模型
64+
llm = provider_manager.get_llm("gpt-3.5-turbo")
65+
66+
response = await llm.apredict(query)
67+
return response
68+
```
69+
70+
### 成本优化
71+
72+
Xyzen 支持根据任务成本进行模型的自动选择:
73+
74+
```python
75+
# 按成本优化的 Agent 配置
76+
agent_config = {
77+
"name": "Cost-Aware Agent",
78+
"routing_strategy": "cost_optimized",
79+
"models": [
80+
{
81+
"model": "gpt-3.5-turbo",
82+
"cost_per_1k_tokens": 0.0015,
83+
"suitability": ["simple_qa", "summarization"]
84+
},
85+
{
86+
"model": "gpt-4",
87+
"cost_per_1k_tokens": 0.03,
88+
"suitability": ["complex_reasoning", "code_generation"]
89+
}
90+
]
91+
}
92+
```
93+
94+
---
95+
96+
## 2. MCP(Model Context Protocol)服务器集成
97+
98+
### 概述
99+
100+
MCP 是一个开放协议,允许 AI 模型通过标准化的接口访问外部工具和数据源。Xyzen 原生支持 MCP 服务器集成。
101+
102+
### 注册 MCP 服务器
103+
104+
#### 通过 API 注册
105+
106+
```bash
107+
POST /api/v1/mcp-servers
108+
Content-Type: application/json
109+
110+
{
111+
"name": "Scientific Tools",
112+
"description": "A collection of scientific computing tools",
113+
"url": "http://localhost:3000/mcp",
114+
"token": "optional-auth-token",
115+
"tags": ["science", "computing"]
116+
}
117+
```
118+
119+
#### 服务器健康检查
120+
121+
```bash
122+
GET /api/v1/mcp-servers/{server_id}/health
123+
```
124+
125+
Xyzen 会自动:
126+
1. 连接到 MCP 服务器
127+
2. 列出所有可用工具
128+
3. 验证工具的可访问性
129+
4. 定期检查服务器状态
130+
131+
### 在 Agent 中使用 MCP 工具
132+
133+
#### 配置 Agent 使用 MCP
134+
135+
```json
136+
{
137+
"name": "Research Assistant",
138+
"agent_type": "regular",
139+
"mcp_servers": [
140+
{
141+
"mcp_server_id": "server-uuid-1",
142+
"enabled": true
143+
},
144+
{
145+
"mcp_server_id": "server-uuid-2",
146+
"enabled": true
147+
}
148+
]
149+
}
150+
```
151+
152+
#### MCP 工具调用流程
153+
154+
<Mermaid chart={`
155+
graph TD
156+
START["用户输入"] --> RECEIVE["Agent 收到请求"]
157+
RECEIVE --> ANALYZE["LLM 分析是否需要工具"]
158+
ANALYZE --> DECIDE{"需要工具?"}
159+
160+
DECIDE -->|否| RESPONSE["直接生成响应"]
161+
RESPONSE --> END1["返回最终答案"]
162+
163+
DECIDE -->|是| CONFIRM{"需要确认?"}
164+
CONFIRM -->|是| CONFIRM_UI["工具确认<br/>用户审批"]
165+
CONFIRM -->|否| EXECUTE
166+
CONFIRM_UI --> EXECUTE["MCP 服务器执行"]
167+
EXECUTE --> RESULT["返回工具结果"]
168+
RESULT --> SYNTHESIS["Agent 综合结果"]
169+
SYNTHESIS --> END1
170+
171+
style START fill:#4f46e5,stroke:#312e81,stroke-width:2px,color:#fff
172+
style RECEIVE fill:#7c3aed,stroke:#5b21b6,stroke-width:2px,color:#fff
173+
style ANALYZE fill:#7c3aed,stroke:#5b21b6,stroke-width:2px,color:#fff
174+
style DECIDE fill:#ec4899,stroke:#be185d,stroke-width:2px,color:#fff
175+
style CONFIRM fill:#ec4899,stroke:#be185d,stroke-width:2px,color:#fff
176+
style CONFIRM_UI fill:#f59e0b,stroke:#d97706,stroke-width:2px,color:#fff
177+
style EXECUTE fill:#06b6d4,stroke:#0e7490,stroke-width:2px,color:#fff
178+
style RESULT fill:#06b6d4,stroke:#0e7490,stroke-width:2px,color:#fff
179+
style RESPONSE fill:#10b981,stroke:#047857,stroke-width:2px,color:#fff
180+
style SYNTHESIS fill:#8b5cf6,stroke:#6d28d9,stroke-width:2px,color:#fff
181+
style END1 fill:#10b981,stroke:#047857,stroke-width:2px,color:#fff
182+
`} />
183+
184+
### 工具使用确认机制
185+
186+
为了提高安全性,Xyzen 支持在执行工具前进行确认:
187+
188+
```bash
189+
POST /api/v1/chat/messages
190+
Content-Type: application/json
191+
192+
{
193+
"agent_id": "agent-uuid",
194+
"content": "计算 123 + 456",
195+
"require_tool_confirmation": true
196+
}
197+
```
198+
199+
响应(等待确认):
200+
201+
```json
202+
{
203+
"message_id": "msg-uuid",
204+
"status": "pending_tool_confirmation",
205+
"pending_tools": [
206+
{
207+
"name": "add",
208+
"description": "Add two numbers",
209+
"arguments": {
210+
"a": 123,
211+
"b": 456
212+
}
213+
}
214+
]
215+
}
216+
```
217+
218+
用户确认后:
219+
220+
```bash
221+
POST /api/v1/chat/messages/{message_id}/confirm-tools
222+
Content-Type: application/json
223+
224+
{
225+
"confirmed": true
226+
}
227+
```
228+
229+
### 内置 MCP 服务器
230+
231+
Xyzen 预置了几个常用的 MCP 服务器:
232+
233+
| 服务器 | 功能 | 工具示例 |
234+
|--------|------|---------|
235+
| **BioYond** | 生物科学工具 | 蛋白质序列分析、基因搜索 |
236+
| **Lab** | 实验室工具 | 数据处理、可视化 |
237+
| **Dify** | 工作流集成 | 调用 Dify 工作流 |
238+
239+
---
240+
241+
242+
## 3. 内置 Agent 系统
243+
244+
### 概述
245+
246+
Xyzen 提供了两个预定义的系统 Agent,可供所有用户使用:
247+
248+
#### 随便聊聊 Assistants Agent
249+
- **用途**:通用对话和问题解答
250+
- **特点**:友好、有用、支持工具调用
251+
- **适用场景**:日常提问、信息查询、任务协助
252+
253+
#### 创作工坊 Development Agent
254+
- **用途**:AI Agent 设计和优化
255+
- **特点**:专业、深入、提供架构建议
256+
- **适用场景**:Agent 设计、提示词工程、工作流优化
257+
258+
### 系统 Agent 配置
259+
260+
```python
261+
# 系统 Agent 配置示例
262+
SYSTEM_AGENTS = {
263+
"chat": {
264+
"name": "随便聊聊",
265+
"capabilities": ["general_chat", "qa", "assistance", "tools"],
266+
"tags": ["助手", "对话", "工具"],
267+
},
268+
"workshop": {
269+
"name": "创作工坊",
270+
"capabilities": ["agent_design", "tool_selection", "prompt_engineering"],
271+
"tags": ["设计", "创作", "优化"],
272+
}
273+
}
274+
```
275+
276+
### 创建内置 Graph Agent
277+
278+
Xyzen 支持将 Python 类自动注册为内置 Graph Agent:
279+
280+
```python
281+
# 示例:创建科研论文分析 Agent
282+
from handler.builtin_agents.base_graph_agent import BaseBuiltinGraphAgent
283+
from langgraph.graph import StateGraph
284+
285+
class ScientificFigureAgent(BaseBuiltinGraphAgent):
286+
"""分析科研论文中的图表的内置 Agent"""
287+
288+
def __init__(self):
289+
super().__init__(
290+
name="Scientific Figure Analyzer",
291+
description="分析并解释科研论文中的图表、数据和可视化",
292+
version="1.0.0",
293+
capabilities=[
294+
"figure_analysis",
295+
"data_extraction",
296+
"interpretation"
297+
],
298+
tags=["science", "analysis", "figures"],
299+
author="Xyzen Team",
300+
license_="Apache 2.0"
301+
)
302+
303+
def build_graph(self) -> CompiledStateGraph:
304+
"""构建工作流"""
305+
workflow = StateGraph(GraphState)
306+
307+
# 添加节点
308+
workflow.add_node("extract", self._extract_figure_data)
309+
workflow.add_node("analyze", self._analyze_content)
310+
workflow.add_node("generate_report", self._generate_report)
311+
312+
# 添加边
313+
workflow.add_edge("extract", "analyze")
314+
workflow.add_edge("analyze", "generate_report")
315+
316+
# 设置入出口
317+
workflow.set_entry_point("extract")
318+
workflow.set_finish_point("generate_report")
319+
320+
return workflow.compile()
321+
322+
def get_state_schema(self) -> dict:
323+
"""返回状态模式"""
324+
return {
325+
"image": "bytes",
326+
"extracted_data": "dict",
327+
"analysis": "str",
328+
"report": "str"
329+
}
330+
331+
async def _extract_figure_data(self, state: dict) -> dict:
332+
"""提取图表数据"""
333+
# 实现数据提取逻辑
334+
return {
335+
**state,
336+
"extracted_data": {...}
337+
}
338+
339+
async def _analyze_content(self, state: dict) -> dict:
340+
"""分析内容"""
341+
# 实现分析逻辑
342+
return {
343+
**state,
344+
"analysis": "..."
345+
}
346+
347+
async def _generate_report(self, state: dict) -> dict:
348+
"""生成报告"""
349+
# 实现报告生成逻辑
350+
return {
351+
**state,
352+
"report": "..."
353+
}
354+
```
355+
356+
内置 Agent 会自动注册到系统中,用户可以直接使用。
357+
358+
---
359+
360+
361+
## 相关资源
362+
363+
- [图 Agent API 文档](/xyzen/api)
364+
- [MCP 服务器开发指南](/mcp/development)

0 commit comments

Comments
 (0)