Add OpenAI Responses API (/v1/responses) endpoint
Translate Responses API requests (input→messages, instructions→system,
max_output_tokens→max_tokens, text.format→response_format, flat tools→nested
{function:{…}}) to upstream chat/completions, then convert responses back to
Responses format (streaming SSE event lifecycle + non-streaming JSON).
Verified against OpenAI migration guide and Python SDK Response model:
- Echo back required fields parallel_tool_calls/tool_choice/tools
- Include content:[] in reasoning items, logprobs:[] in output_text parts
- Support function_call/function_call_output multi-turn input items
- Map usage fields prompt_tokens→input_tokens, completion_tokens→output_tokens
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
当前实现包含:
|
||||
|
||||
- 登录页支持插件默认的移动云手机号验证码登录:验证码校验后按插件流程调用 `/api/acepilot/zhanlu/v1/login` 获取用户资料,再通过 SM2 签名调用 `/user/api/v2/external/key/get-or-create` 换取模型 API Key,凭据自动保存到本地 JSON。
|
||||
- OpenAI 兼容接口:`/v1/models`(优先从 `/gateway/v1/model/info` 拉取模型列表)、`/v1/chat/completions`(携带 `Authorization: Bearer <apiKey>` 请求 `{modelBaseUrl}/chat/completions`)。
|
||||
- OpenAI 兼容接口:`/v1/models`(优先从 `/gateway/v1/model/info` 拉取模型列表)、`/v1/chat/completions`(携带 `Authorization: Bearer <apiKey>` 请求 `{modelBaseUrl}/chat/completions`)、`/v1/responses`(Responses API,自动转换请求/响应格式,兼容新版 SDK 与 AI 编码工具)。
|
||||
- 湛卢登录签名逻辑:RSA `authorization`、SHA-256 query hash、HMAC-SHA1 `Signature`,用于 v1.4.2 的 v1/login 认证。
|
||||
- 模型 API Key 换取签名逻辑:SM3 摘要 + SM2 签名(`X-Auth-Signature`/`X-Auth-Timestamp`/`X-Auth-Nonce`)。
|
||||
- 上游 SSE 直接透传为 OpenAI SSE;非流式请求在本地聚合为 OpenAI Chat Completion JSON。
|
||||
@@ -169,6 +169,30 @@ curl -N http://127.0.0.1:8080/v1/chat/completions `
|
||||
|
||||
请求中的 `tools`、`tool_choice` 会传给湛卢模型。流式响应返回增量 `delta.tool_calls`;非流式响应会把分片聚合为完整的 `message.tool_calls`,并保留 `finish_reason: "tool_calls"`。执行工具后,将 assistant 的 `tool_calls` 和 `role: "tool"` 结果放回 `messages` 再发起请求即可得到最终回答。
|
||||
|
||||
### Responses API
|
||||
|
||||
代理还兼容 OpenAI Responses API(`POST /v1/responses`),支持使用该 API 的客户端(如新版 OpenAI SDK、Cursor 等 AI 编码工具)直接对接。代理会将 Responses API 请求格式转换为上游 chat/completions 格式,再将响应转回 Responses 格式。
|
||||
|
||||
请求中的 `input`(字符串或消息数组)、`instructions`(系统提示词)、`max_output_tokens`、`temperature`、`top_p`、`tools`、`tool_choice`、`response_format` 等参数均会翻译并透传给上游。`tools` 格式自动从 Responses 扁平结构转换为 Chat Completions 嵌套 `{function:{…}}` 结构。多轮对话中的 `function_call` 和 `function_call_output` 输入项也会自动转换为对应的 assistant `tool_calls` 和 `role: tool` 消息。
|
||||
|
||||
非流式:
|
||||
|
||||
```powershell
|
||||
curl http://127.0.0.1:8080/v1/responses `
|
||||
-H "Content-Type: application/json" `
|
||||
-d '{"model":"zhanlu/auto","input":"hello","stream":false}'
|
||||
```
|
||||
|
||||
流式:
|
||||
|
||||
```powershell
|
||||
curl -N http://127.0.0.1:8080/v1/responses `
|
||||
-H "Content-Type: application/json" `
|
||||
-d '{"model":"zhanlu/auto","input":"hello","stream":true}'
|
||||
```
|
||||
|
||||
流式响应返回标准 Responses API SSE 事件序列(`response.created` → `response.output_item.added` → `response.output_text.delta` → `response.output_text.done` → `response.output_item.done` → `response.completed`),思考模型额外包含 `response.reasoning_summary_text.delta` 事件,工具调用包含 `response.function_call_arguments.delta` 事件。Token 统计同样记录在 SQLite 中。
|
||||
|
||||
如果设置了本地 OpenAI 兼容 API Key,需要带 `Authorization`:
|
||||
|
||||
```powershell
|
||||
|
||||
Reference in New Issue
Block a user