diff --git a/README.md b/README.md index e280c2354..e97b7078c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ const openai = new OpenAI({ }); async function main() { - const params: OpenAI.Chat.CompletionCreateParams = { + const params: OpenAI.Chat.ChatCompletionCreateParams = { messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo', }; diff --git a/api.md b/api.md index 1c443b4f1..14b70dffe 100644 --- a/api.md +++ b/api.md @@ -19,6 +19,7 @@ Types: - ChatCompletion - ChatCompletionChunk - ChatCompletionMessage +- ChatCompletionMessageParam - CreateChatCompletionRequestMessage Methods: diff --git a/examples/chat-params-types.ts b/examples/chat-params-types.ts index 66f27ed31..86c28fc8b 100755 --- a/examples/chat-params-types.ts +++ b/examples/chat-params-types.ts @@ -9,7 +9,7 @@ const openai = new OpenAI(); async function main() { // ---------------- Explicit non-streaming params ------------ - const params: OpenAI.Chat.CompletionCreateParams = { + const params: OpenAI.Chat.ChatCompletionCreateParams = { model: 'gpt-4', messages: [{ role: 'user', content: 'Say this is a test!' }], }; @@ -18,7 +18,7 @@ async function main() { // ---------------- Explicit streaming params ---------------- - const streamingParams: OpenAI.Chat.CompletionCreateParams = { + const streamingParams: OpenAI.Chat.ChatCompletionCreateParams = { model: 'gpt-4', messages: [{ role: 'user', content: 'Say this is a test!' }], stream: true, @@ -32,12 +32,12 @@ async function main() { // ---------------- Explicit (non)streaming types ---------------- - const params1: OpenAI.Chat.CompletionCreateParamsNonStreaming = { + const params1: OpenAI.Chat.ChatCompletionCreateParamsNonStreaming = { model: 'gpt-4', messages: [{ role: 'user', content: 'Say this is a test!' }], }; - const params2: OpenAI.Chat.CompletionCreateParamsStreaming = { + const params2: OpenAI.Chat.ChatCompletionCreateParamsStreaming = { model: 'gpt-4', messages: [{ role: 'user', content: 'Say this is a test!' }], stream: true, @@ -52,9 +52,9 @@ async function main() { // `role: string` is not assignable. const streamingParams2 = { model: 'gpt-4', - messages: [{ role: 'user', content: 'Say this is a test!' }], - stream: true, - } as const; + messages: [{ role: 'user' as const, content: 'Say this is a test!' }], + stream: true as const, + }; // TS knows this is a Stream instance. const stream2 = await openai.chat.completions.create(streamingParams2); @@ -95,11 +95,13 @@ async function main() { // not the response will be streamed. export async function createCompletionParams( stream: true, -): Promise; +): Promise; export async function createCompletionParams( stream: false, -): Promise; -export async function createCompletionParams(stream: boolean): Promise { +): Promise; +export async function createCompletionParams( + stream: boolean, +): Promise { const params = { model: 'gpt-3.5-turbo', messages: [{ role: 'user' as const, content: 'Hello!' }], diff --git a/examples/function-call-stream.ts b/examples/function-call-stream.ts index 6126a7ff2..be4688aa7 100755 --- a/examples/function-call-stream.ts +++ b/examples/function-call-stream.ts @@ -5,13 +5,13 @@ import OpenAI from 'openai'; import { ChatCompletionMessage, ChatCompletionChunk, - CreateChatCompletionRequestMessage, + ChatCompletionMessageParam, } from 'openai/resources/chat'; // gets API Key from environment variable OPENAI_API_KEY const openai = new OpenAI(); -const functions: OpenAI.Chat.CompletionCreateParams.Function[] = [ +const functions: OpenAI.Chat.ChatCompletionCreateParams.Function[] = [ { name: 'list', description: 'list queries books by genre, and returns a list of names of books', @@ -63,7 +63,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall): } async function main() { - const messages: CreateChatCompletionRequestMessage[] = [ + const messages: ChatCompletionMessageParam[] = [ { role: 'system', content: diff --git a/examples/function-call.ts b/examples/function-call.ts index 158437e68..ce12431b0 100755 --- a/examples/function-call.ts +++ b/examples/function-call.ts @@ -1,12 +1,12 @@ #!/usr/bin/env -S npm run tsn -T import OpenAI from 'openai'; -import { ChatCompletionMessage, CreateChatCompletionRequestMessage } from 'openai/resources/chat'; +import { ChatCompletionMessage, ChatCompletionMessageParam } from 'openai/resources/chat'; // gets API Key from environment variable OPENAI_API_KEY const openai = new OpenAI(); -const functions: OpenAI.Chat.CompletionCreateParams.Function[] = [ +const functions: OpenAI.Chat.ChatCompletionCreateParams.Function[] = [ { name: 'list', description: 'list queries books by genre, and returns a list of names of books', @@ -58,7 +58,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall): } async function main() { - const messages: CreateChatCompletionRequestMessage[] = [ + const messages: ChatCompletionMessageParam[] = [ { role: 'system', content: diff --git a/src/resources/chat/chat.ts b/src/resources/chat/chat.ts index 9a256b596..5d10f2f4d 100644 --- a/src/resources/chat/chat.ts +++ b/src/resources/chat/chat.ts @@ -13,8 +13,12 @@ export namespace Chat { export import ChatCompletion = API.ChatCompletion; export import ChatCompletionChunk = API.ChatCompletionChunk; export import ChatCompletionMessage = API.ChatCompletionMessage; + export import ChatCompletionMessageParam = API.ChatCompletionMessageParam; export import CreateChatCompletionRequestMessage = API.CreateChatCompletionRequestMessage; + export import ChatCompletionCreateParams = API.ChatCompletionCreateParams; export import CompletionCreateParams = API.CompletionCreateParams; + export import ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming; export import CompletionCreateParamsNonStreaming = API.CompletionCreateParamsNonStreaming; + export import ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming; export import CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming; } diff --git a/src/resources/chat/completions.ts b/src/resources/chat/completions.ts index b05ab1eb4..ddfe1344b 100644 --- a/src/resources/chat/completions.ts +++ b/src/resources/chat/completions.ts @@ -11,17 +11,20 @@ export class Completions extends APIResource { /** * Creates a model response for the given chat conversation. */ - create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise; create( - body: CompletionCreateParamsStreaming, + body: ChatCompletionCreateParamsNonStreaming, + options?: Core.RequestOptions, + ): APIPromise; + create( + body: ChatCompletionCreateParamsStreaming, options?: Core.RequestOptions, ): APIPromise>; create( - body: CompletionCreateParamsBase, + body: ChatCompletionCreateParamsBase, options?: Core.RequestOptions, ): APIPromise | ChatCompletion>; create( - body: CompletionCreateParams, + body: ChatCompletionCreateParams, options?: Core.RequestOptions, ): APIPromise | APIPromise> { return this.post('/chat/completions', { body, ...options, stream: body.stream ?? false }) as @@ -229,7 +232,7 @@ export namespace ChatCompletionMessage { } } -export interface CreateChatCompletionRequestMessage { +export interface ChatCompletionMessageParam { /** * The contents of the message. `content` is required for all messages, and may be * null for assistant messages with function calls. @@ -246,7 +249,7 @@ export interface CreateChatCompletionRequestMessage { * The name and arguments of a function that should be called, as generated by the * model. */ - function_call?: CreateChatCompletionRequestMessage.FunctionCall; + function_call?: ChatCompletionMessageParam.FunctionCall; /** * The name of the author of this message. `name` is required if role is @@ -257,7 +260,7 @@ export interface CreateChatCompletionRequestMessage { name?: string; } -export namespace CreateChatCompletionRequestMessage { +export namespace ChatCompletionMessageParam { /** * The name and arguments of a function that should be called, as generated by the * model. @@ -278,14 +281,21 @@ export namespace CreateChatCompletionRequestMessage { } } -export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming; +/** + * @deprecated ChatCompletionMessageParam should be used instead + */ +export type CreateChatCompletionRequestMessage = ChatCompletionMessageParam; -export interface CompletionCreateParamsBase { +export type ChatCompletionCreateParams = + | ChatCompletionCreateParamsNonStreaming + | ChatCompletionCreateParamsStreaming; + +export interface ChatCompletionCreateParamsBase { /** * A list of messages comprising the conversation so far. * [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb). */ - messages: Array; + messages: Array; /** * ID of the model to use. See the @@ -323,12 +333,12 @@ export interface CompletionCreateParamsBase { * the default when no functions are present. "auto" is the default if functions * are present. */ - function_call?: 'none' | 'auto' | CompletionCreateParams.FunctionCallOption; + function_call?: 'none' | 'auto' | ChatCompletionCreateParams.FunctionCallOption; /** * A list of functions the model may generate JSON inputs for. */ - functions?: Array; + functions?: Array; /** * Modify the likelihood of specified tokens appearing in the completion. @@ -406,7 +416,7 @@ export interface CompletionCreateParamsBase { user?: string; } -export namespace CompletionCreateParams { +export namespace ChatCompletionCreateParams { export interface FunctionCallOption { /** * The name of the function to call. @@ -439,11 +449,16 @@ export namespace CompletionCreateParams { description?: string; } - export type CompletionCreateParamsNonStreaming = API.CompletionCreateParamsNonStreaming; - export type CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming; + export type ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming; + export type ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming; } -export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase { +/** + * @deprecated Use ChatCompletionCreateParams instead + */ +export type CompletionCreateParams = ChatCompletionCreateParams; + +export interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase { /** * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be * sent as data-only @@ -455,7 +470,12 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara stream?: false | null; } -export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase { +/** + * @deprecated Use ChatCompletionCreateParamsNonStreaming instead + */ +export type CompletionCreateParamsNonStreaming = ChatCompletionCreateParamsNonStreaming; + +export interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase { /** * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be * sent as data-only @@ -467,12 +487,21 @@ export interface CompletionCreateParamsStreaming extends CompletionCreateParamsB stream: true; } +/** + * @deprecated Use ChatCompletionCreateParamsStreaming instead + */ +export type CompletionCreateParamsStreaming = ChatCompletionCreateParamsStreaming; + export namespace Completions { export import ChatCompletion = API.ChatCompletion; export import ChatCompletionChunk = API.ChatCompletionChunk; export import ChatCompletionMessage = API.ChatCompletionMessage; + export import ChatCompletionMessageParam = API.ChatCompletionMessageParam; export import CreateChatCompletionRequestMessage = API.CreateChatCompletionRequestMessage; + export import ChatCompletionCreateParams = API.ChatCompletionCreateParams; export import CompletionCreateParams = API.CompletionCreateParams; + export import ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming; export import CompletionCreateParamsNonStreaming = API.CompletionCreateParamsNonStreaming; + export import ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming; export import CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming; } diff --git a/src/resources/chat/index.ts b/src/resources/chat/index.ts index f9232bffe..ea9d1d1b9 100644 --- a/src/resources/chat/index.ts +++ b/src/resources/chat/index.ts @@ -5,9 +5,13 @@ export { ChatCompletion, ChatCompletionChunk, ChatCompletionMessage, + ChatCompletionMessageParam, CreateChatCompletionRequestMessage, + ChatCompletionCreateParams, CompletionCreateParams, + ChatCompletionCreateParamsNonStreaming, CompletionCreateParamsNonStreaming, + ChatCompletionCreateParamsStreaming, CompletionCreateParamsStreaming, Completions, } from './completions';