Skip to content

Commit

Permalink
v4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
schnerd committed Aug 22, 2023
1 parent b0c9fdb commit ddb2815
Show file tree
Hide file tree
Showing 20 changed files with 665 additions and 42 deletions.
1 change: 0 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@

## Changes being requested


## Additional context & links
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configured_endpoints: 23
configured_endpoints: 28
17 changes: 17 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ Methods:
- <code title="get /models">client.models.<a href="./src/resources/models.ts">list</a>() -> ModelsPage</code>
- <code title="delete /models/{model}">client.models.<a href="./src/resources/models.ts">del</a>(model) -> ModelDeleted</code>

# FineTuning

## Jobs

Types:

- <code><a href="./src/resources/fine-tuning/jobs.ts">FineTuningJob</a></code>
- <code><a href="./src/resources/fine-tuning/jobs.ts">FineTuningJobEvent</a></code>

Methods:

- <code title="post /fine_tuning/jobs">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">create</a>({ ...params }) -> FineTuningJob</code>
- <code title="get /fine_tuning/jobs/{fine_tuning_job_id}">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">retrieve</a>(fineTuningJobId) -> FineTuningJob</code>
- <code title="get /fine_tuning/jobs">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">list</a>({ ...params }) -> FineTuningJobsPage</code>
- <code title="post /fine_tuning/jobs/{fine_tuning_job_id}/cancel">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">cancel</a>(fineTuningJobId) -> FineTuningJob</code>
- <code title="get /fine_tuning/jobs/{fine_tuning_job_id}/events">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">listEvents</a>(fineTuningJobId, { ...params }) -> FineTuningJobEventsPage</code>

# FineTunes

Types:
Expand Down
114 changes: 114 additions & 0 deletions examples/chat-params-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI from 'openai';
import { Stream } from 'openai/streaming';

// gets API Key from environment variable OPENAI_API_KEY
const openai = new OpenAI();

async function main() {
// ---------------- Explicit non-streaming params ------------

const params: OpenAI.Chat.CompletionCreateParams = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
};
const completion = await openai.chat.completions.create(params);
console.log(completion.choices[0]?.message?.content);

// ---------------- Explicit streaming params ----------------

const streamingParams: OpenAI.Chat.CompletionCreateParams = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
stream: true,
};

const stream = await openai.chat.completions.create(streamingParams);
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
process.stdout.write('\n');

// ---------------- Explicit (non)streaming types ----------------

const params1: OpenAI.Chat.CompletionCreateParamsNonStreaming = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
};

const params2: OpenAI.Chat.CompletionCreateParamsStreaming = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
stream: true,
};

// ---------------- Implicit params type -------------------

// Note: the `as const` is required here so that TS can properly infer
// the right params type.
//
// If you didn't include it then you'd also get an error saying that
// `role: string` is not assignable.
const streamingParams2 = {
model: 'gpt-4',
messages: [{ role: 'user', 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);
for await (const chunk of stream2) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
process.stdout.write('\n');

// Without the `as const` for `stream`.
const streamingParams3 = {
model: 'gpt-4',
messages: [{ role: 'user' as const, content: 'Say this is a test!' }],
stream: true,
};

// TS doesn't know if this is a `Stream` or a direct response
const response = await openai.chat.completions.create(streamingParams3);
if (response instanceof Stream) {
// here TS knows the response type is a `Stream`
} else {
// here TS knows the response type is a `ChatCompletion`
}

// ---------------- Dynamic params type -------------------

// TS knows this is a `Stream`
const streamParamsFromFn = await createCompletionParams(true);
const streamFromFn = await openai.chat.completions.create(streamParamsFromFn);
console.log(streamFromFn);

// TS knows this is a `ChatCompletion`
const paramsFromFn = await createCompletionParams(false);
const completionFromFn = await openai.chat.completions.create(paramsFromFn);
console.log(completionFromFn);
}

// Dynamically construct the params object while retaining whether or
// not the response will be streamed.
export async function createCompletionParams(
stream: true,
): Promise<OpenAI.Chat.CompletionCreateParamsStreaming>;
export async function createCompletionParams(
stream: false,
): Promise<OpenAI.Chat.CompletionCreateParamsNonStreaming>;
export async function createCompletionParams(stream: boolean): Promise<OpenAI.Chat.CompletionCreateParams> {
const params = {
model: 'gpt-3.5-turbo',
messages: [{ role: 'user' as const, content: 'Hello!' }],
stream: stream,
};

// <your logic here>

return params;
}

main();
31 changes: 31 additions & 0 deletions examples/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI from 'openai';

// gets API Key from environment variable OPENAI_API_KEY
const openai = new OpenAI();

async function main() {
// Explicit non streaming params type:
const params: OpenAI.Chat.CompletionCreateParams = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
};
const completion = await openai.chat.completions.create(params);
console.log(completion.choices[0]?.message?.content);

// Explicit streaming params type:
const streaming_params: OpenAI.Chat.CompletionCreateParams = {
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test!' }],
stream: true,
};

const stream = await openai.chat.completions.create(streaming_params);
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
process.stdout.write('\n');
}

main();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openai",
"version": "4.0.1",
"version": "4.1.0",
"description": "Client library for the OpenAI API",
"author": "OpenAI <[email protected]>",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ declare const navigator: { userAgent: string } | undefined;

// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
function getBrowserInfo(): BrowserInfo | null {
if (!navigator || typeof navigator === 'undefined') {
if (typeof navigator === 'undefined' || !navigator) {
return null;
}

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class OpenAI extends Core.APIClient {
audio: API.Audio = new API.Audio(this);
moderations: API.Moderations = new API.Moderations(this);
models: API.Models = new API.Models(this);
fineTuning: API.FineTuning = new API.FineTuning(this);
fineTunes: API.FineTunes = new API.FineTunes(this);

protected override defaultQuery(): Core.DefaultQuery | undefined {
Expand Down Expand Up @@ -248,6 +249,8 @@ export namespace OpenAI {
export import ModelDeleted = API.ModelDeleted;
export import ModelsPage = API.ModelsPage;

export import FineTuning = API.FineTuning;

export import FineTunes = API.FineTunes;
export import FineTune = API.FineTune;
export import FineTuneEvent = API.FineTuneEvent;
Expand Down
10 changes: 6 additions & 4 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Completions extends APIResource {
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionChunk>>;
create(
body: CompletionCreateParams,
body: CompletionCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
create(
Expand Down Expand Up @@ -278,7 +278,9 @@ export namespace CreateChatCompletionRequestMessage {
}
}

export interface CompletionCreateParams {
export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;

export interface CompletionCreateParamsBase {
/**
* 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).
Expand Down Expand Up @@ -441,7 +443,7 @@ export namespace CompletionCreateParams {
export type CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming;
}

export interface CompletionCreateParamsNonStreaming extends CompletionCreateParams {
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
/**
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be
* sent as data-only
Expand All @@ -453,7 +455,7 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
stream?: false | null;
}

export interface CompletionCreateParamsStreaming extends CompletionCreateParams {
export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
/**
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be
* sent as data-only
Expand Down
12 changes: 8 additions & 4 deletions src/resources/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Completions extends APIResource {
options?: Core.RequestOptions,
): APIPromise<Stream<Completion>>;
create(
body: CompletionCreateParams,
body: CompletionCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<Completion> | Completion>;
create(
Expand Down Expand Up @@ -112,7 +112,9 @@ export interface CompletionUsage {
total_tokens: number;
}

export interface CompletionCreateParams {
export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;

export interface CompletionCreateParamsBase {
/**
* ID of the model to use. You can use the
* [List models](/docs/api-reference/models/list) API to see all of your available
Expand All @@ -121,6 +123,8 @@ export interface CompletionCreateParams {
*/
model:
| (string & {})
| 'babbage-002'
| 'davinci-002'
| 'text-davinci-003'
| 'text-davinci-002'
| 'text-davinci-001'
Expand Down Expand Up @@ -272,7 +276,7 @@ export namespace CompletionCreateParams {
export type CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming;
}

export interface CompletionCreateParamsNonStreaming extends CompletionCreateParams {
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
/**
* Whether to stream back partial progress. If set, tokens will be sent as
* data-only
Expand All @@ -284,7 +288,7 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
stream?: false | null;
}

export interface CompletionCreateParamsStreaming extends CompletionCreateParams {
export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
/**
* Whether to stream back partial progress. If set, tokens will be sent as
* data-only
Expand Down
6 changes: 2 additions & 4 deletions src/resources/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ export interface FileCreateParams {
* Name of the [JSON Lines](https://jsonlines.readthedocs.io/en/latest/) file to be
* uploaded.
*
* If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt"
* and "completion" fields representing your
* [training examples](/docs/guides/fine-tuning/prepare-training-data).
* If the `purpose` is set to "fine-tune", the file will be used for fine-tuning.
*/
file: Uploadable;

/**
* The intended purpose of the uploaded documents.
*
* Use "fine-tune" for [Fine-tuning](/docs/api-reference/fine-tunes). This allows
* Use "fine-tune" for [fine-tuning](/docs/api-reference/fine-tuning). This allows
* us to validate the format of the uploaded file.
*/
purpose: string;
Expand Down
Loading

0 comments on commit ddb2815

Please sign in to comment.