Skip to content

Commit dcea483

Browse files
authored
Feat: Add Deepl support for plugins/ai-proxy (alibaba#1147)
1 parent 8fa1224 commit dcea483

File tree

4 files changed

+255
-8
lines changed

4 files changed

+255
-8
lines changed

plugins/wasm-go/extensions/ai-proxy/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ Gemini 所对应的 `type` 为 `gemini`。它特有的配置字段如下:
165165
| --------------------- | -------- | -------- |-----|-------------------------------------------------------------------------------------------------|
166166
| `geminiSafetySetting` | map of string | 非必填 | - | Gemini AI内容过滤和安全级别设定。参考[Safety settings](https://ai.google.dev/gemini-api/docs/safety-settings) |
167167

168+
#### DeepL
169+
170+
DeepL 所对应的 `type``deepl`。它特有的配置字段如下:
171+
172+
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
173+
| ------------ | -------- | -------- | ------ | ---------------------------- |
174+
| `targetLang` | string | 必填 | - | DeepL 翻译服务需要的目标语种 |
175+
168176
## 用法示例
169177

170178
### 使用 OpenAI 协议代理 Azure OpenAI 服务
@@ -1008,6 +1016,59 @@ provider:
10081016
}
10091017
```
10101018

1019+
### 使用 OpenAI 协议代理 DeepL 文本翻译服务
1020+
1021+
**配置信息**
1022+
1023+
```yaml
1024+
provider:
1025+
type: deepl
1026+
apiTokens:
1027+
- "YOUR_DEEPL_API_TOKEN"
1028+
targetLang: "ZH"
1029+
```
1030+
1031+
**请求示例**
1032+
此处 `model` 表示 DeepL 的服务类型,只能填 `Free` 或 `Pro`。`content` 中设置需要翻译的文本;在 `role: system` 的 `content` 中可以包含可能影响翻译但本身不会被翻译的上下文,例如翻译产品名称时,可以将产品描述作为上下文传递,这种额外的上下文可能会提高翻译的质量。
1033+
1034+
```json
1035+
{
1036+
"model": "Free",
1037+
"messages": [
1038+
{
1039+
"role": "system",
1040+
"content": "money"
1041+
},
1042+
{
1043+
"content": "sit by the bank"
1044+
},
1045+
{
1046+
"content": "a bank in China"
1047+
}
1048+
]
1049+
}
1050+
```
1051+
1052+
**响应示例**
1053+
```json
1054+
{
1055+
"choices": [
1056+
{
1057+
"index": 0,
1058+
"message": { "name": "EN", "role": "assistant", "content": "坐庄" }
1059+
},
1060+
{
1061+
"index": 1,
1062+
"message": { "name": "EN", "role": "assistant", "content": "中国银行" }
1063+
}
1064+
],
1065+
"created": 1722747752,
1066+
"model": "Free",
1067+
"object": "chat.completion",
1068+
"usage": {}
1069+
}
1070+
```
1071+
10111072
## 完整配置示例
10121073

10131074
### Kubernetes 示例

plugins/wasm-go/extensions/ai-proxy/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,18 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, pluginConfig config.PluginCo
140140
return types.ActionContinue
141141
}
142142

143-
contentType, err := proxywasm.GetHttpResponseHeader("Content-Type")
144-
if err != nil || !strings.HasPrefix(contentType, "text/event-stream") {
145-
if err != nil {
146-
log.Errorf("unable to load content-type header from response: %v", err)
147-
}
148-
ctx.BufferResponseBody()
149-
}
150-
151143
if handler, ok := activeProvider.(provider.ResponseHeadersHandler); ok {
152144
apiName, _ := ctx.GetContext(ctxKeyApiName).(provider.ApiName)
153145
action, err := handler.OnResponseHeaders(ctx, apiName, log)
154146
if err == nil {
147+
checkStream(&ctx, &log)
155148
return action
156149
}
157150
_ = util.SendResponse(500, "ai-proxy.proc_resp_headers_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process response headers: %v", err))
158151
return types.ActionContinue
159152
}
160153

154+
checkStream(&ctx, &log)
161155
_, needHandleBody := activeProvider.(provider.ResponseBodyHandler)
162156
_, needHandleStreamingBody := activeProvider.(provider.StreamingResponseBodyHandler)
163157
if !needHandleBody && !needHandleStreamingBody {
@@ -223,3 +217,13 @@ func getOpenAiApiName(path string) provider.ApiName {
223217
}
224218
return ""
225219
}
220+
221+
func checkStream(ctx *wrapper.HttpContext, log *wrapper.Log) {
222+
contentType, err := proxywasm.GetHttpResponseHeader("Content-Type")
223+
if err != nil || !strings.HasPrefix(contentType, "text/event-stream") {
224+
if err != nil {
225+
log.Errorf("unable to load content-type header from response: %v", err)
226+
}
227+
(*ctx).BufferResponseBody()
228+
}
229+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package provider
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"time"
8+
9+
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util"
10+
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
11+
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
12+
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
13+
)
14+
15+
// deeplProvider is the provider for DeepL service.
16+
const (
17+
deeplHostPro = "api.deepl.com"
18+
deeplHostFree = "api-free.deepl.com"
19+
deeplChatCompletionPath = "/v2/translate"
20+
)
21+
22+
type deeplProviderInitializer struct {
23+
}
24+
25+
type deeplProvider struct {
26+
config ProviderConfig
27+
contextCache *contextCache
28+
}
29+
30+
// spec reference: https://developers.deepl.com/docs/v/zh/api-reference/translate/openapi-spec-for-text-translation
31+
type deeplRequest struct {
32+
// "Model" parameter is used to distinguish which service to use
33+
Model string `json:"model,omitempty"`
34+
Text []string `json:"text"`
35+
SourceLang string `json:"source_lang,omitempty"`
36+
TargetLang string `json:"target_lang"`
37+
Context string `json:"context,omitempty"`
38+
SplitSentences string `json:"split_sentences,omitempty"`
39+
PreserveFormatting bool `json:"preserve_formatting,omitempty"`
40+
Formality string `json:"formality,omitempty"`
41+
GlossaryId string `json:"glossary_id,omitempty"`
42+
TagHandling string `json:"tag_handling,omitempty"`
43+
OutlineDetection bool `json:"outline_detection,omitempty"`
44+
NonSplittingTags []string `json:"non_splitting_tags,omitempty"`
45+
SplittingTags []string `json:"splitting_tags,omitempty"`
46+
IgnoreTags []string `json:"ignore_tags,omitempty"`
47+
}
48+
49+
type deeplResponse struct {
50+
Translations []deeplResponseTranslation `json:"translations,omitempty"`
51+
Message string `json:"message,omitempty"`
52+
}
53+
54+
type deeplResponseTranslation struct {
55+
DetectedSourceLanguage string `json:"detected_source_language"`
56+
Text string `json:"text"`
57+
}
58+
59+
func (d *deeplProviderInitializer) ValidateConfig(config ProviderConfig) error {
60+
if config.targetLang == "" {
61+
return errors.New("missing targetLang in deepl provider config")
62+
}
63+
return nil
64+
}
65+
66+
func (d *deeplProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) {
67+
return &deeplProvider{
68+
config: config,
69+
contextCache: createContextCache(&config),
70+
}, nil
71+
}
72+
73+
func (d *deeplProvider) GetProviderType() string {
74+
return providerTypeDeepl
75+
}
76+
77+
func (d *deeplProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) (types.Action, error) {
78+
if apiName != ApiNameChatCompletion {
79+
return types.ActionContinue, errUnsupportedApiName
80+
}
81+
_ = util.OverwriteRequestPath(deeplChatCompletionPath)
82+
_ = util.OverwriteRequestAuthorization("DeepL-Auth-Key " + d.config.GetRandomToken())
83+
_ = proxywasm.RemoveHttpRequestHeader("Content-Length")
84+
_ = proxywasm.RemoveHttpRequestHeader("Accept-Encoding")
85+
return types.HeaderStopIteration, nil
86+
}
87+
88+
func (d *deeplProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) (types.Action, error) {
89+
if apiName != ApiNameChatCompletion {
90+
return types.ActionContinue, errUnsupportedApiName
91+
}
92+
if d.config.protocol == protocolOriginal {
93+
request := &deeplRequest{}
94+
if err := json.Unmarshal(body, request); err != nil {
95+
return types.ActionContinue, fmt.Errorf("unable to unmarshal request: %v", err)
96+
}
97+
if err := d.overwriteRequestHost(request.Model); err != nil {
98+
return types.ActionContinue, err
99+
}
100+
ctx.SetContext(ctxKeyFinalRequestModel, request.Model)
101+
return types.ActionContinue, replaceJsonRequestBody(request, log)
102+
} else {
103+
originRequest := &chatCompletionRequest{}
104+
if err := decodeChatCompletionRequest(body, originRequest); err != nil {
105+
return types.ActionContinue, err
106+
}
107+
if err := d.overwriteRequestHost(originRequest.Model); err != nil {
108+
return types.ActionContinue, err
109+
}
110+
ctx.SetContext(ctxKeyFinalRequestModel, originRequest.Model)
111+
deeplRequest := &deeplRequest{
112+
Text: make([]string, 0),
113+
TargetLang: d.config.targetLang,
114+
}
115+
for _, msg := range originRequest.Messages {
116+
if msg.Role == roleSystem {
117+
deeplRequest.Context = msg.Content
118+
} else {
119+
deeplRequest.Text = append(deeplRequest.Text, msg.Content)
120+
}
121+
}
122+
return types.ActionContinue, replaceJsonRequestBody(deeplRequest, log)
123+
}
124+
}
125+
126+
func (d *deeplProvider) OnResponseHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) (types.Action, error) {
127+
_ = proxywasm.RemoveHttpResponseHeader("Content-Length")
128+
return types.ActionContinue, nil
129+
}
130+
131+
func (d *deeplProvider) OnResponseBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) (types.Action, error) {
132+
deeplResponse := &deeplResponse{}
133+
if err := json.Unmarshal(body, deeplResponse); err != nil {
134+
return types.ActionContinue, fmt.Errorf("unable to unmarshal deepl response: %v", err)
135+
}
136+
response := d.responseDeepl2OpenAI(ctx, deeplResponse)
137+
return types.ActionContinue, replaceJsonResponseBody(response, log)
138+
}
139+
140+
func (d *deeplProvider) responseDeepl2OpenAI(ctx wrapper.HttpContext, deeplResponse *deeplResponse) *chatCompletionResponse {
141+
var choices []chatCompletionChoice
142+
// Fail
143+
if deeplResponse.Message != "" {
144+
choices = make([]chatCompletionChoice, 1)
145+
choices[0] = chatCompletionChoice{
146+
Message: &chatMessage{Role: roleAssistant, Content: deeplResponse.Message},
147+
Index: 0,
148+
}
149+
} else {
150+
// Success
151+
choices = make([]chatCompletionChoice, len(deeplResponse.Translations))
152+
for idx, t := range deeplResponse.Translations {
153+
choices[idx] = chatCompletionChoice{
154+
Index: idx,
155+
Message: &chatMessage{Role: roleAssistant, Content: t.Text, Name: t.DetectedSourceLanguage},
156+
}
157+
}
158+
}
159+
return &chatCompletionResponse{
160+
Created: time.Now().UnixMilli() / 1000,
161+
Object: objectChatCompletion,
162+
Choices: choices,
163+
Model: ctx.GetStringContext(ctxKeyFinalRequestModel, ""),
164+
}
165+
}
166+
167+
func (d *deeplProvider) overwriteRequestHost(model string) error {
168+
if model == "Pro" {
169+
_ = util.OverwriteRequestHost(deeplHostPro)
170+
} else if model == "Free" {
171+
_ = util.OverwriteRequestHost(deeplHostFree)
172+
} else {
173+
return errors.New(`deepl model should be "Free" or "Pro"`)
174+
}
175+
return nil
176+
}

plugins/wasm-go/extensions/ai-proxy/provider/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
providerTypeCloudflare = "cloudflare"
3636
providerTypeSpark = "spark"
3737
providerTypeGemini = "gemini"
38+
providerTypeDeepl = "deepl"
3839

3940
protocolOpenAI = "openai"
4041
protocolOriginal = "original"
@@ -88,6 +89,7 @@ var (
8889
providerTypeCloudflare: &cloudflareProviderInitializer{},
8990
providerTypeSpark: &sparkProviderInitializer{},
9091
providerTypeGemini: &geminiProviderInitializer{},
92+
providerTypeDeepl: &deeplProviderInitializer{},
9193
}
9294
)
9395

@@ -176,6 +178,9 @@ type ProviderConfig struct {
176178
// @Title zh-CN Gemini AI内容过滤和安全级别设定
177179
// @Description zh-CN 仅适用于 Gemini AI 服务。参考:https://ai.google.dev/gemini-api/docs/safety-settings
178180
geminiSafetySetting map[string]string `required:"false" yaml:"geminiSafetySetting" json:"geminiSafetySetting"`
181+
// @Title zh-CN 翻译服务需指定的目标语种
182+
// @Description zh-CN 翻译结果的语种,目前仅适用于DeepL服务。
183+
targetLang string `required:"false" yaml:"targetLang" json:"targetLang"`
179184
}
180185

181186
func (c *ProviderConfig) FromJson(json gjson.Result) {
@@ -223,6 +228,7 @@ func (c *ProviderConfig) FromJson(json gjson.Result) {
223228
c.geminiSafetySetting[k] = v.String()
224229
}
225230
}
231+
c.targetLang = json.Get("targetLang").String()
226232
}
227233

228234
func (c *ProviderConfig) Validate() error {

0 commit comments

Comments
 (0)