Skip to content

Commit 239d3e1

Browse files
authored
Merge pull request #156 from doringeman/openairecorder-choices
fix(OpenAIRecorder): save last valid "choices"
2 parents 6006f68 + 6394ed9 commit 239d3e1

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

pkg/metrics/openai_recorder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string {
195195
lines := strings.Split(streamingBody, "\n")
196196
var contentBuilder strings.Builder
197197
var reasoningContentBuilder strings.Builder
198-
var lastChunk map[string]interface{}
198+
var lastChoice, lastChunk map[string]interface{}
199199

200200
for _, line := range lines {
201201
if strings.HasPrefix(line, "data: ") {
@@ -213,6 +213,7 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string {
213213

214214
if choices, ok := chunk["choices"].([]interface{}); ok && len(choices) > 0 {
215215
if choice, ok := choices[0].(map[string]interface{}); ok {
216+
lastChoice = choice
216217
if delta, ok := choice["delta"].(map[string]interface{}); ok {
217218
if content, ok := delta["content"].(string); ok {
218219
contentBuilder.WriteString(content)
@@ -235,6 +236,7 @@ func (r *OpenAIRecorder) convertStreamingResponse(streamingBody string) string {
235236
for key, value := range lastChunk {
236237
finalResponse[key] = value
237238
}
239+
finalResponse["choices"] = []interface{}{lastChoice}
238240

239241
if choices, ok := finalResponse["choices"].([]interface{}); ok && len(choices) > 0 {
240242
if choice, ok := choices[0].(map[string]interface{}); ok {

0 commit comments

Comments
 (0)