
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
node-openai
Advanced tools
An elegant Node.js library written in TypeScript for the OpenAI API. Pure JavaScript, no dependencies. Works in Node.js and the browser.
npm install node-openai
| API | Supported |
|---|---|
| Models | âś… |
| Completions | âś… |
| Chat | âś… |
| Edits | âś… |
| Images | âś… |
| Embeddings | âś… |
| Audio | âś… |
| Files | âś… |
| Fine-tunes | âś… |
| Moderations | âś… |
For Node.js (CommonJS):
const { OpenAI } = require('node-openai');
For ES Modules:
import { OpenAI } from 'node-openai';
For TypeScript:
import { OpenAI } from 'node-openai';
Create an instance of the OpenAI class:
const openai = new OpenAI({
apiKey: 'YOUR_API_KEY',
// organization: 'YOUR_ORGANIZATION_ID',
// endpoint: 'https://api.openai.com',
});
To use the OpenAI V1 API, you must call the v1() method on the client instance:
const api = openai.v1();
Check out the OpenAI V1 API docs for more information.
List all available models:
const models = await api.models.list();
Retrieve a model:
const model = await api.models.retrieve('davinci');
Delete fine-tuned model:
const model = await api.models.delete('curie:ft-acmeco-2021-03-03-21-44-20');
Create a completion:
const completion = await api.completions.create({
model: 'davinci',
prompt: 'This is a test',
max_tokens: 5,
temperature: 0.9,
stream: false,
});
If the stream option is set to true, the completion will be streamed:
const stream = await api.completions.create({
model: 'davinci',
prompt: 'This is a test',
max_tokens: 5,
temperature: 0.9,
stream: true,
});
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
// Read the stream
Create a chat:
const chat = await api.chat.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: 'Hello, how are you?'
}
]
});
Create an edit:
const edit = await api.edits.create({
model: 'text-davinci-edit-001',
input: 'I am a test',
instruction: 'Make this text funny',
});
Create an image:
const image = await api.images.create({
prompt: 'A cute baby sea otter',
n: 1,
size: '512x512',
});
Create image edit:
const imageEdit = await api.images.edit({
prompt: 'Make this image funny',
n: 1,
size: '512x512',
}, '/path/to/image.png');
Create image variation:
const imageVariation = await api.images.variation({
n: 1,
size: '512x512',
}, '/path/to/image.png');
Create an embedding:
const embedding = await api.embeddings.create({
model: 'text-embedding-ada-002',
input: 'This is a test',
});
Create transcription:
const transcription = await api.audio.createTranscription({
model: 'whisper-1',
prompt: 'This is a test',
}, '/path/to/audio.mp3');
Create translation:
const translation = await api.audio.createTranslation({
model: 'whisper-1',
prompt: 'This is a test',
}, '/path/to/audio.mp3');
List all available files:
const files = await api.files.list();
Retrieve a file:
const file = await api.files.retrieve('file-123');
Upload a file:
const file = await api.files.upload('/path/to/file.txt', 'fine-tune');
Delete a file:
const file = await api.files.delete('file-123');
Retrieve a file's contents:
const content = await api.files.retrieveContents('file-123');
Create fine-tune:
const fineTune = await api.fineTunes.create({
training_file: 'file-123',
});
List fine-tunes:
const fineTunes = await api.fineTunes.list();
Retrieve fine-tune:
const fineTune = await api.fineTunes.retrieve('ft-AF1WoRqd3aJAHsqc9NY7iL8F');
Cancel fine-tune:
const fineTune = await api.fineTunes.cancel('ft-AF1WoRqd3aJAHsqc9NY7iL8F');
List fine-tune's events:
const events = await api.fineTunes.listEvents('ft-AF1WoRqd3aJAHsqc9NY7iL8F');
Create moderation:
const moderation = await api.moderations.create({
model: 'text-moderation-stable',
input: 'This is a test',
});
FAQs
An elegant NodeJs library for openai api.
The npm package node-openai receives a total of 105 weekly downloads. As such, node-openai popularity was classified as not popular.
We found that node-openai demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.