From 32f3a1149a036cced210e699b0d925e63a98cdc8 Mon Sep 17 00:00:00 2001 From: "devin.yf" Date: Tue, 30 Jul 2024 16:19:05 +0800 Subject: [PATCH] wanx support international base url --- tongyiclient.go | 2 +- wanx/wanx_params.go | 13 ++++++------- wanx/wanxcli.go | 18 +++++++++--------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/tongyiclient.go b/tongyiclient.go index 0a7572d..08edf60 100644 --- a/tongyiclient.go +++ b/tongyiclient.go @@ -184,7 +184,7 @@ func (q *TongyiClient) CreateImageGeneration(ctx context.Context, payload *wanx. } payload.Model = q.Model } - return wanx.CreateImageGeneration(ctx, payload, q.httpCli, q.token) + return wanx.CreateImageGeneration(ctx, q.baseUrl, payload, q.httpCli, q.token) } // voice file to text. diff --git a/wanx/wanx_params.go b/wanx/wanx_params.go index 06806a8..f697ee7 100644 --- a/wanx/wanx_params.go +++ b/wanx/wanx_params.go @@ -3,9 +3,8 @@ package wanx import "fmt" const ( - DashScopeBaseURL = "https://dashscope.aliyuncs.com" - ImageSynthesisURI = "/api/v1/services/aigc/text2image/image-synthesis" - TaskURI = "/api/v1/tasks/%s" + ImageSynthesisURI = "/v1/services/aigc/text2image/image-synthesis" + TaskURI = "/v1/tasks/%s" ) type ModelWanx = string @@ -16,10 +15,10 @@ const ( WanxBgGenV2 ModelWanx = "wanx-background-generation-v2" ) -func ImageSynthesisURL() string { - return DashScopeBaseURL + ImageSynthesisURI +func ImageSynthesisURL(baseURL string) string { + return baseURL + ImageSynthesisURI } -func TaskURL(taskID string) string { - return DashScopeBaseURL + fmt.Sprintf(TaskURI, taskID) +func TaskURL(baseURL, taskID string) string { + return baseURL + fmt.Sprintf(TaskURI, taskID) } diff --git a/wanx/wanxcli.go b/wanx/wanxcli.go index bd6690c..bdd029e 100644 --- a/wanx/wanxcli.go +++ b/wanx/wanxcli.go @@ -17,14 +17,14 @@ var ( ) //nolint:lll -func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest, httpcli httpclient.IHttpClient, token string) ([]*ImgBlob, error) { +func CreateImageGeneration(ctx context.Context, baseURL string, payload *ImageSynthesisRequest, httpcli httpclient.IHttpClient, token string) ([]*ImgBlob, error) { header := map[string]string{ "Content-Type": "application/json", } tokenOpt := httpclient.WithTokenHeaderOption(token) headerOpt := httpclient.WithHeader(header) - resp, err := SyncCall(ctx, payload, httpcli, tokenOpt, headerOpt) + resp, err := SyncCall(ctx, baseURL, payload, httpcli, tokenOpt, headerOpt) if err != nil { return nil, err } @@ -54,8 +54,8 @@ func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest, // tongyi-wanx-api only support AsyncCall, so we need to warp it to be Sync. // //nolint:lll -func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*Output, error) { - rsp, err := AsyncCall(ctx, req, httpcli, options...) +func SyncCall(ctx context.Context, baseURL string, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*Output, error) { + rsp, err := AsyncCall(ctx, baseURL, req, httpcli, options...) if err != nil { return nil, err } @@ -78,7 +78,7 @@ func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclien time.Sleep(time.Duration(delayDurationToCheckStatus) * time.Millisecond) // log.Println("TaskStatus: ", currentTaskStatus) - taskResp, err = CheckTaskStatus(ctx, &taskReq, httpcli, options...) + taskResp, err = CheckTaskStatus(ctx, baseURL, &taskReq, httpcli, options...) if err != nil { return nil, err } @@ -100,7 +100,7 @@ func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclien // calling tongyi-wanx-api to get image-generation async task id. // //nolint:lll -func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*ImageResponse, error) { +func AsyncCall(ctx context.Context, baseURL string, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*ImageResponse, error) { header := map[string]string{"X-DashScope-Async": "enable"} headerOpt := httpclient.WithHeader(header) options = append(options, headerOpt) @@ -110,7 +110,7 @@ func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclie } resp := ImageResponse{} - err := httpcli.Post(ctx, ImageSynthesisURL(), req, &resp, options...) + err := httpcli.Post(ctx, ImageSynthesisURL(baseURL), req, &resp, options...) if err != nil { return nil, err } @@ -119,9 +119,9 @@ func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclie } //nolint:lll -func CheckTaskStatus(ctx context.Context, req *TaskRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*TaskResponse, error) { +func CheckTaskStatus(ctx context.Context, baseURL string, req *TaskRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*TaskResponse, error) { resp := TaskResponse{} - err := httpcli.Get(ctx, TaskURL(req.TaskID), nil, &resp, options...) + err := httpcli.Get(ctx, TaskURL(baseURL, req.TaskID), nil, &resp, options...) if err != nil { return nil, err }