Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openai/openai-node
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.15.4
Choose a base ref
...
head repository: openai/openai-node
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.16.0
Choose a head ref
  • 5 commits
  • 66 files changed
  • 1 contributor

Commits on Nov 6, 2023

  1. Copy the full SHA
    18b04e8 View commit details
  2. Copy the full SHA
    6e630d6 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    timyates Tim Yates
    Copy the full SHA
    1d08057 View commit details
  4. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    84b4328 View commit details
  5. release: 4.16.0

    stainless-bot committed Nov 6, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    timyates Tim Yates
    Copy the full SHA
    d4d4687 View commit details
Showing with 4,621 additions and 252 deletions.
  1. +1 −1 .release-please-manifest.json
  2. +1 −1 .stats.yml
  3. +19 −0 CHANGELOG.md
  4. +17 −9 README.md
  5. +124 −1 api.md
  6. +10 −6 build-deno
  7. +4 −4 ecosystem-tests/node-ts-cjs-auto/tests/test.ts
  8. +35 −0 examples/audio.ts
  9. +126 −0 examples/tool-call-helpers.ts
  10. +33 −8 helpers.md
  11. +1 −1 package.json
  12. +14 −0 src/index.ts
  13. +215 −49 src/lib/AbstractChatCompletionRunner.ts
  14. +22 −20 src/lib/ChatCompletionRunFunctions.test.ts
  15. +24 −7 src/lib/ChatCompletionRunner.ts
  16. +109 −21 src/lib/ChatCompletionStream.ts
  17. +20 −4 src/lib/ChatCompletionStreamingRunner.ts
  18. +12 −0 src/lib/RunnableFunction.ts
  19. +28 −0 src/lib/chatCompletionUtils.ts
  20. +2 −2 src/pagination.ts
  21. +4 −0 src/resources/audio/audio.ts
  22. +1 −0 src/resources/audio/index.ts
  23. +49 −0 src/resources/audio/speech.ts
  24. +2 −2 src/resources/audio/transcriptions.ts
  25. +2 −2 src/resources/audio/translations.ts
  26. +470 −0 src/resources/beta/assistants/assistants.ts
  27. +154 −0 src/resources/beta/assistants/files.ts
  28. +19 −0 src/resources/beta/assistants/index.ts
  29. +17 −0 src/resources/beta/beta.ts
  30. +42 −1 src/resources/beta/chat/completions.ts
  31. +17 −0 src/resources/beta/index.ts
  32. +31 −0 src/resources/beta/threads/index.ts
  33. +105 −0 src/resources/beta/threads/messages/files.ts
  34. +14 −0 src/resources/beta/threads/messages/index.ts
  35. +343 −0 src/resources/beta/threads/messages/messages.ts
  36. +23 −0 src/resources/beta/threads/runs/index.ts
  37. +535 −0 src/resources/beta/threads/runs/runs.ts
  38. +365 −0 src/resources/beta/threads/runs/steps.ts
  39. +339 −0 src/resources/beta/threads/threads.ts
  40. +13 −0 src/resources/chat/chat.ts
  41. +390 −55 src/resources/chat/completions.ts
  42. +13 −0 src/resources/chat/index.ts
  43. +20 −2 src/resources/completions.ts
  44. +1 −1 src/resources/edits.ts
  45. +2 −2 src/resources/embeddings.ts
  46. +52 −28 src/resources/files.ts
  47. +3 −3 src/resources/fine-tunes.ts
  48. +15 −3 src/resources/fine-tuning/jobs.ts
  49. +46 −5 src/resources/images.ts
  50. +9 −1 src/resources/index.ts
  51. +1 −1 src/resources/models.ts
  52. +1 −1 src/version.ts
  53. +20 −0 tests/api-resources/audio/speech.test.ts
  54. +109 −0 tests/api-resources/beta/assistants/assistants.test.ts
  55. +95 −0 tests/api-resources/beta/assistants/files.test.ts
  56. +10 −0 tests/api-resources/beta/chat/completions.test.ts
  57. +68 −0 tests/api-resources/beta/threads/messages/files.test.ts
  58. +89 −0 tests/api-resources/beta/threads/messages/messages.test.ts
  59. +131 −0 tests/api-resources/beta/threads/runs/runs.test.ts
  60. +61 −0 tests/api-resources/beta/threads/runs/steps.test.ts
  61. +98 −0 tests/api-resources/beta/threads/threads.test.ts
  62. +9 −8 tests/api-resources/chat/completions.test.ts
  63. +1 −0 tests/api-resources/completions.test.ts
  64. +9 −2 tests/api-resources/files.test.ts
  65. +1 −1 tests/api-resources/fine-tuning/jobs.test.ts
  66. +5 −0 tests/api-resources/images.test.ts
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "4.15.4"
".": "4.16.0"
}
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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 4.16.0 (2023-11-06)

Full Changelog: [v4.15.4...v4.16.0](https://github.com/openai/openai-node/compare/v4.15.4...v4.16.0)

### Features

* **api:** releases from DevDay; assistants, multimodality, tools, dall-e-3, tts, and more ([#433](https://github.com/openai/openai-node/issues/433)) ([fb92f5e](https://github.com/openai/openai-node/commit/fb92f5e6e3b6e7969b3d91f4ccdaef87e5fea0a4))


### Bug Fixes

* improve deno readme ([#429](https://github.com/openai/openai-node/issues/429)) ([871ceac](https://github.com/openai/openai-node/commit/871ceac2b37f53f7fc7c0163454115c709cd7ced))


### Documentation

* deno version ([#432](https://github.com/openai/openai-node/issues/432)) ([74bf336](https://github.com/openai/openai-node/commit/74bf3364379fd23252fde01401c44b2fa796cba4))
* update deno link in more places ([#431](https://github.com/openai/openai-node/issues/431)) ([5da63d4](https://github.com/openai/openai-node/commit/5da63d4a9143c0ab493b742f7fde22b01a372844))

## 4.15.4 (2023-11-05)

Full Changelog: [v4.15.3...v4.15.4](https://github.com/openai/openai-node/compare/v4.15.3...v4.15.4)
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,10 +18,14 @@ yarn add openai

You can import in Deno via:

<!-- x-release-please-start-version -->

```ts
import OpenAI from 'https://deno.land/x/openai';
import OpenAI from 'https://deno.land/x/openai@4.15.4/mod.ts';
```

<!-- x-release-please-end -->

## Usage

The full API of this library can be found in [api.md file](https://github.com/openai/openai-node/blob/master/api.md). The code below shows how to get started using the chat completions API.
@@ -58,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 || '');
}
}

@@ -117,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();
@@ -139,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';
125 changes: 124 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
@@ -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:
@@ -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>
@@ -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:
@@ -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>
16 changes: 10 additions & 6 deletions build-deno
Original file line number Diff line number Diff line change
@@ -5,20 +5,24 @@ set -exuo pipefail
rm -rf deno; mkdir deno
cp -rp src/* deno

PACKAGE_VERSION=$(node -p 'require("./package.json").version')

cat << EOF > deno/README.md
# OpenAI Node API Library - Deno build
This is a build produced from openai/openai-node - please go there to read the source, file issues, etc.
This is a build produced from https://github.com/openai/openai-node please go there to read the source and docs, file issues, etc.
Installation:
Usage:
\`\`\`
import OpenAI from "https://deno.land/x/openai";
\`\`\`ts
import OpenAI from "$(echo 'https://deno.land/x/openai@4.15.4/mod.ts' | sed -E s/@\.+\\//@"$PACKAGE_VERSION"\\//)";
const client = new OpenAI();
\`\`\`
Note that in many Deno environments, you can also do this:
\`\`\`
\`\`\`ts
import OpenAI from "npm:openai";
\`\`\`
EOF
@@ -33,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
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
@@ -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
@@ -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;
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