Skip to content

Commit d9d0f0f

Browse files
committed
feat(api): add usage metadata when streaming (#829)
1 parent cdc3ad7 commit d9d0f0f

File tree

9 files changed

+43
-3
lines changed

9 files changed

+43
-3
lines changed

.stats.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
configured_endpoints: 64
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-97c9a5f089049dc9eb5cee9475558049003e37e42202cab39e59d75e08b4c613.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-edb5af3ade0cd27cf366b0654b90c7a81c43c433e11fc3f6e621e2c779de10d4.yml

api.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Types:
4141
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessageToolCall</a></code>
4242
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionNamedToolChoice</a></code>
4343
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionRole</a></code>
44+
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionStreamOptions</a></code>
4445
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionSystemMessageParam</a></code>
4546
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionTokenLogprob</a></code>
4647
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionTool</a></code>

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export namespace OpenAI {
251251
export import ChatCompletionMessageToolCall = API.ChatCompletionMessageToolCall;
252252
export import ChatCompletionNamedToolChoice = API.ChatCompletionNamedToolChoice;
253253
export import ChatCompletionRole = API.ChatCompletionRole;
254+
export import ChatCompletionStreamOptions = API.ChatCompletionStreamOptions;
254255
export import ChatCompletionSystemMessageParam = API.ChatCompletionSystemMessageParam;
255256
export import ChatCompletionTokenLogprob = API.ChatCompletionTokenLogprob;
256257
export import ChatCompletionTool = API.ChatCompletionTool;

src/resources/chat/chat.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export namespace Chat {
4545
export import ChatCompletionMessageToolCall = CompletionsAPI.ChatCompletionMessageToolCall;
4646
export import ChatCompletionNamedToolChoice = CompletionsAPI.ChatCompletionNamedToolChoice;
4747
export import ChatCompletionRole = CompletionsAPI.ChatCompletionRole;
48+
export import ChatCompletionStreamOptions = CompletionsAPI.ChatCompletionStreamOptions;
4849
export import ChatCompletionSystemMessageParam = CompletionsAPI.ChatCompletionSystemMessageParam;
4950
export import ChatCompletionTokenLogprob = CompletionsAPI.ChatCompletionTokenLogprob;
5051
export import ChatCompletionTool = CompletionsAPI.ChatCompletionTool;

src/resources/chat/completions.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ export interface ChatCompletionChunk {
183183
id: string;
184184

185185
/**
186-
* A list of chat completion choices. Can be more than one if `n` is greater
187-
* than 1.
186+
* A list of chat completion choices. Can contain more than one elements if `n` is
187+
* greater than 1. Can also be empty for the last chunk if you set
188+
* `stream_options: {"include_usage": true}`.
188189
*/
189190
choices: Array<ChatCompletionChunk.Choice>;
190191

@@ -210,6 +211,14 @@ export interface ChatCompletionChunk {
210211
* backend changes have been made that might impact determinism.
211212
*/
212213
system_fingerprint?: string;
214+
215+
/**
216+
* An optional field that will only be present when you set
217+
* `stream_options: {"include_usage": true}` in your request. When present, it
218+
* contains a null value except for the last chunk which contains the token usage
219+
* statistics for the entire request.
220+
*/
221+
usage?: CompletionsAPI.CompletionUsage;
213222
}
214223

215224
export namespace ChatCompletionChunk {
@@ -517,6 +526,19 @@ export namespace ChatCompletionNamedToolChoice {
517526
*/
518527
export type ChatCompletionRole = 'system' | 'user' | 'assistant' | 'tool' | 'function';
519528

529+
/**
530+
* Options for streaming response. Only set this when you set `stream: true`.
531+
*/
532+
export interface ChatCompletionStreamOptions {
533+
/**
534+
* If set, an additional chunk will be streamed before the `data: [DONE]` message.
535+
* The `usage` field on this chunk shows the token usage statistics for the entire
536+
* request, and the `choices` field will always be an empty array. All other chunks
537+
* will also include a `usage` field, but with a null value.
538+
*/
539+
include_usage?: boolean;
540+
}
541+
520542
export interface ChatCompletionSystemMessageParam {
521543
/**
522544
* The contents of the system message.
@@ -786,6 +808,11 @@ export interface ChatCompletionCreateParamsBase {
786808
*/
787809
stream?: boolean | null;
788810

811+
/**
812+
* Options for streaming response. Only set this when you set `stream: true`.
813+
*/
814+
stream_options?: ChatCompletionStreamOptions | null;
815+
789816
/**
790817
* What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
791818
* make the output more random, while lower values like 0.2 will make it more
@@ -949,6 +976,7 @@ export namespace Completions {
949976
export import ChatCompletionMessageToolCall = ChatCompletionsAPI.ChatCompletionMessageToolCall;
950977
export import ChatCompletionNamedToolChoice = ChatCompletionsAPI.ChatCompletionNamedToolChoice;
951978
export import ChatCompletionRole = ChatCompletionsAPI.ChatCompletionRole;
979+
export import ChatCompletionStreamOptions = ChatCompletionsAPI.ChatCompletionStreamOptions;
952980
export import ChatCompletionSystemMessageParam = ChatCompletionsAPI.ChatCompletionSystemMessageParam;
953981
export import ChatCompletionTokenLogprob = ChatCompletionsAPI.ChatCompletionTokenLogprob;
954982
export import ChatCompletionTool = ChatCompletionsAPI.ChatCompletionTool;

src/resources/chat/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ChatCompletionMessageToolCall,
1515
ChatCompletionNamedToolChoice,
1616
ChatCompletionRole,
17+
ChatCompletionStreamOptions,
1718
ChatCompletionSystemMessageParam,
1819
ChatCompletionTokenLogprob,
1920
ChatCompletionTool,

src/resources/completions.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Core from 'openai/core';
44
import { APIPromise } from 'openai/core';
55
import { APIResource } from 'openai/resource';
66
import * as CompletionsAPI from 'openai/resources/completions';
7+
import * as ChatCompletionsAPI from 'openai/resources/chat/completions';
78
import { Stream } from 'openai/streaming';
89

910
export class Completions extends APIResource {
@@ -251,6 +252,11 @@ export interface CompletionCreateParamsBase {
251252
*/
252253
stream?: boolean | null;
253254

255+
/**
256+
* Options for streaming response. Only set this when you set `stream: true`.
257+
*/
258+
stream_options?: ChatCompletionsAPI.ChatCompletionStreamOptions | null;
259+
254260
/**
255261
* The suffix that comes after a completion of inserted text.
256262
*

tests/api-resources/chat/completions.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('resource completions', () => {
3939
seed: -9223372036854776000,
4040
stop: 'string',
4141
stream: false,
42+
stream_options: { include_usage: true },
4243
temperature: 1,
4344
tool_choice: 'none',
4445
tools: [

tests/api-resources/completions.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('resource completions', () => {
3535
seed: -9223372036854776000,
3636
stop: '\n',
3737
stream: false,
38+
stream_options: { include_usage: true },
3839
suffix: 'test.',
3940
temperature: 1,
4041
top_p: 1,

0 commit comments

Comments
 (0)