diff --git a/README.md b/README.md index 8f5d9c7..0ca0dbb 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ func main() { Params: wanx.ImageSynthesisParams{ N: 1, }, + Download: true // 从 URL 下载图片 } ctx := context.TODO() @@ -94,6 +95,8 @@ func main() { } for _, blob := range imgBlobs { + // blob.Data 会在 request 中设置了 Download: true 时下载 + // 否则使用 blob.ImgURL saveImg2Desktop(blob.ImgType, blob.Data) } } diff --git a/dtypes.go b/dtypes.go new file mode 100644 index 0000000..eb21337 --- /dev/null +++ b/dtypes.go @@ -0,0 +1,5 @@ +package dashscopego + +const ( + DashscopeTokenEnvName = "DASHSCOPE_API_KEY" //nolint:gosec +) diff --git a/tongyiclient_test.go b/tongyiclient_test.go index 0f6871d..782a69b 100644 --- a/tongyiclient_test.go +++ b/tongyiclient_test.go @@ -193,6 +193,7 @@ func TestImageGeneration(t *testing.T) { Input: wanx.ImageSynthesisInput{ Prompt: "A beautiful sunset", }, + Download: true, } imgBlobs, err := cli.CreateImageGeneration(ctx, req) @@ -201,6 +202,7 @@ func TestImageGeneration(t *testing.T) { for _, blob := range imgBlobs { assert.NotEmpty(t, blob.Data) + assert.NotEmpty(t, blob.ImgURL) assert.Equal(t, "image/png", blob.ImgType) } } diff --git a/wanx/wanx_dtypes.go b/wanx/wanx_dtypes.go index 5439a8c..dc70004 100644 --- a/wanx/wanx_dtypes.go +++ b/wanx/wanx_dtypes.go @@ -42,9 +42,10 @@ type ImageSynthesisInput struct { } type ImageSynthesisRequest struct { - Model string `json:"model"` - Input ImageSynthesisInput `json:"input"` - Params ImageSynthesisParams `json:"parameters"` + Model string `json:"model"` + Input ImageSynthesisInput `json:"input"` + Params ImageSynthesisParams `json:"parameters"` + Download bool `json:"-"` } type Output struct { @@ -76,6 +77,7 @@ type ImageResponse struct { type ImgBlob struct { // types include: "image/png". ImgType string + ImgURL string // Raw bytes for media formats. Data []byte } diff --git a/wanx/wanxcli.go b/wanx/wanxcli.go index 59bfc68..250952e 100644 --- a/wanx/wanxcli.go +++ b/wanx/wanxcli.go @@ -31,12 +31,21 @@ func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest, blobList := make([]*ImgBlob, 0, len(resp.Results)) for _, img := range resp.Results { - imgByte, err := httpcli.GetImage(ctx, img.URL, tokenOpt) - if err != nil { - return nil, err + tmpImgBlob := &ImgBlob{ + ImgType: "image/png", + ImgURL: img.URL, + } + + if payload.Download { + imgByte, err := httpcli.GetImage(ctx, img.URL, tokenOpt) + if err != nil { + return nil, err + } + + tmpImgBlob.Data = imgByte } - blobList = append(blobList, &ImgBlob{Data: imgByte, ImgType: "image/png"}) + blobList = append(blobList, tmpImgBlob) } return blobList, nil