OpenAI NodeJS

Class

OpenAI

OpenAI client library.

Constructor

new OpenAI(key, organizationopt, engineopt)

Parameters

  • key String

    API key

  • organization String <optional>
    null

    Organization ID

  • engine String <optional>
    davinci

    The engine you will use in your requests

Source

Methods

async

answer(question, bodyopt) → {Promise.<Answer>}

[BETA] Answers the specified question using the provided documents and examples.

Parameters

  • question string | array
  • body AnswerBody <optional>
    {}

Returns

  • Promise.<Answer>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

const documents = ['Puppy A is happy.', 'Puppy B is sad.'];
const examples_context = 'In 2017, U.S. life expectancy was 78.6 years.';
const examples = [
    ['What is human life expectancy in the United States?', '78 years.']
];

const stop = ['\n', '<|endoftext|>'];

client.answer('Which puppy is happy?', {documents, examples_context, examples, stop})
.then(answer => {
    console.log(`Answer: ${answer.answers[0]}`);
})
.catch(console.error);

Source

async

classificate(query, bodyopt) → {Promise.<Classification>}

[BETA] Classifies the specified query using provided examples.

Parameters

Returns

  • Promise.<Classification>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

const examples = [
    ['A happy moment', 'Positive'],
    ['I am sad.', 'Negative'],
    ['I am feeling awesome', 'Positive']
];

const labels = ['Positive', 'Negative', 'Neutral'];

client.classificate('It is a raining day :(', {examples, labels, 'search_model': 'ada', 'model': 'curie'})
.then(classification => {
    console.log(`Classification: ${classification.label}`);
})
.catch(console.error);

Source

async

complete(promptopt, bodyopt) → {Promise.<Completion>}

Creates a new completion for the provided prompt and parameters.

Parameters

  • prompt string | array <optional>
    <|endoftext|>

    The prompt(s) to generate completions for, encoded as a string, a list of strings, or a list of token lists.

  • body CompletionBody <optional>
    {}

Returns

  • Promise.<Completion>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

var prompt = 'My name is Bond';
client.complete(prompt, {stop: ['\n', '"'], temperature: 0})
.then(completion => {
    console.log(`Result: ${prompt}${completion.choices[0].text}`);
})
.catch(console.error);

Source

decode(text) → {string}

Decode keys to text.

Parameters

  • text Array.<String>

    The encoded text

Returns

  • string

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

var encoded = client.encode('Hello, world!');
var decoded = client.decode(encoded);
console.log(`Decoded: ${decoded}`);

Source

encode(text) → {Array.<String>}

Split text by keys.

Parameters

  • text String

    The string to be encoded

Returns

  • Array.<String>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

var encoded = client.encode('Hello, world!');
console.log(encoded);

Source

async

getEngine(engine) → {Promise.<Engine>}

Retrieves an engine instance, providing basic information about the engine such as the owner and availability.

Parameters

  • engine String

    The ID of the engine to use for this request

Returns

  • Promise.<Engine>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

client.getEngine('davinci')
.then(engine => {
    console.log(`Engine owner: ${engine.owner}`);
})
.catch(console.error);

Source

async

getEngines() → {Promise.<Array.<Engine>>}

Lists the currently available engines, and provides basic information about each one such as the owner and availability.

Returns

  • Promise.<Array.<Engine>>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

client.getEngines()
.then(engines => {
    console.log(`Engines: ${engines.map(engine => engine.id).join(', ')}`);
})
.catch(console.error);

Source

async

getFiles() → {Promise.<Array.<File>>}

Returns a list of files that belong to the user's organization.

Returns

  • Promise.<Array.<File>>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

client.getFiles()
.then(console.log)
.catch(console.error);

Source

async

Given a query and a set of documents or labels, the model ranks each document based on its semantic similarity to the provided query.

Parameters

  • query string | array
  • body SearchBody <optional>
    {}

Returns

  • Promise.<Search>

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

const documents = ['Dancing', 'Programming', 'Skating', 'Drawing'];

client.search('I do not like CSS', {documents})
.then(result => {
    var max_score = Math.max(...result.map(e => e.score), 0);

    console.log(`Likely subject: ${documents[result.find(e => e.score === max_score).document]} (${max_score})`);
})
.catch(console.error);

Source

tokens(text) → {Number}

Shows the token amount of the inserted text.

Parameters

  • text String

    The string to get the token amount

Returns

  • Number

Example

const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

var tokens = client.tokens('Hello, world!');
console.log(`Tokens count: ${tokens}`);

Source

async

uploadFile(file, purpose) → {File}

Upload a file that contains document(s) to be used across various endpoints/features.

Parameters

  • file string | ReadStream

    The content of the JSON to be uploaded.

  • purpose String

    The intended purpose of the uploaded documents.

Returns

  • File

Example

const fs = require('fs');
const OpenAI = require('openai-nodejs');
const client = new OpenAI('YOUR_API_KEY');

client.uploadFile(fs.createReadStream('file.jsonl'), 'answers')
.then(console.log)
.catch(console.error);

client.uploadFile('{"text": "A text here"}', 'answers')
.then(console.log)
.catch(console.error);

Source