Skip to content

Commit

Permalink
feat(api): releases from DevDay; assistants, multimodality, tools, da…
Browse files Browse the repository at this point in the history
…ll-e-3, tts, and more (#433)
  • Loading branch information
stainless-bot authored Nov 6, 2023
1 parent 74bf336 commit fb92f5e
Show file tree
Hide file tree
Showing 62 changed files with 4,585 additions and 243 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configured_endpoints: 28
configured_endpoints: 57
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ async function main() {
messages: [{ role: 'user', content: 'Say this is a test' }],
stream: true,
});
for await (const part of stream) {
process.stdout.write(part.choices[0]?.delta?.content || '');
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
}

Expand Down Expand Up @@ -121,8 +121,8 @@ async function main() {
});

// or, equivalently:
for await (const part of stream) {
process.stdout.write(part.choices[0]?.delta?.content || '');
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

const chatCompletion = await stream.finalChatCompletion();
Expand All @@ -143,14 +143,18 @@ If you need to cancel a stream, you can `break` from a `for await` loop or call

### Automated function calls

We provide a `openai.beta.chat.completions.runFunctions({…})` convenience helper for using function calls
with the `/chat/completions` endpoint which automatically calls the JavaScript functions you provide
We provide `openai.beta.chat.completions.runFunctions({…})` and `openai.beta.chat.completions.runTools({…})`
convenience helpers for using function calls with the `/chat/completions` endpoint
which automatically call the JavaScript functions you provide
and sends their results back to the `/chat/completions` endpoint,
looping as long as the model requests function calls.

If you pass a `parse` function, it will automatically parse the `arguments` for you and returns any parsing errors to the model to attempt auto-recovery. Otherwise, the args will be passed to the function you provide as a string.
If you pass a `parse` function, it will automatically parse the `arguments` for you
and returns any parsing errors to the model to attempt auto-recovery.
Otherwise, the args will be passed to the function you provide as a string.

If you pass `function_call: {name: …}` instead of `auto`, it returns immediately after calling that function (and only loops to auto-recover parsing errors).
If you pass `function_call: {name: …}` or `tool_call: {function: {name: …}}` instead of `auto`,
it returns immediately after calling that function (and only loops to auto-recover parsing errors).

```ts
import OpenAI from 'openai';
Expand Down
125 changes: 124 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ Methods:
Types:

- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionAssistantMessageParam</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionContentPart</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionContentPartImage</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionContentPartText</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionFunctionCallOption</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionFunctionMessageParam</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">ChatCompletionMessageToolCall</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionNamedToolChoice</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionRole</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionSystemMessageParam</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionTool</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionToolChoiceOption</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionToolMessageParam</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionUserMessageParam</a></code>
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>

Methods:
Expand Down Expand Up @@ -60,7 +73,7 @@ Methods:

- <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="get /files">client.files.<a href="./src/resources/files.ts">list</a>({ ...params }) -> 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">retrieveContent</a>(fileId) -> string</code>
- <code>client.files.<a href="./src/resources/files.ts">waitForProcessing</a>(id, { pollInterval = 5000, maxWait = 30 _ 60 _ 1000 }) -> Promise&lt;FileObject&gt;</code>
Expand Down Expand Up @@ -100,6 +113,12 @@ Methods:

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

## Speech

Methods:

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

# Moderations

Types:
Expand Down Expand Up @@ -166,4 +185,108 @@ Methods:
Methods:

- <code>client.beta.chat.completions.<a href="./src/resources/beta/chat/completions.ts">runFunctions</a>(body, options?) -> ChatCompletionRunner | ChatCompletionStreamingRunner</code>
- <code>client.beta.chat.completions.<a href="./src/resources/beta/chat/completions.ts">runTools</a>(body, options?) -> ChatCompletionRunner | ChatCompletionStreamingRunner</code>
- <code>client.beta.chat.completions.<a href="./src/resources/beta/chat/completions.ts">stream</a>(body, options?) -> ChatCompletionStream</code>

## Assistants

Types:

- <code><a href="./src/resources/beta/assistants/assistants.ts">Assistant</a></code>
- <code><a href="./src/resources/beta/assistants/assistants.ts">AsssitantDeleted</a></code>

Methods:

- <code title="post /assistants">client.beta.assistants.<a href="./src/resources/beta/assistants/assistants.ts">create</a>({ ...params }) -> Assistant</code>
- <code title="get /assistants/{assistant_id}">client.beta.assistants.<a href="./src/resources/beta/assistants/assistants.ts">retrieve</a>(assistantId) -> Assistant</code>
- <code title="post /assistants/{assistant_id}">client.beta.assistants.<a href="./src/resources/beta/assistants/assistants.ts">update</a>(assistantId, { ...params }) -> Assistant</code>
- <code title="get /assistants">client.beta.assistants.<a href="./src/resources/beta/assistants/assistants.ts">list</a>({ ...params }) -> AssistantsPage</code>
- <code title="delete /assistants/{assistant_id}">client.beta.assistants.<a href="./src/resources/beta/assistants/assistants.ts">del</a>(assistantId) -> AsssitantDeleted</code>

### Files

Types:

- <code><a href="./src/resources/beta/assistants/files.ts">AssistantFile</a></code>
- <code><a href="./src/resources/beta/assistants/files.ts">FileDeleteResponse</a></code>

Methods:

- <code title="post /assistants/{assistant_id}/files">client.beta.assistants.files.<a href="./src/resources/beta/assistants/files.ts">create</a>(assistantId, { ...params }) -> AssistantFile</code>
- <code title="get /assistants/{assistant_id}/files/{file_id}">client.beta.assistants.files.<a href="./src/resources/beta/assistants/files.ts">retrieve</a>(assistantId, fileId) -> AssistantFile</code>
- <code title="get /assistants/{assistant_id}/files">client.beta.assistants.files.<a href="./src/resources/beta/assistants/files.ts">list</a>(assistantId, { ...params }) -> AssistantFilesPage</code>
- <code title="delete /assistants/{assistant_id}/files/{file_id}">client.beta.assistants.files.<a href="./src/resources/beta/assistants/files.ts">del</a>(assistantId, fileId) -> FileDeleteResponse</code>

## Threads

Types:

- <code><a href="./src/resources/beta/threads/threads.ts">Thread</a></code>
- <code><a href="./src/resources/beta/threads/threads.ts">ThreadDeleted</a></code>

Methods:

- <code title="post /threads">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">create</a>({ ...params }) -> Thread</code>
- <code title="get /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">retrieve</a>(threadId) -> Thread</code>
- <code title="post /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">update</a>(threadId, { ...params }) -> Thread</code>
- <code title="delete /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">del</a>(threadId) -> ThreadDeleted</code>
- <code title="post /threads/runs">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRun</a>({ ...params }) -> Run</code>

### Runs

Types:

- <code><a href="./src/resources/beta/threads/runs/runs.ts">RequiredActionFunctionToolCall</a></code>
- <code><a href="./src/resources/beta/threads/runs/runs.ts">Run</a></code>

Methods:

- <code title="post /threads/{thread_id}/runs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">create</a>(threadId, { ...params }) -> Run</code>
- <code title="get /threads/{thread_id}/runs/{run_id}">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">retrieve</a>(threadId, runId) -> Run</code>
- <code title="post /threads/{thread_id}/runs/{run_id}">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">update</a>(threadId, runId, { ...params }) -> Run</code>
- <code title="get /threads/{thread_id}/runs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">list</a>(threadId, { ...params }) -> RunsPage</code>
- <code title="post /threads/{thread_id}/runs/{run_id}/cancel">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">cancel</a>(threadId, runId) -> Run</code>
- <code title="post /threads/{thread_id}/runs/{run_id}/submit_tool_outputs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputs</a>(threadId, runId, { ...params }) -> Run</code>

#### Steps

Types:

- <code><a href="./src/resources/beta/threads/runs/steps.ts">CodeToolCall</a></code>
- <code><a href="./src/resources/beta/threads/runs/steps.ts">FunctionToolCall</a></code>
- <code><a href="./src/resources/beta/threads/runs/steps.ts">MessageCreationStepDetails</a></code>
- <code><a href="./src/resources/beta/threads/runs/steps.ts">RetrievalToolCall</a></code>
- <code><a href="./src/resources/beta/threads/runs/steps.ts">RunStep</a></code>
- <code><a href="./src/resources/beta/threads/runs/steps.ts">ToolCallsStepDetails</a></code>

Methods:

- <code title="get /threads/{thread_id}/runs/{run_id}/steps/{step_id}">client.beta.threads.runs.steps.<a href="./src/resources/beta/threads/runs/steps.ts">retrieve</a>(threadId, runId, stepId) -> RunStep</code>
- <code title="get /threads/{thread_id}/runs/{run_id}/steps">client.beta.threads.runs.steps.<a href="./src/resources/beta/threads/runs/steps.ts">list</a>(threadId, runId, { ...params }) -> RunStepsPage</code>

### Messages

Types:

- <code><a href="./src/resources/beta/threads/messages/messages.ts">MessageContentImageFile</a></code>
- <code><a href="./src/resources/beta/threads/messages/messages.ts">MessageContentText</a></code>
- <code><a href="./src/resources/beta/threads/messages/messages.ts">ThreadMessage</a></code>
- <code><a href="./src/resources/beta/threads/messages/messages.ts">ThreadMessageDeleted</a></code>

Methods:

- <code title="post /threads/{thread_id}/messages">client.beta.threads.messages.<a href="./src/resources/beta/threads/messages/messages.ts">create</a>(threadId, { ...params }) -> ThreadMessage</code>
- <code title="get /threads/{thread_id}/messages/{message_id}">client.beta.threads.messages.<a href="./src/resources/beta/threads/messages/messages.ts">retrieve</a>(threadId, messageId) -> ThreadMessage</code>
- <code title="post /threads/{thread_id}/messages/{message_id}">client.beta.threads.messages.<a href="./src/resources/beta/threads/messages/messages.ts">update</a>(threadId, messageId, { ...params }) -> ThreadMessage</code>
- <code title="get /threads/{thread_id}/messages">client.beta.threads.messages.<a href="./src/resources/beta/threads/messages/messages.ts">list</a>(threadId, { ...params }) -> ThreadMessagesPage</code>

#### Files

Types:

- <code><a href="./src/resources/beta/threads/messages/files.ts">MessageFile</a></code>

Methods:

- <code title="get /threads/{thread_id}/messages/{message_id}/files/{file_id}">client.beta.threads.messages.files.<a href="./src/resources/beta/threads/messages/files.ts">retrieve</a>(threadId, messageId, fileId) -> MessageFile</code>
- <code title="get /threads/{thread_id}/messages/{message_id}/files">client.beta.threads.messages.files.<a href="./src/resources/beta/threads/messages/files.ts">list</a>(threadId, messageId, { ...params }) -> MessageFilesPage</code>
2 changes: 1 addition & 1 deletion build-deno
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ done
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" deno; fi
done
npm exec ts-node -- scripts/denoify.ts
npm exec ts-node -T -- scripts/denoify.ts
deno fmt deno
deno check deno/mod.ts
if [ -e deno_tests ]; then
Expand Down
8 changes: 4 additions & 4 deletions ecosystem-tests/node-ts-cjs-auto/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ it(`streaming works`, async function () {
it(`ChatCompletionStream works`, async function () {
const chunks: OpenAI.Chat.ChatCompletionChunk[] = [];
const contents: [string, string][] = [];
const messages: OpenAI.Chat.ChatCompletionMessage[] = [];
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [];
const chatCompletions: OpenAI.Chat.ChatCompletion[] = [];
let finalContent: string | undefined;
let finalMessage: OpenAI.Chat.ChatCompletionMessage | undefined;
let finalMessage: OpenAI.Chat.ChatCompletionMessageParam | undefined;
let finalChatCompletion: OpenAI.Chat.ChatCompletion | undefined;

const stream = client.beta.chat.completions
Expand Down Expand Up @@ -113,10 +113,10 @@ it(`ChatCompletionStream works`, async function () {
it(`aborting ChatCompletionStream works`, async function () {
const chunks: OpenAI.Chat.ChatCompletionChunk[] = [];
const contents: [string, string][] = [];
const messages: OpenAI.Chat.ChatCompletionMessage[] = [];
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [];
const chatCompletions: OpenAI.Chat.ChatCompletion[] = [];
let finalContent: string | undefined;
let finalMessage: OpenAI.Chat.ChatCompletionMessage | undefined;
let finalMessage: OpenAI.Chat.ChatCompletionMessageParam | undefined;
let finalChatCompletion: OpenAI.Chat.ChatCompletion | undefined;
let emittedError: any;
let caughtError: any;
Expand Down
35 changes: 35 additions & 0 deletions examples/audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env -S npm run tsn -T

import OpenAI, { toFile } from 'openai';
import fs from 'fs/promises';
import path from 'path';

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

const speechFile = path.resolve(__dirname, './speech.mp3');

async function main() {
const mp3 = await openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: 'the quick brown fox jumped over the lazy dogs',
});

const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.writeFile(speechFile, buffer);

const transcription = await openai.audio.transcriptions.create({
file: await toFile(buffer, 'speech.mp3'),
model: 'whisper-1',
});
console.log(transcription.text);

const translation = await openai.audio.translations.create({
file: await toFile(buffer, 'speech.mp3'),
model: 'whisper-1',
});
console.log(translation.text);
}

main();
Loading

0 comments on commit fb92f5e

Please sign in to comment.