fix: request usage for OpenAI streams
This commit is contained in:
@@ -25,6 +25,7 @@ func (c OpenAIClient) Run(ctx context.Context, request Request) (Response, error
|
||||
body := cloneBody(request.Body)
|
||||
body["model"] = upstreamModelName(request.Candidate)
|
||||
stream := request.Stream || boolValue(body, "stream")
|
||||
ensureOpenAIStreamUsage(body, request.Kind, stream)
|
||||
raw, _ := json.Marshal(body)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, joinURL(request.Candidate.BaseURL, endpoint), bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
@@ -91,6 +92,20 @@ func cloneBody(body map[string]any) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
func ensureOpenAIStreamUsage(body map[string]any, kind string, stream bool) {
|
||||
if !stream || kind != "chat.completions" {
|
||||
return
|
||||
}
|
||||
streamOptions := map[string]any{}
|
||||
if existing, ok := body["stream_options"].(map[string]any); ok {
|
||||
for key, value := range existing {
|
||||
streamOptions[key] = value
|
||||
}
|
||||
}
|
||||
streamOptions["include_usage"] = true
|
||||
body["stream_options"] = streamOptions
|
||||
}
|
||||
|
||||
func joinURL(base string, path string) string {
|
||||
base = strings.TrimRight(strings.TrimSpace(base), "/")
|
||||
if base == "" {
|
||||
|
||||
Reference in New Issue
Block a user