Skip to content

Commit

Permalink
support international baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
devinyf committed Jul 30, 2024
1 parent c61e42d commit e492fb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
13 changes: 7 additions & 6 deletions qwen/qwen_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qwen

const (
DashScopeBaseURL = "https://dashscope.aliyuncs.com/api"
DashScopeIntlBaseURL = "https://dashscope-intl.aliyuncs.com/api"
QwenSubURL = "/v1/services/aigc/text-generation/generation"
QwenVLSubURL = "/v1/services/aigc/multimodal-generation/generation"
QwenAudioSubURL = QwenVLSubURL
Expand All @@ -25,17 +26,17 @@ const (
)

// text-generation only.
func URLQwen() string {
return DashScopeBaseURL + QwenSubURL
func URLQwen(baseURL string) string {
return baseURL + QwenSubURL
}

// multimodal.
func URLQwenVL() string {
return DashScopeBaseURL + QwenVLSubURL
func URLQwenVL(baseURL string) string {
return baseURL + QwenVLSubURL
}

func URLQwenAudio() string {
return DashScopeBaseURL + QwenAudioSubURL
func URLQwenAudio(baseURL string) string {
return baseURL + QwenAudioSubURL
}

type RoleType = string
Expand Down
21 changes: 15 additions & 6 deletions tongyiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,28 @@ type TongyiClient struct {
httpCli httpclient.IHttpClient
wsCli *httpclient.WsClient // only used for paraformer realtime speech to text.
uploadCache qwen.UploadCacher
baseUrl string
}

func NewTongyiClient(model string, token string) *TongyiClient {
httpcli := httpclient.NewHTTPClient()
return newTongyiCLientWithHTTPCli(model, token, httpcli)
baseURL := qwen.DashScopeBaseURL
return newTongyiCLientWithHTTPCli(baseURL, model, token, httpcli)
}

func newTongyiCLientWithHTTPCli(model string, token string, httpcli httpclient.IHttpClient) *TongyiClient {
func NewTongyiClientIntl(model string, token string) *TongyiClient {
httpcli := httpclient.NewHTTPClient()
baseURL := qwen.DashScopeIntlBaseURL
return newTongyiCLientWithHTTPCli(baseURL, model, token, httpcli)
}

func newTongyiCLientWithHTTPCli(baseURL, model, token string, httpcli httpclient.IHttpClient) *TongyiClient {
return &TongyiClient{
Model: model,
httpCli: httpcli,
token: token,
uploadCache: qwen.NewMemoryFileCache(),
baseUrl: baseURL,
}
}

Expand All @@ -48,7 +57,7 @@ func (q *TongyiClient) SetUploadCache(uploadCache qwen.UploadCacher) *TongyiClie
// nolint:lll
func (q *TongyiClient) CreateCompletion(ctx context.Context, payload *qwen.Request[*qwen.TextContent]) (*TextQwenResponse, error) {
payload = payloadPreCheck(q, payload)
return genericCompletion[*qwen.TextContent, *qwen.TextContent](ctx, payload, q.httpCli, qwen.URLQwen(), q.token)
return genericCompletion[*qwen.TextContent, *qwen.TextContent](ctx, payload, q.httpCli, qwen.URLQwen(q.baseUrl), q.token)
}

//nolint:lll
Expand All @@ -71,7 +80,7 @@ func (q *TongyiClient) CreateVLCompletion(ctx context.Context, payload *qwen.Req
}
}

return genericCompletion[*qwen.VLContentList, *qwen.VLContentList](ctx, payload, q.httpCli, qwen.URLQwenVL(), q.token)
return genericCompletion[*qwen.VLContentList, *qwen.VLContentList](ctx, payload, q.httpCli, qwen.URLQwenVL(q.baseUrl), q.token)
}

//nolint:lll
Expand All @@ -95,7 +104,7 @@ func (q *TongyiClient) CreateAudioCompletion(ctx context.Context, payload *qwen.
}
}

return genericCompletion[*qwen.AudioContentList, *qwen.AudioContentList](ctx, payload, q.httpCli, qwen.URLQwenVL(), q.token)
return genericCompletion[*qwen.AudioContentList, *qwen.AudioContentList](ctx, payload, q.httpCli, qwen.URLQwenAudio(q.baseUrl), q.token)
}

// used for pdf_extracter plugin.
Expand All @@ -120,7 +129,7 @@ func (q *TongyiClient) CreateFileCompletion(ctx context.Context, payload *qwen.R
}
}

return genericCompletion[*qwen.FileContentList, *qwen.TextContent](ctx, payload, q.httpCli, qwen.URLQwen(), q.token)
return genericCompletion[*qwen.FileContentList, *qwen.TextContent](ctx, payload, q.httpCli, qwen.URLQwen(q.baseUrl), q.token)
}

func checkIfNeedUploadFile(ctx context.Context, filepath string, model, token string, uploadCacher qwen.UploadCacher) (string, bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion tongyiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newMockClient(t *testing.T, model string, ctrl *gomock.Controller, f mockFn

f(mockHTTPCli)

qwenCli := newTongyiCLientWithHTTPCli(model, fackToken, mockHTTPCli)
qwenCli := newTongyiCLientWithHTTPCli(qwen.DashScopeBaseURL, model, fackToken, mockHTTPCli)
return qwenCli
}

Expand Down

0 comments on commit e492fb2

Please sign in to comment.