feat(api): enhance model assignment logic in image handlers
- Updated `buildImagesResponsesRequest` to derive `model` dynamically based on `toolJSON`. - Adjusted streaming execution to handle dynamic model resolution across multiple contexts. Closes: #2965
This commit is contained in:
@@ -499,7 +499,17 @@ func (h *OpenAIAPIHandler) imagesEditsFromJSON(c *gin.Context) {
|
|||||||
|
|
||||||
func buildImagesResponsesRequest(prompt string, images []string, toolJSON []byte) []byte {
|
func buildImagesResponsesRequest(prompt string, images []string, toolJSON []byte) []byte {
|
||||||
req := []byte(`{"instructions":"","stream":true,"reasoning":{"effort":"medium","summary":"auto"},"parallel_tool_calls":true,"include":["reasoning.encrypted_content"],"model":"","store":false,"tool_choice":{"type":"image_generation"}}`)
|
req := []byte(`{"instructions":"","stream":true,"reasoning":{"effort":"medium","summary":"auto"},"parallel_tool_calls":true,"include":["reasoning.encrypted_content"],"model":"","store":false,"tool_choice":{"type":"image_generation"}}`)
|
||||||
req, _ = sjson.SetBytes(req, "model", defaultImagesMainModel)
|
mainModel := defaultImagesMainModel
|
||||||
|
if len(toolJSON) > 0 && json.Valid(toolJSON) {
|
||||||
|
toolModel := strings.TrimSpace(gjson.GetBytes(toolJSON, "model").String())
|
||||||
|
if idx := strings.LastIndex(toolModel, "/"); idx > 0 && idx < len(toolModel)-1 {
|
||||||
|
prefix := strings.TrimSpace(toolModel[:idx])
|
||||||
|
if prefix != "" {
|
||||||
|
mainModel = prefix + "/" + defaultImagesMainModel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
req, _ = sjson.SetBytes(req, "model", mainModel)
|
||||||
|
|
||||||
input := []byte(`[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]`)
|
input := []byte(`[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]`)
|
||||||
input, _ = sjson.SetBytes(input, "0.content.0.text", prompt)
|
input, _ = sjson.SetBytes(input, "0.content.0.text", prompt)
|
||||||
@@ -530,7 +540,11 @@ func (h *OpenAIAPIHandler) collectImagesFromResponses(c *gin.Context, responsesR
|
|||||||
cliCtx = handlers.WithDisallowFreeAuth(cliCtx)
|
cliCtx = handlers.WithDisallowFreeAuth(cliCtx)
|
||||||
stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx)
|
stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx)
|
||||||
|
|
||||||
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", defaultImagesMainModel, responsesReq, "")
|
mainModel := strings.TrimSpace(gjson.GetBytes(responsesReq, "model").String())
|
||||||
|
if mainModel == "" {
|
||||||
|
mainModel = defaultImagesMainModel
|
||||||
|
}
|
||||||
|
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", mainModel, responsesReq, "")
|
||||||
|
|
||||||
out, errMsg := collectImagesFromResponsesStream(cliCtx, dataChan, errChan, responseFormat)
|
out, errMsg := collectImagesFromResponsesStream(cliCtx, dataChan, errChan, responseFormat)
|
||||||
stopKeepAlive()
|
stopKeepAlive()
|
||||||
@@ -718,7 +732,11 @@ func (h *OpenAIAPIHandler) streamImagesFromResponses(c *gin.Context, responsesRe
|
|||||||
|
|
||||||
cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background())
|
cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background())
|
||||||
cliCtx = handlers.WithDisallowFreeAuth(cliCtx)
|
cliCtx = handlers.WithDisallowFreeAuth(cliCtx)
|
||||||
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", defaultImagesMainModel, responsesReq, "")
|
mainModel := strings.TrimSpace(gjson.GetBytes(responsesReq, "model").String())
|
||||||
|
if mainModel == "" {
|
||||||
|
mainModel = defaultImagesMainModel
|
||||||
|
}
|
||||||
|
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", mainModel, responsesReq, "")
|
||||||
|
|
||||||
setSSEHeaders := func() {
|
setSSEHeaders := func() {
|
||||||
c.Header("Content-Type", "text/event-stream")
|
c.Header("Content-Type", "text/event-stream")
|
||||||
|
|||||||
Reference in New Issue
Block a user