Skip to content

Commit

Permalink
feat (provider/openai): set stream_options/include_usage to true when…
Browse files Browse the repository at this point in the history
… streaming (#1578)

Co-authored-by: Valentin Cassarino <valencassa@gmail.com>
  • Loading branch information
lgrammel and ValenCassa authored May 14, 2024
1 parent e403313 commit 1009594
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-years-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/openai': patch
---

feat (provider/openai): set stream_options/include_usage to true when streaming
4 changes: 4 additions & 0 deletions examples/ai-core/src/stream-text/openai-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ async function main() {
for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);
1 change: 1 addition & 0 deletions packages/openai/src/openai-chat-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ describe('doStream', () => {

expect(await server.getRequestBodyJson()).toStrictEqual({
stream: true,
stream_options: { include_usage: true },
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: [{ type: 'text', text: 'Hello' }] }],
logprobs: false,
Expand Down
3 changes: 3 additions & 0 deletions packages/openai/src/openai-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ export class OpenAIChatLanguageModel implements LanguageModelV1 {
body: {
...args,
stream: true,
stream_options: {
include_usage: true,
},
},
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createEventSourceResponseHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ describe('doStream', () => {

expect(await server.getRequestBodyJson()).toStrictEqual({
stream: true,
stream_options: { include_usage: true },
model: 'gpt-3.5-turbo-instruct',
prompt: 'Hello',
});
Expand Down
3 changes: 3 additions & 0 deletions packages/openai/src/openai-completion-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ export class OpenAICompletionLanguageModel implements LanguageModelV1 {
body: {
...this.getArgs(options),
stream: true,
stream_options: {
include_usage: true,
},
},
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createEventSourceResponseHandler(
Expand Down

0 comments on commit 1009594

Please sign in to comment.