npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-openai

v0.5.2

Published

An elegant NodeJs library for openai api.

Downloads

236

Readme

node-openai

An elegant Node.js library written in TypeScript for the OpenAI API. Pure JavaScript, no dependencies. Works in Node.js and the browser.

npm npm GitHub GitHub Workflow Status Libraries.io dependency status for GitHub repo

Installation

npm install node-openai

Features

| API | Supported | | --- | --- | | Models | ✅ | | Completions | ✅ | | Chat | ✅ | | Edits | ✅ | | Images | ✅ | | Embeddings | ✅ | | Audio | ✅ | | Files | ✅ | | Fine-tunes | ✅ | | Moderations | ✅ |

Example

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',
});

V1 API

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.

Models

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');

Completions

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

Chat

Create a chat:

const chat = await api.chat.create({
    model: 'gpt-3.5-turbo',
    messages: [
        {
            role: 'user',
            content: 'Hello, how are you?'
        }
    ]
});

Edits

Create an edit:

const edit = await api.edits.create({
    model: 'text-davinci-edit-001',
    input: 'I am a test',
    instruction: 'Make this text funny',
});

Images

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');

Embeddings

Create an embedding:

const embedding = await api.embeddings.create({
    model: 'text-embedding-ada-002',
    input: 'This is a test',
});

Audio

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');

Files

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');

Fine-tunes

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');

Moderations

Create moderation:

const moderation = await api.moderations.create({
    model: 'text-moderation-stable',
    input: 'This is a test',
});