fix: Fix bugs in the bedrock model name escaping logic (#2663)

Co-authored-by: 澄潭 <zty98751@alibaba-inc.com>
This commit is contained in:
Kent Dong
2025-08-15 17:40:13 +08:00
committed by GitHub
parent be603af461
commit 24dca0455e
2 changed files with 14 additions and 16 deletions

View File

@@ -22,8 +22,6 @@ import (
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/higress-group/wasm-go/pkg/log"
"github.com/higress-group/wasm-go/pkg/wrapper"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
const (
@@ -591,11 +589,6 @@ func (b *bedrockProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName
}
func (b *bedrockProvider) TransformRequestBodyHeaders(ctx wrapper.HttpContext, apiName ApiName, body []byte, headers http.Header) ([]byte, error) {
if gjson.GetBytes(body, "model").Exists() {
rawModel := gjson.GetBytes(body, "model").String()
encodedModel := url.QueryEscape(rawModel)
body, _ = sjson.SetBytes(body, "model", encodedModel)
}
switch apiName {
case ApiNameChatCompletion:
return b.onChatCompletionRequestBody(ctx, body, headers)
@@ -633,7 +626,7 @@ func (b *bedrockProvider) onImageGenerationRequestBody(ctx wrapper.HttpContext,
return nil, err
}
headers.Set("Accept", "*/*")
util.OverwriteRequestPathHeader(headers, fmt.Sprintf(bedrockInvokeModelPath, request.Model))
b.overwriteRequestPathHeader(headers, bedrockInvokeModelPath, request.Model)
return b.buildBedrockImageGenerationRequest(request, headers)
}
@@ -657,7 +650,6 @@ func (b *bedrockProvider) buildBedrockImageGenerationRequest(origRequest *imageG
Quality: origRequest.Quality,
},
}
util.OverwriteRequestPathHeader(headers, fmt.Sprintf(bedrockInvokeModelPath, origRequest.Model))
requestBytes, err := json.Marshal(request)
b.setAuthHeaders(requestBytes, headers)
return requestBytes, err
@@ -696,9 +688,9 @@ func (b *bedrockProvider) onChatCompletionRequestBody(ctx wrapper.HttpContext, b
streaming := request.Stream
headers.Set("Accept", "*/*")
if streaming {
util.OverwriteRequestPathHeader(headers, fmt.Sprintf(bedrockStreamChatCompletionPath, request.Model))
b.overwriteRequestPathHeader(headers, bedrockStreamChatCompletionPath, request.Model)
} else {
util.OverwriteRequestPathHeader(headers, fmt.Sprintf(bedrockChatCompletionPath, request.Model))
b.overwriteRequestPathHeader(headers, bedrockChatCompletionPath, request.Model)
}
return b.buildBedrockTextGenerationRequest(request, headers)
}
@@ -770,6 +762,17 @@ func (b *bedrockProvider) buildChatCompletionResponse(ctx wrapper.HttpContext, b
}
}
func (b *bedrockProvider) overwriteRequestPathHeader(headers http.Header, format, model string) {
modelInPath := model
// Just in case the model name has already been URL-escaped, we shouldn't escape it again.
if !strings.ContainsRune(model, '%') {
modelInPath = url.QueryEscape(model)
}
path := fmt.Sprintf(format, modelInPath)
log.Debugf("overwriting bedrock request path: %s", path)
util.OverwriteRequestPathHeader(headers, path)
}
func stopReasonBedrock2OpenAI(reason string) string {
switch reason {
case "end_turn":

View File

@@ -167,11 +167,6 @@ func SetOriginalRequestAuth(auth string) {
}
func OverwriteRequestAuthorizationHeader(headers http.Header, credential string) {
if exist := headers.Get(HeaderOriginalAuth); exist == "" {
if originAuth := headers.Get(HeaderAuthorization); originAuth != "" {
headers.Set(HeaderOriginalAuth, originAuth)
}
}
headers.Set(HeaderAuthorization, credential)
}