refactor: 实现 ConversionEngine 协议转换引擎,替代旧 protocol 包
引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
This commit is contained in:
@@ -4,10 +4,14 @@ AI 网关后端服务,提供统一的大模型 API 代理接口。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 支持 OpenAI 协议(`/v1/chat/completions`)
|
||||
- 支持 Anthropic 协议(`/v1/messages`)
|
||||
- 支持 OpenAI 协议(`/openai/v1/...`)
|
||||
- 支持 Anthropic 协议(`/anthropic/v1/...`)
|
||||
- 支持 Hub-and-Spoke 跨协议双向转换(OpenAI ↔ Anthropic)
|
||||
- 同协议透传(零语义损失、零序列化开销)
|
||||
- 支持流式响应(SSE)
|
||||
- 支持 Function Calling / Tools
|
||||
- 支持 Thinking / Reasoning
|
||||
- 支持扩展层接口(Models、Embeddings、Rerank)
|
||||
- 多供应商配置和路由
|
||||
- 用量统计
|
||||
- 结构化日志(zap + lumberjack)
|
||||
@@ -48,19 +52,36 @@ backend/
|
||||
│ │ │ ├── logging.go
|
||||
│ │ │ ├── recovery.go
|
||||
│ │ │ └── cors.go
|
||||
│ │ ├── openai_handler.go
|
||||
│ │ ├── anthropic_handler.go
|
||||
│ │ ├── proxy_handler.go # 统一代理处理器
|
||||
│ │ ├── provider_handler.go
|
||||
│ │ ├── model_handler.go
|
||||
│ │ └── stats_handler.go
|
||||
│ ├── protocol/ # 协议适配器
|
||||
│ │ ├── openai/
|
||||
│ │ │ ├── types.go # 请求/响应类型 + 验证
|
||||
│ │ │ └── adapter.go # OpenAI 协议适配
|
||||
│ │ └── anthropic/
|
||||
│ │ ├── types.go # 请求/响应类型 + 验证
|
||||
│ │ ├── converter.go # 协议转换
|
||||
│ │ └── stream_converter.go # 流式转换
|
||||
│ ├── conversion/ # 协议转换引擎
|
||||
│ │ ├── canonical/ # Canonical Model
|
||||
│ │ │ ├── types.go # 核心请求/响应类型
|
||||
│ │ │ ├── stream.go # 流式事件类型
|
||||
│ │ │ └── extended.go # 扩展层 Models
|
||||
│ │ ├── openai/ # OpenAI 协议适配器
|
||||
│ │ │ ├── types.go
|
||||
│ │ │ ├── adapter.go
|
||||
│ │ │ ├── decoder.go
|
||||
│ │ │ ├── encoder.go
|
||||
│ │ │ ├── stream_decoder.go
|
||||
│ │ │ └── stream_encoder.go
|
||||
│ │ ├── anthropic/ # Anthropic 协议适配器
|
||||
│ │ │ ├── types.go
|
||||
│ │ │ ├── adapter.go
|
||||
│ │ │ ├── decoder.go
|
||||
│ │ │ ├── encoder.go
|
||||
│ │ │ ├── stream_decoder.go
|
||||
│ │ │ └── stream_encoder.go
|
||||
│ │ ├── adapter.go # ProtocolAdapter 接口 + Registry
|
||||
│ │ ├── stream.go # StreamDecoder/Encoder/Converter
|
||||
│ │ ├── middleware.go # Middleware 接口和 Chain
|
||||
│ │ ├── engine.go # ConversionEngine 门面
|
||||
│ │ ├── errors.go # ConversionError
|
||||
│ │ ├── interface.go # InterfaceType 枚举
|
||||
│ │ └── provider.go # TargetProvider
|
||||
│ ├── provider/ # 供应商客户端
|
||||
│ │ └── client.go
|
||||
│ ├── repository/ # 数据访问层
|
||||
@@ -184,10 +205,15 @@ goose -dir migrations sqlite3 ~/.nex/config.db up
|
||||
|
||||
### 代理接口
|
||||
|
||||
#### OpenAI Chat Completions
|
||||
使用 `/{protocol}/v1/{path}` URL 前缀路由:
|
||||
|
||||
#### OpenAI 协议代理
|
||||
|
||||
```
|
||||
POST /v1/chat/completions
|
||||
POST /openai/v1/chat/completions
|
||||
GET /openai/v1/models
|
||||
POST /openai/v1/embeddings
|
||||
POST /openai/v1/rerank
|
||||
```
|
||||
|
||||
请求示例:
|
||||
@@ -202,10 +228,11 @@ POST /v1/chat/completions
|
||||
}
|
||||
```
|
||||
|
||||
#### Anthropic Messages
|
||||
#### Anthropic 协议代理
|
||||
|
||||
```
|
||||
POST /v1/messages
|
||||
POST /anthropic/v1/messages
|
||||
GET /anthropic/v1/models
|
||||
```
|
||||
|
||||
请求示例:
|
||||
@@ -220,6 +247,8 @@ POST /v1/messages
|
||||
}
|
||||
```
|
||||
|
||||
**协议转换**:网关支持任意协议间的双向转换。客户端使用 OpenAI 协议请求,上游供应商可以是 Anthropic 协议(反之亦然)。同协议时自动透传,零序列化开销。
|
||||
|
||||
### 管理接口
|
||||
|
||||
#### 供应商管理
|
||||
@@ -237,10 +266,15 @@ POST /v1/messages
|
||||
"id": "openai",
|
||||
"name": "OpenAI",
|
||||
"api_key": "sk-...",
|
||||
"base_url": "https://api.openai.com/v1"
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"protocol": "openai"
|
||||
}
|
||||
```
|
||||
|
||||
**Protocol 字段说明:**
|
||||
- `protocol` 标识上游供应商使用的协议类型,可选值:`"openai"`(默认)、`"anthropic"`
|
||||
- 同协议透传时,请求体和响应体原样转发,零序列化开销
|
||||
|
||||
**重要说明:**
|
||||
- `base_url` 应配置到 API 版本路径,不包含具体端点
|
||||
- OpenAI: `https://api.openai.com/v1`
|
||||
|
||||
Reference in New Issue
Block a user