Code Monkey home page Code Monkey logo

openai-node's Introduction

openai-node

A library for using the OpenAI GPT-3 API in Node. It tries to resemble OpenAI's provided Python bindings as closely as possible but with the additional asynchronous ways of Node.

I am not affiliated with OpenAI.

All methods require that the api_key is set to something.

organization is optional for your organization ID, but if it's set to something, it'll be included in the header.

Usage

Install

npm i openai-node

Completion

Completion takes the normal GPT-3 parameters. The only one that is actually required in this library is prompt, the rest have the default values shown below.

var  openai  =  require("openai-node");

openai.api_key  =  "YOUR API KEY"; // required
openai.organization  =  "YOUR ORGANIZATION ID"; // optional



//Completion
openai.Completion.create({
	engine: "davinci",
	prompt: "Original: She no went to the market.\nStandard American English:",
	temperature: 1,
	max_tokens: 64
	top_p: 1,
	frequency_penalty: 0,
	presence_penalty: 1,
	n: 1,
	stream: false,
	logprobs: null,
	echo: false,
	best_of: 1,
	stop: null,
}).then((response) => {
	console.log(response);
	//EXAMPLE OUTPUT: She didn't go to the market.
});

Search

Search takes the normal GPT-3 parameters. documents and query must not be empty. Davinci is chosen by default.

var openai = require("openai-node");

openai.api_key = "YOUR API KEY"; // required
openai.organization = "YOUR ORGANIZATION ID"; // optional

//Search
openai
    .Engine("davinci")
    .search({
        documents: ["whitehouse", "school", "hospital"],
        query: "president",
    })
    .then((response) => {
        console.log(response);
    });
/*
OUTPUT:
{
	object: 'list',
	data: [
		{ object: 'search_result', document: 0, score: 563.445 },
		{ object: 'search_result', document: 1, score: 155.26 },
		{ object: 'search_result', document: 2, score: 241.302 }
	],
	model: 'davinci:2020-05-03'
}*/

Engine List

Engine List returns a list off all the engines GPT-3 has available. (api_key is still required)

openai.Engine.list().then((res) => console.log(res));

Engine Retrieve

Returns engine information. (api_key is still required)

openai.Engine.retrieve("ada").then((res) => console.log(res));
/*
OUTPUT:
{
	id: 'ada',
	object: 'engine',
	created: null,
	max_replicas: null,
	owner: 'openai',
	permissions: null,
	ready: true,
	ready_replicas: null,
	replicas: null
}
*/

File

List

Lists all available files that belong to your organization.

openai.File.list().then((res) => {
    console.log(res);
});

Create

Sends a new file to the endpoint and returns back it's ID for use with Search and Classification. Learn more about files at OpenAI's documention. File must be a .jsonl (JSON Lines).

const fs = require("fs");

openai.File.create({
    purpose: "answers",
    file: fs.createReadStream("./traning.jsonl"),
}).then((res) => {
    console.log(res);
    /* OUTPUT: 
	{
	  "id": "file-Twfy8T7YyIZR6jxPpIipbpxh",
	  "object": "file",
	  "bytes": 140,
	  "created_at": 1616240608,
	  "filename": "training.jsonl",
	  "purpose": "answers"
	}
	*/
});

Remove

Removes file from your organization. OpenAI's Python library calls this function delete but that's a reserved word in JS.

openai
    .File("file-Twfy8T7YyIZR6jxPpIipbpxh")
    .remove()
    .then((res) => {
        console.log(res);
    });
/* OUTPUT: 
{
  "id": "file-Twfy8T7YyIZR6jxPpIipbpxh",
  "object": "file",
  "deleted": true
}
*/

Classifications (new)

Creates a new classification request. All parameters match OpenAI's. You can see them here

openai.Classification.create({
	search_model: "ada",
	model: "curie",
	file: "file-XsOVjn4yoY2BRweTtcZB1P1B"
    query: "It is a raining day :(",
    labels: ["Positive", "Negative", "Neutral"],
}).then(res => {
	console.log(res);
})

Answer

Creates a new answer request. All parameters match OpenAI's. You can see them here

openai.Answer.create(
    (search_model: "ada"),
    (mode: "curie"),
    (question: "which puppy is happy?"),
    (documents: ["Puppy A is happy.", "Puppy B is sad."]),
    (examples_context: "In 2017, U.S. life expectancy was 78.6 years."),
    (examples: [["What is human life expectancy in the United States?", "78 years."]]),
    (max_tokens: 5),
    (stop: ["\n", "<|endoftext|>"])
);

openai-node's People

Contributors

jtams avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.