ChatCompletionStream.d.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import * as Core from "../core.js";
  2. import { ChatCompletionTokenLogprob, type ChatCompletion, type ChatCompletionChunk, type ChatCompletionCreateParams, type ChatCompletionCreateParamsBase, type ChatCompletionRole } from "../resources/chat/completions/completions.js";
  3. import { AbstractChatCompletionRunner, type AbstractChatCompletionRunnerEvents } from "./AbstractChatCompletionRunner.js";
  4. import { type ReadableStream } from "../_shims/index.js";
  5. import OpenAI from "../index.js";
  6. import { ParsedChatCompletion } from "../resources/beta/chat/completions.js";
  7. export interface ContentDeltaEvent {
  8. delta: string;
  9. snapshot: string;
  10. parsed: unknown | null;
  11. }
  12. export interface ContentDoneEvent<ParsedT = null> {
  13. content: string;
  14. parsed: ParsedT | null;
  15. }
  16. export interface RefusalDeltaEvent {
  17. delta: string;
  18. snapshot: string;
  19. }
  20. export interface RefusalDoneEvent {
  21. refusal: string;
  22. }
  23. export interface FunctionToolCallArgumentsDeltaEvent {
  24. name: string;
  25. index: number;
  26. arguments: string;
  27. parsed_arguments: unknown;
  28. arguments_delta: string;
  29. }
  30. export interface FunctionToolCallArgumentsDoneEvent {
  31. name: string;
  32. index: number;
  33. arguments: string;
  34. parsed_arguments: unknown;
  35. }
  36. export interface LogProbsContentDeltaEvent {
  37. content: Array<ChatCompletionTokenLogprob>;
  38. snapshot: Array<ChatCompletionTokenLogprob>;
  39. }
  40. export interface LogProbsContentDoneEvent {
  41. content: Array<ChatCompletionTokenLogprob>;
  42. }
  43. export interface LogProbsRefusalDeltaEvent {
  44. refusal: Array<ChatCompletionTokenLogprob>;
  45. snapshot: Array<ChatCompletionTokenLogprob>;
  46. }
  47. export interface LogProbsRefusalDoneEvent {
  48. refusal: Array<ChatCompletionTokenLogprob>;
  49. }
  50. export interface ChatCompletionStreamEvents<ParsedT = null> extends AbstractChatCompletionRunnerEvents {
  51. content: (contentDelta: string, contentSnapshot: string) => void;
  52. chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
  53. 'content.delta': (props: ContentDeltaEvent) => void;
  54. 'content.done': (props: ContentDoneEvent<ParsedT>) => void;
  55. 'refusal.delta': (props: RefusalDeltaEvent) => void;
  56. 'refusal.done': (props: RefusalDoneEvent) => void;
  57. 'tool_calls.function.arguments.delta': (props: FunctionToolCallArgumentsDeltaEvent) => void;
  58. 'tool_calls.function.arguments.done': (props: FunctionToolCallArgumentsDoneEvent) => void;
  59. 'logprobs.content.delta': (props: LogProbsContentDeltaEvent) => void;
  60. 'logprobs.content.done': (props: LogProbsContentDoneEvent) => void;
  61. 'logprobs.refusal.delta': (props: LogProbsRefusalDeltaEvent) => void;
  62. 'logprobs.refusal.done': (props: LogProbsRefusalDoneEvent) => void;
  63. }
  64. export type ChatCompletionStreamParams = Omit<ChatCompletionCreateParamsBase, 'stream'> & {
  65. stream?: true;
  66. };
  67. export declare class ChatCompletionStream<ParsedT = null> extends AbstractChatCompletionRunner<ChatCompletionStreamEvents<ParsedT>, ParsedT> implements AsyncIterable<ChatCompletionChunk> {
  68. #private;
  69. constructor(params: ChatCompletionCreateParams | null);
  70. get currentChatCompletionSnapshot(): ChatCompletionSnapshot | undefined;
  71. /**
  72. * Intended for use on the frontend, consuming a stream produced with
  73. * `.toReadableStream()` on the backend.
  74. *
  75. * Note that messages sent to the model do not appear in `.on('message')`
  76. * in this context.
  77. */
  78. static fromReadableStream(stream: ReadableStream): ChatCompletionStream<null>;
  79. static createChatCompletion<ParsedT>(client: OpenAI, params: ChatCompletionStreamParams, options?: Core.RequestOptions): ChatCompletionStream<ParsedT>;
  80. protected _createChatCompletion(client: OpenAI, params: ChatCompletionCreateParams, options?: Core.RequestOptions): Promise<ParsedChatCompletion<ParsedT>>;
  81. protected _fromReadableStream(readableStream: ReadableStream, options?: Core.RequestOptions): Promise<ChatCompletion>;
  82. [Symbol.asyncIterator](this: ChatCompletionStream<ParsedT>): AsyncIterator<ChatCompletionChunk>;
  83. toReadableStream(): ReadableStream;
  84. }
  85. /**
  86. * Represents a streamed chunk of a chat completion response returned by model,
  87. * based on the provided input.
  88. */
  89. export interface ChatCompletionSnapshot {
  90. /**
  91. * A unique identifier for the chat completion.
  92. */
  93. id: string;
  94. /**
  95. * A list of chat completion choices. Can be more than one if `n` is greater
  96. * than 1.
  97. */
  98. choices: Array<ChatCompletionSnapshot.Choice>;
  99. /**
  100. * The Unix timestamp (in seconds) of when the chat completion was created.
  101. */
  102. created: number;
  103. /**
  104. * The model to generate the completion.
  105. */
  106. model: string;
  107. /**
  108. * This fingerprint represents the backend configuration that the model runs with.
  109. *
  110. * Can be used in conjunction with the `seed` request parameter to understand when
  111. * backend changes have been made that might impact determinism.
  112. */
  113. system_fingerprint?: string;
  114. }
  115. export declare namespace ChatCompletionSnapshot {
  116. interface Choice {
  117. /**
  118. * A chat completion delta generated by streamed model responses.
  119. */
  120. message: Choice.Message;
  121. /**
  122. * The reason the model stopped generating tokens. This will be `stop` if the model
  123. * hit a natural stop point or a provided stop sequence, `length` if the maximum
  124. * number of tokens specified in the request was reached, `content_filter` if
  125. * content was omitted due to a flag from our content filters, or `function_call`
  126. * if the model called a function.
  127. */
  128. finish_reason: ChatCompletion.Choice['finish_reason'] | null;
  129. /**
  130. * Log probability information for the choice.
  131. */
  132. logprobs: ChatCompletion.Choice.Logprobs | null;
  133. /**
  134. * The index of the choice in the list of choices.
  135. */
  136. index: number;
  137. }
  138. namespace Choice {
  139. /**
  140. * A chat completion delta generated by streamed model responses.
  141. */
  142. interface Message {
  143. /**
  144. * The contents of the chunk message.
  145. */
  146. content?: string | null;
  147. refusal?: string | null;
  148. parsed?: unknown | null;
  149. /**
  150. * The name and arguments of a function that should be called, as generated by the
  151. * model.
  152. */
  153. function_call?: Message.FunctionCall;
  154. tool_calls?: Array<Message.ToolCall>;
  155. /**
  156. * The role of the author of this message.
  157. */
  158. role?: ChatCompletionRole;
  159. }
  160. namespace Message {
  161. interface ToolCall {
  162. /**
  163. * The ID of the tool call.
  164. */
  165. id: string;
  166. function: ToolCall.Function;
  167. /**
  168. * The type of the tool.
  169. */
  170. type: 'function';
  171. }
  172. namespace ToolCall {
  173. interface Function {
  174. /**
  175. * The arguments to call the function with, as generated by the model in JSON
  176. * format. Note that the model does not always generate valid JSON, and may
  177. * hallucinate parameters not defined by your function schema. Validate the
  178. * arguments in your code before calling your function.
  179. */
  180. arguments: string;
  181. parsed_arguments?: unknown;
  182. /**
  183. * The name of the function to call.
  184. */
  185. name: string;
  186. }
  187. }
  188. /**
  189. * The name and arguments of a function that should be called, as generated by the
  190. * model.
  191. */
  192. interface FunctionCall {
  193. /**
  194. * The arguments to call the function with, as generated by the model in JSON
  195. * format. Note that the model does not always generate valid JSON, and may
  196. * hallucinate parameters not defined by your function schema. Validate the
  197. * arguments in your code before calling your function.
  198. */
  199. arguments?: string;
  200. /**
  201. * The name of the function to call.
  202. */
  203. name?: string;
  204. }
  205. }
  206. }
  207. }
  208. //# sourceMappingURL=ChatCompletionStream.d.ts.map