Skip to content

Commit

Permalink
set include_usage by default for all model providers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlanni committed Feb 26, 2025
1 parent f6c4841 commit 5ffa569
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
10 changes: 9 additions & 1 deletion plugins/wasm-go/extensions/ai-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

const (
Expand Down Expand Up @@ -149,7 +150,14 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
)
return types.ActionContinue
}

// Default setting include_usage.
if gjson.GetBytes(body, "stream").Bool() {
var err error
newBody, err = sjson.SetBytes(newBody, "stream_options.include_usage", true)
if err != nil {
log.Errorf("set include_usage failed, err:%s", err)
}
}
log.Debugf("[onHttpRequestBody] newBody=%s", newBody)
body = newBody
action, err := handler.OnRequestBody(ctx, apiName, body, log)
Expand Down
19 changes: 6 additions & 13 deletions plugins/wasm-go/extensions/ai-proxy/provider/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,14 @@ func (m *openaiProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName,
}

func (m *openaiProvider) TransformRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) ([]byte, error) {
request := &chatCompletionRequest{}
if err := decodeChatCompletionRequest(body, request); err != nil {
return nil, err
}
if m.config.responseJsonSchema != nil {
request := &chatCompletionRequest{}
if err := decodeChatCompletionRequest(body, request); err != nil {
return nil, err
}
log.Debugf("[ai-proxy] set response format to %s", m.config.responseJsonSchema)
request.ResponseFormat = m.config.responseJsonSchema
body, _ = json.Marshal(request)
}
if request.Stream {
// For stream requests, we need to include usage in the response.
if request.StreamOptions == nil {
request.StreamOptions = &streamOptions{IncludeUsage: true}
} else if !request.StreamOptions.IncludeUsage {
request.StreamOptions.IncludeUsage = true
}
}
return json.Marshal(request)
return m.config.defaultTransformRequestBody(ctx, apiName, body, log)
}

0 comments on commit 5ffa569

Please sign in to comment.