feat: Anthropic Messages API compat (/v1/messages)
Add a wire-compatible Anthropic endpoint alongside the existing OpenAI one
so Claude Code / anthropic-sdk / Cursor Agent can hit Lingma directly.
- app/anthropic_schema.py (new): request model + content-block flattener
+ internal-messages adapter + affinity key helper. Handles text / image /
tool_use / tool_result blocks; unknown types degrade gracefully.
- app/auth.py: add require_anthropic_key (x-api-key, Bearer fallback)
and AnthropicAuthError so auth failures render in Anthropic's error
envelope instead of FastAPI's {detail:...} wrapper.
- app/main.py: POST /v1/messages. Shares LingmaPool / SessionCache /
InFlightGuard / StatsCollector with the OpenAI path — same api_key +
same conversation prefix hits the same upstream sessionId across both
protocols (KV cache carries over). Streaming emits the named Anthropic
event sequence (message_start / content_block_start / content_block_delta
/ content_block_stop / message_delta / message_stop). No claude-*
model mapping table: resolve_model's default fallback handles it.
- README.md / DESIGN.md: document the new endpoint, add decision 5.12,
iteration history M5, and a 4.3b streaming flow diagram.
- Bump FastAPI app version to 0.4.0.
Made-with: Cursor
This commit is contained in:
44
README.md
44
README.md
@@ -2,7 +2,10 @@
|
||||
|
||||
把本地 Lingma 插件封装成 OpenAI 兼容接口。任何能调 OpenAI 的客户端(Cursor、Dify、LangChain、curl…)都能直接接入。
|
||||
|
||||
**支持:** `GET /v1/models` / `POST /v1/chat/completions`(含 SSE 流式) / Bearer 鉴权 / Prometheus / 多账号实例池 / 会话复用 / 免浏览器登录态注入。
|
||||
**支持:**
|
||||
- OpenAI 兼容:`GET /v1/models` / `POST /v1/chat/completions`(含 SSE 流式) / Bearer 鉴权
|
||||
- **Anthropic 兼容**:`POST /v1/messages`(含 Anthropic SSE 事件流) / `x-api-key` 鉴权
|
||||
- Prometheus / 多账号实例池 / 会话复用(跨两种协议共享) / 免浏览器登录态注入
|
||||
|
||||
> 想看架构、模块划分、设计决策、二开路线图 → 直接读 [`DESIGN.md`](./DESIGN.md)。
|
||||
|
||||
@@ -160,6 +163,7 @@ curl -s http://127.0.0.1:8317/v1/chat/completions \
|
||||
| GET | `/healthz` | 免鉴权;返回 `ok` / `pool_size` / `pool_ready` / 每实例状态 |
|
||||
| GET | `/v1/models` | OpenAI 兼容;`id` 是 Lingma 原 key,`name` 是可读名 |
|
||||
| POST | `/v1/chat/completions` | OpenAI 兼容;`stream=true` 走 SSE;`model: "agent"` 切 agent 模式 |
|
||||
| POST | `/v1/messages` | **Anthropic Messages 兼容**;`x-api-key` 或 `Authorization: Bearer`;`stream=true` 走 Anthropic 命名事件 SSE |
|
||||
|
||||
**chat 请求示例(非流式)**
|
||||
|
||||
@@ -182,6 +186,44 @@ curl -N http://127.0.0.1:8317/v1/chat/completions \
|
||||
}'
|
||||
```
|
||||
|
||||
**Anthropic Messages 示例(非流式)**
|
||||
|
||||
```bash
|
||||
curl -s http://127.0.0.1:8317/v1/messages \
|
||||
-H "x-api-key: $API_KEY" \
|
||||
-H "anthropic-version: 2023-06-01" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"model":"claude-3-5-sonnet-20241022",
|
||||
"max_tokens":256,
|
||||
"system":"你是一个简洁的助手",
|
||||
"messages":[{"role":"user","content":"你好"}]
|
||||
}'
|
||||
```
|
||||
|
||||
**Anthropic Messages 示例(流式)**
|
||||
|
||||
```bash
|
||||
curl -N http://127.0.0.1:8317/v1/messages \
|
||||
-H "x-api-key: $API_KEY" \
|
||||
-H "anthropic-version: 2023-06-01" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"model":"claude-3-5-sonnet-20241022",
|
||||
"max_tokens":256,
|
||||
"stream":true,
|
||||
"messages":[{"role":"user","content":"写一首四行诗"}]
|
||||
}'
|
||||
# 返回 message_start / content_block_start / content_block_delta* /
|
||||
# content_block_stop / message_delta / message_stop
|
||||
```
|
||||
|
||||
说明:
|
||||
- **模型名兼容**:客户端可以继续传 `claude-3-*` 等名字;未识别的 model 会回退到 `DEFAULT_MODEL` 对应的 Lingma key,后端实际仍由 Lingma 提供(Qwen 系列)。如需显式选模型,直接传 Lingma key(`dashscope_qmodel` 等)。
|
||||
- **会话复用共享**:Anthropic 与 OpenAI 两个端点共用同一 `SessionCache`,只要 API key 相同、对话前缀相同,就会命中同一上游 `sessionId`。
|
||||
- **多模态**:`image` 块会被降级为 `[image]` 占位符(Lingma 不支持 vision);`tool_use` / `tool_result` 会以纯文本形式保留语义。
|
||||
- **鉴权**:优先 `x-api-key` 头(Anthropic 官方 SDK 默认),回退 `Authorization: Bearer`(方便 curl / OpenAI 风格客户端)。
|
||||
|
||||
### 3.2 观测(`METRICS_TOKEN` 或 `API_KEYS`)
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|
||||
Reference in New Issue
Block a user