Repository navigation
Feat responses api #4381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat responses api #4381
Changes from 2 commits
72172e6
7cf9eed
7ee7808
4d318a9
f0a3c03
ae3b512
f797ef3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import ( | |
| "github.com/QuantumNous/new-api/constant" | ||
| "github.com/QuantumNous/new-api/dto" | ||
| "github.com/QuantumNous/new-api/i18n" | ||
| "github.com/QuantumNous/new-api/logger" | ||
| "github.com/QuantumNous/new-api/model" | ||
| relayconstant "github.com/QuantumNous/new-api/relay/constant" | ||
| "github.com/QuantumNous/new-api/service" | ||
|
|
@@ -339,6 +340,32 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { | |
| if strings.HasPrefix(c.Request.URL.Path, "/v1/responses/compact") && modelRequest.Model != "" { | ||
| modelRequest.Model = ratio_setting.WithCompactModelSuffix(modelRequest.Model) | ||
| } | ||
| if strings.HasPrefix(c.Request.URL.Path, "/v1/responses") && modelRequest.Model == "" { | ||
| // logger.LogInfo(c, "DEBUG: headers: " + fmt.Sprintf("%v", c.Request.Header)) | ||
| modelRequest.Model = c.Query("model") | ||
| if modelRequest.Model == "" { | ||
| protocol := c.GetHeader("Sec-WebSocket-Protocol") | ||
| if protocol != "" { | ||
| parts := strings.Split(protocol, ",") | ||
| for _, part := range parts { | ||
| part = strings.TrimSpace(part) | ||
| if strings.HasPrefix(part, "openai-model.") { | ||
| modelRequest.Model = strings.TrimPrefix(part, "openai-model.") | ||
| break | ||
| } | ||
| // Fallback: If it's a common model name pattern but not prefixed | ||
| if !strings.HasPrefix(part, "openai-") && !strings.Contains(part, "realtime") && strings.Contains(part, "-") { | ||
| modelRequest.Model = part | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // 終極保底:如果真的什麼都找不到,強制使用這個預設值以避免 400 錯誤 | ||
| if modelRequest.Model == "" { | ||
| modelRequest.Model = "gpt-5.3-codex" | ||
| logger.LogInfo(c, "WebSocket responses: model completely missing, fallback to gpt-5.3-codex") | ||
| } | ||
| } | ||
|
Comment on lines
+343
to
+368
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent hard-coded model fallback and stray debug artifact. A few concerns in this
🔧 Suggested change if strings.HasPrefix(c.Request.URL.Path, "/v1/responses") && modelRequest.Model == "" {
- // logger.LogInfo(c, "DEBUG: headers: " + fmt.Sprintf("%v", c.Request.Header))
modelRequest.Model = c.Query("model")
if modelRequest.Model == "" {
protocol := c.GetHeader("Sec-WebSocket-Protocol")
if protocol != "" {
parts := strings.Split(protocol, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if strings.HasPrefix(part, "openai-model.") {
modelRequest.Model = strings.TrimPrefix(part, "openai-model.")
break
}
- // Fallback: If it's a common model name pattern but not prefixed
if !strings.HasPrefix(part, "openai-") && !strings.Contains(part, "realtime") && strings.Contains(part, "-") {
modelRequest.Model = part
+ break
}
}
}
}
- // 終極保底:如果真的什麼都找不到,強制使用這個預設值以避免 400 錯誤
- if modelRequest.Model == "" {
- modelRequest.Model = "gpt-5.3-codex"
- logger.LogInfo(c, "WebSocket responses: model completely missing, fallback to gpt-5.3-codex")
- }
}🤖 Prompt for AI Agents |
||
| return &modelRequest, shouldSelectChannel, nil | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package claude | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Two items on this conversion:
🔧 Suggested fix for the JSON wrapper usage-import (
- "encoding/json"
+import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"github.com/QuantumNous/new-api/dto"
+ "github.com/QuantumNous/new-api/common"
...
- if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {
+ if err := common.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {As per coding guidelines: "All JSON marshal/unmarshal operations MUST use wrapper functions in Also applies to: 112-162 🤖 Prompt for AI Agents |
||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -14,6 +15,7 @@ import ( | |
| "github.com/QuantumNous/new-api/types" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/samber/lo" | ||
| ) | ||
|
|
||
| type Adaptor struct { | ||
|
|
@@ -108,8 +110,55 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela | |
| } | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| // TODO implement me | ||
| return nil, errors.New("not implemented") | ||
| // Bridge Responses API to standard OpenAI Chat format, then pass through Claude converter. | ||
| oaiReq := &dto.GeneralOpenAIRequest{ | ||
| Model: request.Model, | ||
| Stream: lo.ToPtr(false), | ||
| } | ||
|
|
||
| if request.MaxOutputTokens != nil { | ||
| oaiReq.MaxTokens = request.MaxOutputTokens | ||
| } | ||
| if request.Temperature != nil { | ||
| oaiReq.Temperature = request.Temperature | ||
| } | ||
| if request.TopP != nil { | ||
| oaiReq.TopP = request.TopP | ||
| } | ||
|
|
||
| // Instructions -> System Message | ||
| if len(request.Instructions) > 0 { | ||
| var instrStr string | ||
| if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "system", | ||
| Content: instrStr, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Input -> User Messages | ||
| if len(request.Input) > 0 { | ||
| inputs := request.ParseInput() | ||
| var contentParts []dto.MediaContent | ||
| for _, inp := range inputs { | ||
| if inp.Type == "input_text" { | ||
| contentParts = append(contentParts, dto.MediaContent{Type: "text", Text: inp.Text}) | ||
| } | ||
| } | ||
| if len(contentParts) == 1 { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "user", | ||
| Content: contentParts[0].Text, | ||
| }) | ||
| } else if len(contentParts) > 1 { | ||
| msg := dto.Message{Role: "user"} | ||
| msg.SetMediaContent(contentParts) | ||
| oaiReq.Messages = append(oaiReq.Messages, msg) | ||
| } | ||
| } | ||
|
|
||
| return a.ConvertOpenAIRequest(c, info, oaiReq) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package gemini | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use The -import (
- "encoding/json"
...
- if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {
+ if err := common.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {As per coding guidelines: "All JSON marshal/unmarshal operations MUST use wrapper functions in Also applies to: 264-272 🤖 Prompt for AI Agents |
||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -161,6 +162,11 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { | |
| } | ||
|
|
||
| action := "generateContent" | ||
| if info.RelayMode == constant.RelayModeResponses { | ||
| // Force non-streaming for Gemini via New API for stability with Responses API. | ||
| info.IsStream = false | ||
| } | ||
|
|
||
| if info.IsStream { | ||
| action = "streamGenerateContent?alt=sse" | ||
| if info.RelayMode == constant.RelayModeGemini { | ||
|
|
@@ -238,15 +244,73 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela | |
| } | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| // TODO implement me | ||
| return nil, errors.New("not implemented") | ||
| // Bridge Responses API to standard OpenAI Chat format | ||
| oaiReq := &dto.GeneralOpenAIRequest{ | ||
| Model: request.Model, | ||
| Stream: lo.ToPtr(false), | ||
| } | ||
|
|
||
| if request.MaxOutputTokens != nil { | ||
| oaiReq.MaxTokens = request.MaxOutputTokens | ||
| } | ||
| if request.Temperature != nil { | ||
| oaiReq.Temperature = request.Temperature | ||
| } | ||
| if request.TopP != nil { | ||
| oaiReq.TopP = request.TopP | ||
| } | ||
|
|
||
| // Convert instructions into a system message | ||
| if len(request.Instructions) > 0 { | ||
| var instrStr string | ||
| if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "system", | ||
| Content: instrStr, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Convert input into user messages | ||
| if len(request.Input) > 0 { | ||
| inputs := request.ParseInput() | ||
| var contentParts []dto.MediaContent | ||
| for _, inp := range inputs { | ||
| switch inp.Type { | ||
| case "input_text": | ||
| contentParts = append(contentParts, dto.MediaContent{Type: "text", Text: inp.Text}) | ||
| case "input_image": | ||
| contentParts = append(contentParts, dto.MediaContent{ | ||
| Type: "image_url", | ||
| ImageUrl: &dto.MessageImageUrl{Url: inp.ImageUrl}, | ||
| }) | ||
| } | ||
| } | ||
| if len(contentParts) == 1 && contentParts[0].Type == "text" { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "user", | ||
| Content: contentParts[0].Text, | ||
| }) | ||
| } else if len(contentParts) > 0 { | ||
| msg := dto.Message{Role: "user"} | ||
| msg.SetMediaContent(contentParts) | ||
| oaiReq.Messages = append(oaiReq.Messages, msg) | ||
| } | ||
| } | ||
|
|
||
| return a.ConvertOpenAIRequest(c, info, oaiReq) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) { | ||
| return channel.DoApiRequest(a, c, info, requestBody) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) { | ||
| // Force non-streaming for Responses API stability | ||
| if info.RelayMode == constant.RelayModeResponses { | ||
| info.IsStream = false | ||
| } | ||
|
|
||
| if info.RelayMode == constant.RelayModeGemini { | ||
| if strings.Contains(info.RequestURLPath, ":embedContent") || | ||
| strings.Contains(info.RequestURLPath, ":batchEmbedContents") { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package zhipu | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use The - if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {
+ if err := common.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" {As per coding guidelines: "All JSON marshal/unmarshal operations MUST use wrapper functions in Also applies to: 101-109 🤖 Prompt for AI Agents |
||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -43,10 +44,9 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) { | |
| } | ||
|
|
||
| func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { | ||
| // Force non-streaming for GLM via New API for stability with Responses API. | ||
| info.IsStream = false | ||
| method := "invoke" | ||
| if info.IsStream { | ||
| method = "sse-invoke" | ||
| } | ||
| return fmt.Sprintf("%s/api/paas/v3/model-api/%s/%s", info.ChannelBaseUrl, info.UpstreamModelName, method), nil | ||
| } | ||
|
Comment on lines
46
to
51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical regression: streaming unconditionally disabled for all Zhipu v3 requests, not just Responses mode.
The sibling Gemini adaptor ( func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
- // Force non-streaming for GLM via New API for stability with Responses API.
- info.IsStream = false
- method := "invoke"
+ // Force non-streaming for GLM via New API for stability with Responses API.
+ if info.RelayMode == constant.RelayModeResponses {
+ info.IsStream = false
+ }
+ method := "invoke"
+ if info.IsStream {
+ method = "sse-invoke"
+ }
return fmt.Sprintf("%s/api/paas/v3/model-api/%s/%s", info.ChannelBaseUrl, info.UpstreamModelName, method), nil
} func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
- // Force non-streaming handler
- info.IsStream = false
- usage, err = zhipuHandler(c, info, resp)
+ if info.RelayMode == constant.RelayModeResponses {
+ info.IsStream = false
+ }
+ if info.IsStream {
+ usage, err = zhipuStreamHandler(c, info, resp)
+ } else {
+ usage, err = zhipuHandler(c, info, resp)
+ }
return
}You'll need to add the Also applies to: 135-140 🤖 Prompt for AI Agents |
||
|
|
||
|
|
@@ -81,16 +81,61 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request | |
| } | ||
|
|
||
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| // TODO implement me | ||
| return nil, errors.New("not implemented") | ||
| // Bridge Responses API to standard OpenAI Chat format | ||
| oaiReq := &dto.GeneralOpenAIRequest{ | ||
| Model: request.Model, | ||
| Stream: lo.ToPtr(false), | ||
| } | ||
|
|
||
| if request.MaxOutputTokens != nil { | ||
| oaiReq.MaxTokens = request.MaxOutputTokens | ||
| } | ||
| if request.Temperature != nil { | ||
| oaiReq.Temperature = request.Temperature | ||
| } | ||
| if request.TopP != nil { | ||
| oaiReq.TopP = request.TopP | ||
| } | ||
|
|
||
| // Instructions -> System Message | ||
| if len(request.Instructions) > 0 { | ||
| var instrStr string | ||
| if err := json.Unmarshal(request.Instructions, &instrStr); err == nil && instrStr != "" { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "system", | ||
| Content: instrStr, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Input -> User Messages | ||
| if len(request.Input) > 0 { | ||
| inputs := request.ParseInput() | ||
| var contentParts []dto.MediaContent | ||
| for _, inp := range inputs { | ||
| if inp.Type == "input_text" { | ||
| contentParts = append(contentParts, dto.MediaContent{Type: "text", Text: inp.Text}) | ||
| } | ||
| } | ||
| if len(contentParts) == 1 { | ||
| oaiReq.Messages = append(oaiReq.Messages, dto.Message{ | ||
| Role: "user", | ||
| Content: contentParts[0].Text, | ||
| }) | ||
| } else if len(contentParts) > 1 { | ||
| msg := dto.Message{Role: "user"} | ||
| msg.SetMediaContent(contentParts) | ||
| oaiReq.Messages = append(oaiReq.Messages, msg) | ||
| } | ||
| } | ||
|
|
||
| return a.ConvertOpenAIRequest(c, info, oaiReq) | ||
| } | ||
|
|
||
| func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) { | ||
| if info.IsStream { | ||
| usage, err = zhipuStreamHandler(c, info, resp) | ||
| } else { | ||
| usage, err = zhipuHandler(c, info, resp) | ||
| } | ||
| // Force non-streaming handler | ||
| info.IsStream = false | ||
| usage, err = zhipuHandler(c, info, resp) | ||
| return | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API key in query string leaks via logs.
Accepting
api_keyas a query-string parameter causes the credential to be written to web server access logs, reverse-proxy logs, browser history, andRefererheaders on outbound navigation. WebSocket clients already have a header-based path viaSec-WebSocket-Protocol(see Lines 279–292) — prefer extending that (e.g., acceptopenai-insecure-api-key.sk-xxxfor/v1/responsesas well) and either gate the query-string form behind a config flag or document that it is for insecure/testing use only.🤖 Prompt for AI Agents