fix(protocol): Grok 生图 - 让 response_format 开关生效 (#692)

This commit is contained in:
qihang ma
2026-10-06 09:24:16 +08:00
committed by GitHub
parent 53ee5b0cbd
commit f6003c1c88
3 changed files with 18 additions and 1 deletions

View File

@@ -303,7 +303,7 @@ func grokImageRequestBody(input canvasGenerationInput) (grokImageRequest, string
Model: input.Config.Model,
Prompt: withSystemPrompt(input.Config, input.Prompt),
N: 1,
ResponseFormat: "url",
ResponseFormat: grokImageResponseFormat(input.ImageCapability),
// Grok 图片协议用 aspect_ratio 表达画布比例;同时发送 size 会被上游按 OpenAI 枚举校验并拒绝。
AspectRatio: normalizeGrokImageAspectRatio(input.Config.Size),
Resolution: normalizeGrokImageResolution(input.Config.Quality),
@@ -322,6 +322,13 @@ func grokImageRequestBody(input canvasGenerationInput) (grokImageRequest, string
return body, "/images/edits", nil
}
func grokImageResponseFormat(profile *ImageCapabilityConfig) string {
if imageParameterSupported(profile, "response_format") {
return "b64_json"
}
return "url"
}
// normalizeGrokImageResolution 把画布 quality(1k/2k/high…)映射为 grok2api / xAI 的 resolution。
func normalizeGrokImageResolution(quality string) string {
raw := strings.ToLower(strings.TrimSpace(quality))

View File

@@ -91,6 +91,14 @@ func protocolRequestFromInput(input canvasGenerationInput) protocol.GenerationRe
}
}
}
if input.Mode == "image" && input.Config.InterfaceType == string(model.ChannelInterfaceGrokImage) {
options := make(map[string]any)
for name, value := range request.ProviderOptions[input.Config.InterfaceType] {
options[name] = value
}
options["response_format"] = grokImageResponseFormat(input.ImageCapability)
request.ProviderOptions[input.Config.InterfaceType] = options
}
return request
}