mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-20 21:30:50 +08:00
Add usage to /v1/completions
This commit is contained in:
@@ -332,6 +332,7 @@ func convertChatCompletionsStreamChunkToCompletions(chunkData []byte) []byte {
|
|||||||
|
|
||||||
// Check if this chunk has any meaningful content
|
// Check if this chunk has any meaningful content
|
||||||
hasContent := false
|
hasContent := false
|
||||||
|
hasUsage := root.Get("usage").Exists()
|
||||||
if chatChoices := root.Get("choices"); chatChoices.Exists() && chatChoices.IsArray() {
|
if chatChoices := root.Get("choices"); chatChoices.Exists() && chatChoices.IsArray() {
|
||||||
chatChoices.ForEach(func(_, choice gjson.Result) bool {
|
chatChoices.ForEach(func(_, choice gjson.Result) bool {
|
||||||
// Check if delta has content or finish_reason
|
// Check if delta has content or finish_reason
|
||||||
@@ -350,8 +351,8 @@ func convertChatCompletionsStreamChunkToCompletions(chunkData []byte) []byte {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no meaningful content, return nil to indicate this chunk should be skipped
|
// If no meaningful content and no usage, return nil to indicate this chunk should be skipped
|
||||||
if !hasContent {
|
if !hasContent && !hasUsage {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,6 +411,11 @@ func convertChatCompletionsStreamChunkToCompletions(chunkData []byte) []byte {
|
|||||||
out, _ = sjson.SetRaw(out, "choices", string(choicesJSON))
|
out, _ = sjson.SetRaw(out, "choices", string(choicesJSON))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Copy usage if present
|
||||||
|
if usage := root.Get("usage"); usage.Exists() {
|
||||||
|
out, _ = sjson.SetRaw(out, "usage", usage.Raw)
|
||||||
|
}
|
||||||
|
|
||||||
return []byte(out)
|
return []byte(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user