Skip to content

Commit

Permalink
feat(types): fix ambiguous auto-import for chat completions params (#266
Browse files Browse the repository at this point in the history
)

This renames the following types and deprecates the old names:
- `CompletionCreateParams` -> `ChatCompletionCreateParams`
- `CompletionCreateParamsStreaming` -> `ChatCompletionCreateParamsStreaming`
- `CompletionCreateParamsNonStreaming` -> `ChatCompletionCreateParamsNonStreaming`
- `CreateChatCompletionRequestMessage` -> `ChatCompletionCreateParamsMessage`
  • Loading branch information
stainless-bot committed Aug 29, 2023
1 parent 3f85654 commit 19c99fb
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Types:
- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessage</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessageParam</a></code>
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>

Methods:
Expand Down
22 changes: 12 additions & 10 deletions examples/chat-params-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!' }],
};
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -95,11 +95,13 @@ async function main() {
// not the response will be streamed.
export async function createCompletionParams(
stream: true,
): Promise<OpenAI.Chat.CompletionCreateParamsStreaming>;
): Promise<OpenAI.Chat.ChatCompletionCreateParamsStreaming>;
export async function createCompletionParams(
stream: false,
): Promise<OpenAI.Chat.CompletionCreateParamsNonStreaming>;
export async function createCompletionParams(stream: boolean): Promise<OpenAI.Chat.CompletionCreateParams> {
): Promise<OpenAI.Chat.ChatCompletionCreateParamsNonStreaming>;
export async function createCompletionParams(
stream: boolean,
): Promise<OpenAI.Chat.ChatCompletionCreateParams> {
const params = {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user' as const, content: 'Hello!' }],
Expand Down
6 changes: 3 additions & 3 deletions examples/function-call-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -63,7 +63,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall):
}

async function main() {
const messages: CreateChatCompletionRequestMessage[] = [
const messages: ChatCompletionMessageParam[] = [
{
role: 'system',
content:
Expand Down
6 changes: 3 additions & 3 deletions examples/function-call.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -58,7 +58,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall):
}

async function main() {
const messages: CreateChatCompletionRequestMessage[] = [
const messages: ChatCompletionMessageParam[] = [
{
role: 'system',
content:
Expand Down
4 changes: 4 additions & 0 deletions src/resources/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
63 changes: 46 additions & 17 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatCompletion>;
create(
body: CompletionCreateParamsStreaming,
body: ChatCompletionCreateParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<ChatCompletion>;
create(
body: ChatCompletionCreateParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionChunk>>;
create(
body: CompletionCreateParamsBase,
body: ChatCompletionCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
create(
body: CompletionCreateParams,
body: ChatCompletionCreateParams,
options?: Core.RequestOptions,
): APIPromise<ChatCompletion> | APIPromise<Stream<ChatCompletionChunk>> {
return this.post('/chat/completions', { body, ...options, stream: body.stream ?? false }) as
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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<CreateChatCompletionRequestMessage>;
messages: Array<ChatCompletionMessageParam>;

/**
* ID of the model to use. See the
Expand Down Expand Up @@ -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<CompletionCreateParams.Function>;
functions?: Array<ChatCompletionCreateParams.Function>;

/**
* Modify the likelihood of specified tokens appearing in the completion.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions src/resources/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ export {
ChatCompletion,
ChatCompletionChunk,
ChatCompletionMessage,
ChatCompletionMessageParam,
CreateChatCompletionRequestMessage,
ChatCompletionCreateParams,
CompletionCreateParams,
ChatCompletionCreateParamsNonStreaming,
CompletionCreateParamsNonStreaming,
ChatCompletionCreateParamsStreaming,
CompletionCreateParamsStreaming,
Completions,
} from './completions';

0 comments on commit 19c99fb

Please sign in to comment.