Skip to content

Commit

Permalink
fix(examples): handle usage chunk in tool call streaming (#1068)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Jacob Zimmerman <[email protected]>
  • Loading branch information
RobertCraigie and jacobzim-stl authored Sep 13, 2024
1 parent 58eb50c commit e4188c4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion examples/tool-calls-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ function messageReducer(previous: ChatCompletionMessage, item: ChatCompletionChu
}
return acc;
};
return reduce(previous, item.choices[0]!.delta) as ChatCompletionMessage;

const choice = item.choices[0];
if (!choice) {
// chunk contains information about usage and token counts
return previous;
}
return reduce(previous, choice.delta) as ChatCompletionMessage;
}

function lineRewriter() {
Expand Down

0 comments on commit e4188c4

Please sign in to comment.