Skip to content

Commit

Permalink
stream: make usage optional on streamed response until final chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Oct 30, 2024
1 parent 253a786 commit a2777bc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TransformResponse(req *worker.GenLLMFormdataRequestBody, resp *http.Respons
FinishReason: "stop",
},
},
Usage: models.Usage{
Usage: &models.Usage{
TotalTokens: res.TokensUsed, // TokensUsed already includes prompt tokens
},
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func TransformStreamResponse(chunk worker.LlmStreamChunk, req *worker.GenLLMForm
if chunk.Done {
openAIResp.Choices[0].FinishReason = "stop"
// Only include usage information in the final chunk
openAIResp.Usage = models.Usage{
openAIResp.Usage = &models.Usage{
TotalTokens: chunk.TokensUsed, // TokensUsed already includes prompt tokens
}
}
Expand Down
4 changes: 2 additions & 2 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type OpenAIResponse struct {
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
Usage *Usage `json:"usage"`
}

type OpenAIStreamResponse struct {
Expand All @@ -34,7 +34,7 @@ type OpenAIStreamResponse struct {
Created int64 `json:"created"`
Model string `json:"model"`
Choices []StreamChoice `json:"choices"`
Usage Usage `json:"usage"`
Usage *Usage `json:"usage,omitempty"`
}

type StreamChoice struct {
Expand Down

0 comments on commit a2777bc

Please sign in to comment.