feat(ai-proxy): add responses support for doubao (#2509)

Signed-off-by: Xijun Dai <daixijun1990@gmail.com>
This commit is contained in:
Xijun Dai
2025-06-28 10:39:21 +08:00
committed by GitHub
parent ef49d2f5f6
commit bb786c9618
2 changed files with 37 additions and 0 deletions

View File

@@ -406,6 +406,9 @@ func getApiName(path string) provider.ApiName {
if util.RegDeleteFineTuningCheckpointPermissionPath.MatchString(path) {
return provider.ApiNameDeleteFineTuningCheckpointPermission
}
if strings.HasSuffix(path, "/v1/responses") {
return provider.ApiNameResponses
}
// cohere style
if strings.HasSuffix(path, "/v1/rerank") {
return provider.ApiNameCohereV1Rerank

View File

@@ -6,8 +6,11 @@ import (
"strings"
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util"
"github.com/alibaba/higress/plugins/wasm-go/pkg/log"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
const (
@@ -15,6 +18,7 @@ const (
doubaoChatCompletionPath = "/api/v3/chat/completions"
doubaoEmbeddingsPath = "/api/v3/embeddings"
doubaoImageGenerationPath = "/api/v3/images/generations"
doubaoResponsesPath = "/api/v3/responses"
)
type doubaoProviderInitializer struct{}
@@ -31,6 +35,7 @@ func (m *doubaoProviderInitializer) DefaultCapabilities() map[string]string {
string(ApiNameChatCompletion): doubaoChatCompletionPath,
string(ApiNameEmbeddings): doubaoEmbeddingsPath,
string(ApiNameImageGeneration): doubaoImageGenerationPath,
string(ApiNameResponses): doubaoResponsesPath,
}
}
@@ -70,6 +75,32 @@ func (m *doubaoProvider) TransformRequestHeaders(ctx wrapper.HttpContext, apiNam
headers.Del("Content-Length")
}
func (m *doubaoProvider) TransformRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte) ([]byte, error) {
var err error
switch apiName {
case ApiNameResponses:
// 移除火山 responses 接口暂时不支持的参数
// 参考: https://www.volcengine.com/docs/82379/1569618
// TODO: 这里应该用 DTO 处理
for _, param := range []string{"parallel_tool_calls", "tool_choice", "stream_options"} {
body, err = sjson.DeleteBytes(body, param)
if err != nil {
log.Warnf("[doubao] failed to delete %s in request body, err: %v", param, err)
}
}
case ApiNameImageGeneration:
// 火山生图接口默认会带上水印,但 OpenAI 接口不支持此参数
// 参考: https://www.volcengine.com/docs/82379/1541523
if res := gjson.GetBytes(body, "watermark"); !res.Exists() {
body, err = sjson.SetBytes(body, "watermark", false)
if err != nil {
log.Warnf("[doubao] failed to set watermark in request body, err: %v", err)
}
}
}
return m.config.defaultTransformRequestBody(ctx, apiName, body)
}
func (m *doubaoProvider) GetApiName(path string) ApiName {
if strings.Contains(path, doubaoChatCompletionPath) {
return ApiNameChatCompletion
@@ -80,5 +111,8 @@ func (m *doubaoProvider) GetApiName(path string) ApiName {
if strings.Contains(path, doubaoImageGenerationPath) {
return ApiNameImageGeneration
}
if strings.Contains(path, doubaoResponsesPath) {
return ApiNameResponses
}
return ""
}