fix(inference): 修正文生图 API 调用参数,支持动态分辨率
- 去掉 response_format 参数(API 默认返回 URL,自动下载) - Resolution 参数覆盖 API 调用尺寸(不再仅 mock 路径生效) - parseImageResponse 改为动态 width/height 入参 - 新增 tools/gentest.go 生成测试脚本 - .gitignore 忽略 test_output/
This commit is contained in:
@@ -36,7 +36,6 @@ type imageGenRequest struct {
|
||||
Prompt string `json:"prompt"`
|
||||
N int `json:"n,omitempty"`
|
||||
Size string `json:"size,omitempty"`
|
||||
ResponseFormat string `json:"response_format,omitempty"`
|
||||
Steps int `json:"steps,omitempty"`
|
||||
CFGScale float64 `json:"cfg_scale,omitempty"`
|
||||
}
|
||||
@@ -57,7 +56,12 @@ func GenerateImages(ctx context.Context, prompt string, params AssetParams) ([]G
|
||||
}
|
||||
|
||||
if imgCfg.APIKey != "" {
|
||||
return callImageGenAPI(ctx, prompt, count)
|
||||
width, height := imgCfg.Width, imgCfg.Height
|
||||
if params.Resolution > 0 {
|
||||
width = params.Resolution
|
||||
height = params.Resolution
|
||||
}
|
||||
return callImageGenAPI(ctx, prompt, count, width, height)
|
||||
}
|
||||
|
||||
size := params.Resolution
|
||||
@@ -69,13 +73,12 @@ func GenerateImages(ctx context.Context, prompt string, params AssetParams) ([]G
|
||||
}
|
||||
|
||||
// callImageGenAPI 调用 OpenAI 兼容的 Images API,返回生成的图片。
|
||||
func callImageGenAPI(ctx context.Context, prompt string, count int) ([]GeneratedImage, error) {
|
||||
func callImageGenAPI(ctx context.Context, prompt string, count, width, height int) ([]GeneratedImage, error) {
|
||||
reqBody := imageGenRequest{
|
||||
Model: imgCfg.Model,
|
||||
Prompt: prompt,
|
||||
N: count,
|
||||
Size: fmt.Sprintf("%dx%d", imgCfg.Width, imgCfg.Height),
|
||||
ResponseFormat: "b64_json",
|
||||
Size: fmt.Sprintf("%dx%d", width, height),
|
||||
}
|
||||
if imgCfg.Steps > 0 {
|
||||
reqBody.Steps = imgCfg.Steps
|
||||
@@ -108,7 +111,7 @@ func callImageGenAPI(ctx context.Context, prompt string, count int) ([]Generated
|
||||
return nil, fmt.Errorf("image gen api error %d: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
|
||||
return parseImageResponse(ctx, resp.Body)
|
||||
return parseImageResponse(ctx, resp.Body, width, height)
|
||||
}
|
||||
|
||||
// ======================== 图片编辑 API ========================
|
||||
@@ -138,7 +141,6 @@ func callImageEditAPI(ctx context.Context, imageData []byte, prompt string, coun
|
||||
writer.WriteField("model", imgCfg.Model)
|
||||
writer.WriteField("n", strconv.Itoa(count))
|
||||
writer.WriteField("size", fmt.Sprintf("%dx%d", imgCfg.Width, imgCfg.Height))
|
||||
writer.WriteField("response_format", "b64_json")
|
||||
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, fmt.Errorf("close multipart writer: %w", err)
|
||||
@@ -164,11 +166,11 @@ func callImageEditAPI(ctx context.Context, imageData []byte, prompt string, coun
|
||||
return nil, fmt.Errorf("image edit api error %d: %s", resp.StatusCode, string(b))
|
||||
}
|
||||
|
||||
return parseImageResponse(ctx, resp.Body)
|
||||
return parseImageResponse(ctx, resp.Body, imgCfg.Width, imgCfg.Height)
|
||||
}
|
||||
|
||||
// parseImageResponse 解析 OpenAI 兼容的图片生成/编辑响应体。
|
||||
func parseImageResponse(ctx context.Context, r io.Reader) ([]GeneratedImage, error) {
|
||||
func parseImageResponse(ctx context.Context, r io.Reader, width, height int) ([]GeneratedImage, error) {
|
||||
var genResp imageGenResponse
|
||||
if err := json.NewDecoder(r).Decode(&genResp); err != nil {
|
||||
return nil, fmt.Errorf("decode response: %w", err)
|
||||
@@ -195,8 +197,8 @@ func parseImageResponse(ctx context.Context, r io.Reader) ([]GeneratedImage, err
|
||||
}
|
||||
images = append(images, GeneratedImage{
|
||||
Data: data,
|
||||
Width: imgCfg.Width,
|
||||
Height: imgCfg.Height,
|
||||
Width: width,
|
||||
Height: height,
|
||||
Format: "png",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user