Skip to content

Commit

Permalink
v4.0.0-beta.3
Browse files Browse the repository at this point in the history
  • Loading branch information
schnerd committed Jul 8, 2023
1 parent 492cc2e commit f35bbc5
Show file tree
Hide file tree
Showing 23 changed files with 449 additions and 184 deletions.
86 changes: 43 additions & 43 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,133 +2,133 @@

Types:

- <code><a href="./resources/completions.ts">Completion</a></code>
- <code><a href="./resources/completions.ts">CompletionChoice</a></code>
- <code><a href="./src/resources/completions.ts">Completion</a></code>
- <code><a href="./src/resources/completions.ts">CompletionChoice</a></code>

Methods:

- <code title="post /completions">client.completions.<a href="./resources/completions.ts">create</a>({ ...params }) -> Completion</code>
- <code title="post /completions">client.completions.<a href="./src/resources/completions.ts">create</a>({ ...params }) -> Completion</code>

# Chat

## Completions

Types:

- <code><a href="./resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./resources/chat/completions.ts">ChatCompletionChunk</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>

Methods:

- <code title="post /chat/completions">client.chat.completions.<a href="./resources/chat/completions.ts">create</a>({ ...params }) -> ChatCompletion</code>
- <code title="post /chat/completions">client.chat.completions.<a href="./src/resources/chat/completions.ts">create</a>({ ...params }) -> ChatCompletion</code>

# Edits

Types:

- <code><a href="./resources/edits.ts">Edit</a></code>
- <code><a href="./src/resources/edits.ts">Edit</a></code>

Methods:

- <code title="post /edits">client.edits.<a href="./resources/edits.ts">create</a>({ ...params }) -> Edit</code>
- <code title="post /edits">client.edits.<a href="./src/resources/edits.ts">create</a>({ ...params }) -> Edit</code>

# Embeddings

Types:

- <code><a href="./resources/embeddings.ts">Embedding</a></code>
- <code><a href="./src/resources/embeddings.ts">Embedding</a></code>

Methods:

- <code title="post /embeddings">client.embeddings.<a href="./resources/embeddings.ts">create</a>({ ...params }) -> Embedding</code>
- <code title="post /embeddings">client.embeddings.<a href="./src/resources/embeddings.ts">create</a>({ ...params }) -> Embedding</code>

# Files

Types:

- <code><a href="./resources/files.ts">FileContent</a></code>
- <code><a href="./resources/files.ts">FileDeleted</a></code>
- <code><a href="./resources/files.ts">FileObject</a></code>
- <code><a href="./src/resources/files.ts">FileContent</a></code>
- <code><a href="./src/resources/files.ts">FileDeleted</a></code>
- <code><a href="./src/resources/files.ts">FileObject</a></code>

Methods:

- <code title="post /files">client.files.<a href="./resources/files.ts">create</a>({ ...params }) -> FileObject</code>
- <code title="get /files/{file_id}">client.files.<a href="./resources/files.ts">retrieve</a>(fileId) -> FileObject</code>
- <code title="get /files">client.files.<a href="./resources/files.ts">list</a>() -> FileObjectsPage</code>
- <code title="delete /files/{file_id}">client.files.<a href="./resources/files.ts">del</a>(fileId) -> FileDeleted</code>
- <code title="get /files/{file_id}/content">client.files.<a href="./resources/files.ts">retrieveFileContent</a>(fileId) -> string</code>
- <code title="post /files">client.files.<a href="./src/resources/files.ts">create</a>({ ...params }) -> FileObject</code>
- <code title="get /files/{file_id}">client.files.<a href="./src/resources/files.ts">retrieve</a>(fileId) -> FileObject</code>
- <code title="get /files">client.files.<a href="./src/resources/files.ts">list</a>() -> FileObjectsPage</code>
- <code title="delete /files/{file_id}">client.files.<a href="./src/resources/files.ts">del</a>(fileId) -> FileDeleted</code>
- <code title="get /files/{file_id}/content">client.files.<a href="./src/resources/files.ts">retrieveFileContent</a>(fileId) -> string</code>

# Images

Types:

- <code><a href="./resources/images.ts">Image</a></code>
- <code><a href="./resources/images.ts">ImagesResponse</a></code>
- <code><a href="./src/resources/images.ts">Image</a></code>
- <code><a href="./src/resources/images.ts">ImagesResponse</a></code>

Methods:

- <code title="post /images/variations">client.images.<a href="./resources/images.ts">createVariation</a>({ ...params }) -> ImagesResponse</code>
- <code title="post /images/edits">client.images.<a href="./resources/images.ts">edit</a>({ ...params }) -> ImagesResponse</code>
- <code title="post /images/generations">client.images.<a href="./resources/images.ts">generate</a>({ ...params }) -> ImagesResponse</code>
- <code title="post /images/variations">client.images.<a href="./src/resources/images.ts">createVariation</a>({ ...params }) -> ImagesResponse</code>
- <code title="post /images/edits">client.images.<a href="./src/resources/images.ts">edit</a>({ ...params }) -> ImagesResponse</code>
- <code title="post /images/generations">client.images.<a href="./src/resources/images.ts">generate</a>({ ...params }) -> ImagesResponse</code>

# Audio

## Transcriptions

Types:

- <code><a href="./resources/audio/transcriptions.ts">Transcription</a></code>
- <code><a href="./src/resources/audio/transcriptions.ts">Transcription</a></code>

Methods:

- <code title="post /audio/transcriptions">client.audio.transcriptions.<a href="./resources/audio/transcriptions.ts">create</a>({ ...params }) -> Transcription</code>
- <code title="post /audio/transcriptions">client.audio.transcriptions.<a href="./src/resources/audio/transcriptions.ts">create</a>({ ...params }) -> Transcription</code>

## Translations

Types:

- <code><a href="./resources/audio/translations.ts">Translation</a></code>
- <code><a href="./src/resources/audio/translations.ts">Translation</a></code>

Methods:

- <code title="post /audio/translations">client.audio.translations.<a href="./resources/audio/translations.ts">create</a>({ ...params }) -> Translation</code>
- <code title="post /audio/translations">client.audio.translations.<a href="./src/resources/audio/translations.ts">create</a>({ ...params }) -> Translation</code>

# Moderations

Types:

- <code><a href="./resources/moderations.ts">Moderation</a></code>
- <code><a href="./resources/moderations.ts">ModerationCreateResponse</a></code>
- <code><a href="./src/resources/moderations.ts">Moderation</a></code>
- <code><a href="./src/resources/moderations.ts">ModerationCreateResponse</a></code>

Methods:

- <code title="post /moderations">client.moderations.<a href="./resources/moderations.ts">create</a>({ ...params }) -> ModerationCreateResponse</code>
- <code title="post /moderations">client.moderations.<a href="./src/resources/moderations.ts">create</a>({ ...params }) -> ModerationCreateResponse</code>

# Models

Types:

- <code><a href="./resources/models.ts">Model</a></code>
- <code><a href="./resources/models.ts">ModelDeleted</a></code>
- <code><a href="./src/resources/models.ts">Model</a></code>
- <code><a href="./src/resources/models.ts">ModelDeleted</a></code>

Methods:

- <code title="get /models/{model}">client.models.<a href="./resources/models.ts">retrieve</a>(model) -> Model</code>
- <code title="get /models">client.models.<a href="./resources/models.ts">list</a>() -> ModelsPage</code>
- <code title="delete /models/{model}">client.models.<a href="./resources/models.ts">del</a>(model) -> ModelDeleted</code>
- <code title="get /models/{model}">client.models.<a href="./src/resources/models.ts">retrieve</a>(model) -> Model</code>
- <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>

# FineTunes

Types:

- <code><a href="./resources/fine-tunes.ts">FineTune</a></code>
- <code><a href="./resources/fine-tunes.ts">FineTuneEvent</a></code>
- <code><a href="./resources/fine-tunes.ts">FineTuneEventsListResponse</a></code>
- <code><a href="./src/resources/fine-tunes.ts">FineTune</a></code>
- <code><a href="./src/resources/fine-tunes.ts">FineTuneEvent</a></code>
- <code><a href="./src/resources/fine-tunes.ts">FineTuneEventsListResponse</a></code>

Methods:

- <code title="post /fine-tunes">client.fineTunes.<a href="./resources/fine-tunes.ts">create</a>({ ...params }) -> FineTune</code>
- <code title="get /fine-tunes/{fine_tune_id}">client.fineTunes.<a href="./resources/fine-tunes.ts">retrieve</a>(fineTuneId) -> FineTune</code>
- <code title="get /fine-tunes">client.fineTunes.<a href="./resources/fine-tunes.ts">list</a>() -> FineTunesPage</code>
- <code title="post /fine-tunes/{fine_tune_id}/cancel">client.fineTunes.<a href="./resources/fine-tunes.ts">cancel</a>(fineTuneId) -> FineTune</code>
- <code title="get /fine-tunes/{fine_tune_id}/events">client.fineTunes.<a href="./resources/fine-tunes.ts">listEvents</a>(fineTuneId, { ...params }) -> FineTuneEventsListResponse</code>
- <code title="post /fine-tunes">client.fineTunes.<a href="./src/resources/fine-tunes.ts">create</a>({ ...params }) -> FineTune</code>
- <code title="get /fine-tunes/{fine_tune_id}">client.fineTunes.<a href="./src/resources/fine-tunes.ts">retrieve</a>(fineTuneId) -> FineTune</code>
- <code title="get /fine-tunes">client.fineTunes.<a href="./src/resources/fine-tunes.ts">list</a>() -> FineTunesPage</code>
- <code title="post /fine-tunes/{fine_tune_id}/cancel">client.fineTunes.<a href="./src/resources/fine-tunes.ts">cancel</a>(fineTuneId) -> FineTune</code>
- <code title="get /fine-tunes/{fine_tune_id}/events">client.fineTunes.<a href="./src/resources/fine-tunes.ts">listEvents</a>(fineTuneId, { ...params }) -> FineTuneEventsListResponse</code>
9 changes: 7 additions & 2 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ set -exuo pipefail

node scripts/check-version.cjs

yarn tsc

# Build into dist and will publish the package from there,
# so that src/resources/foo.ts becomes <package root>/resources/foo.js
# This way importing from `"openai/resources/foo"` works
Expand All @@ -12,7 +14,10 @@ rm -rf dist
mkdir dist
# Copy src to dist/src and build from dist/src into dist, so that
# the source map for index.js.map will refer to ./src/index.ts etc
cp -rp src dist
cp -rp src README.md dist
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done
# this converts the export map paths for the dist directory
# and does a few other minor things
node scripts/make-dist-package-json.cjs > dist/package.json
Expand All @@ -21,7 +26,7 @@ node scripts/make-dist-package-json.cjs > dist/package.json
tsc-multi
# copy over handwritten .js/.mjs/.d.ts files
cp src/_shims/*.{d.ts,js,mjs} dist/_shims
tsc-alias -p tsconfig.json --resolve-full-paths
tsc-alias -p tsconfig.build.json
# we need to add exports = module.exports = OpenAI Node to index.js;
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
Expand Down
14 changes: 7 additions & 7 deletions ecosystem-tests/vercel-edge/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecosystem-tests/vercel-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "20.3.1",
"@types/node": "20.3.3",
"@types/react": "18.2.13",
"@types/react-dom": "18.2.6",
"edge-runtime": "^2.4.3",
Expand Down
48 changes: 48 additions & 0 deletions examples/azure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env yarn tsn -T

import OpenAI from 'openai';

// The name of your Azure OpenAI Resource.
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
const resource = '<your resource name>';

// Corresponds to your Model deployment within your OpenAI resource, e.g. my-gpt35-16k-deployment
// Navigate to the Azure OpenAI Studio to deploy a model.
const model = '<your model>';

const apiKey = process.env['AZURE_OPENAI_API_KEY'];
if (!apiKey) {
throw new Error('The AZURE_OPENAI_API_KEY environment variable is missing or empty.');
}

// Azure OpenAI requires a custom baseURL, api-version query param, and api-key header.
const openai = new OpenAI({
apiKey,
baseURL: `https://${resource}.openai.azure.com/openai/deployments/${model}`,
defaultQuery: { 'api-version': '2023-06-01-preview' },
defaultHeaders: { 'api-key': apiKey },
});

async function main() {
console.log('Non-streaming:');
const result = await openai.chat.completions.create({
model,
messages: [{ role: 'user', content: 'Say hello!' }],
});
console.log(result.choices[0]!.message?.content);

console.log();
console.log('Streaming:');
const stream = await openai.chat.completions.create({
model,
messages: [{ role: 'user', content: 'Say hello!' }],
stream: true,
});

for await (const part of stream) {
process.stdout.write(part.choices[0]?.delta?.content ?? '');
}
process.stdout.write('\n');
}

main().catch(console.error);
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.0-beta.2",
"version": "4.0.0-beta.3",
"description": "Client library for the OpenAI API",
"author": "OpenAI <support@openai.com>",
"types": "dist/index.d.ts",
Expand Down
21 changes: 21 additions & 0 deletions scripts/replace-self-referencing-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });

const path = require('path');
const distSrcDir = path.resolve(__dirname, '..', 'dist', 'src');

function replaceSelfReferencingImports({ orig, file, config }) {
// replace self-referencing imports in source files to reduce errors users will
// see if they go to definition
if (!file.startsWith(distSrcDir)) return orig;
return orig.replace(/['"]([^"'\r\n]+)['"]/, (match, importPath) => {
if (!importPath.startsWith('openai/')) return match;
let relativePath = path.relative(
path.dirname(file),
path.join(distSrcDir, importPath.substring('openai/'.length)),
);
if (!relativePath.startsWith('.')) relativePath = `./${relativePath}`;
return JSON.stringify(relativePath);
});
}
exports.default = replaceSelfReferencingImports;
21 changes: 19 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export abstract class APIClient {
};
}

protected abstract defaultQuery(): DefaultQuery | undefined;

/**
* Override this to add your own headers validation:
*/
Expand Down Expand Up @@ -130,9 +132,17 @@ export abstract class APIClient {
const contentLength = typeof body === 'string' ? body.length.toString() : null;

const url = this.buildURL(path!, query);
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
const timeout = options.timeout ?? this.timeout;
validatePositiveInteger('timeout', timeout);
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
const minAgentTimeout = timeout + 1000;
if ((httpAgent as any)?.options && minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)) {
// Allow any given request to bump our agent active socket timeout.
// This may seem strange, but leaking active sockets should be rare and not particularly problematic,
// and without mutating agent we would need to create more of them.
// This tradeoff optimizes for performance.
(httpAgent as any).options.timeout = minAgentTimeout;
}

if (this.idempotencyHeader && method !== 'get') {
if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
Expand Down Expand Up @@ -260,6 +270,11 @@ export abstract class APIClient {
new URL(path)
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));

const defaultQuery = this.defaultQuery();
if (!isEmptyObj(defaultQuery)) {
query = { ...defaultQuery, ...query } as Req;
}

if (query) {
url.search = qs.stringify(query, this.qsOptions());
}
Expand Down Expand Up @@ -516,6 +531,7 @@ type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';

export type RequestClient = { fetch: Fetch };
export type Headers = Record<string, string | null | undefined>;
export type DefaultQuery = Record<string, string | undefined>;
export type KeysEnum<T> = { [P in keyof Required<T>]: true };

export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
Expand Down Expand Up @@ -564,6 +580,7 @@ export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Reada
};

export type APIResponse<T> = T & {
/** @deprecated - we plan to add a different way to access raw response information shortly. */
responseHeaders: Headers;
};

Expand Down
Loading

0 comments on commit f35bbc5

Please sign in to comment.