HOW TO USE THE OPEN AI API IN VANILLA JAVASCRIPT

Isaac Sichangi
4 min readMay 23, 2023

--

Artificial Intelligence

Open AI has gained popularity in the last year with the launch of Chat GPT which has made the advanced capabilities of AI accessible to anyone using their enhanced language models. The team at Open AI has developed a restful API that can be used to access the various services they have available which is accessible at https://platform.openai.com/docs/api-reference.

We decided to develop a client-side Javascript library in Vanilla JS to access the API in order for developers to be able to quickly familiarize themselves with the API. The library is accessible on GitHub at https://github.com/IsaacSichangi/OPen-AI-JavaScript-SDK.

Getting Started

Works with Javascript ES6 syntax

Ensure that you have generated your API KEY from Open AI and kindly note client-side Javascript does not support loading values from environment variables.

Installation

Add the Open Ai library to your site.

<script src="https://cdn.jsdelivr.net/gh/IsaacSichangi/OPen-AI-JavaScript-SDK@master/js/openai.js"></script>

Initialization

let openAi = new Openai("API KEY");

Models

All models are static and can be accessed using the open Ai object.


openAi.GPT_4;
openAi.GPT_4_03014;
openAi.GPT_4_32K;
openAi.GPT_4_32K_0314;
openAi.GPT_3_5_TURBO;
openAi.GPT_3_5_TURBO_0301;

///v1/completions
openAi.TEXT_DAVINCI_003;
openAi.TEXT_DAVICNCI_002;
openAi.TEXT_DAVINCI_001;
openAi.TEXT_CURIE_001;
openAi.TEXT_CABBAGE_001;
openAi.TEXT_ADA_001;

///v1/edits
openAi.TEXT_DAVINCI_EDIT_001;
openAi.CODE_DAVINCI_EDIT_001;

///v1/fine-tunes
openAi.DAVINCI;
openAi.CURIE;
openAi.BABBAGE;
openAi.ADA;
///v1/embeddings
openAi.TEXT_EMBEDDING_ADA_002;
openAi.TEXT_SEARCH_ADA_DOC_001;

///v1/moderations
openAi.TEXT_MODERATION_STABLE;
openAi.TEXT_MODERATION_LATEST;

/**
/v1/audio/transcriptions
/v1/audio/translations*
*/
openAi.WHISPER_1;

List Models

Lists the currently available models, and provides basic information about each one such as the owner and availability. https://platform.openai.com/docs/api-reference/models/list

openAi.listModels().then(function (value) {

//returns json
console.log(value);
}, function (error) {

});

Retrieve Model

Retrieves a model instance, providing basic information about the model such as the owner and permissions. https://platform.openai.com/docs/api-reference/models/retrieve

openAi.retrieveModel(MODELNAME).then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Completion

Creates a completion for the chat message. https://platform.openai.com/docs/api-reference/chat


openAi.createChatCompletion(model, messages, temperature = 1, top_p = 1, n = 1, stream = false, stop = null, max_tokens = null, presence_penalty = 0, frequency_penalty = 0, logit_bias = null, user = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Edit

Creates a new edit for the provided input, instruction, and parameters. https://platform.openai.com/docs/api-reference/edits

openAi.createEdit(model, input, instruction, n = 1, temperature = 1, top_p = 1)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Image

Creates an image given a prompt. https://platform.openai.com/docs/api-reference/images/create


openAi.createImage(prompt, n = 1, size = "1024x1024", response_format = "url", user = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Image Edit

Creates an edited or extended image given an original image and a prompt. https://platform.openai.com/docs/api-reference/images/create-edit


openAi.createImageEdit(image, mask = null, prompt, n = 1, size = "1024x1024", response_format = "url", user = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Image Variation

Creates a variation of a given image. https://platform.openai.com/docs/api-reference/images/create-variation


openAi.createImageVariation(image, n = 1, size = "1024x1024", response_format = "url", user = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Embeddings

Creates an embedding vector representing the input text. https://platform.openai.com/docs/api-reference/embeddings

openAi.createEmbeddings(model,input,  user = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Transcription

Transcribes audio into the input language. https://platform.openai.com/docs/api-reference/audio


openAi.createTranscription(file, model = Openai.WHISPER_1, prompt = null, response_format = "json", temperature = 0, language = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Translation

Translates audio into English. https://platform.openai.com/docs/api-reference/audio/create

openAi.createTranlsation (file, model = Openai.WHISPER_1, prompt = null, response_format = "json", temperature = 0)

.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

List Files

Returns a list of files that belong to the user’s organization. https://platform.openai.com/docs/api-reference/files/list

openAi.listFiles()
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Upload File

Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit. https://platform.openai.com/docs/api-reference/files/upload

openAi.uploadFile(file, purpose)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Delete File

Delete a file. https://platform.openai.com/docs/api-reference/files/delete

openAi.deleteFile(file_id)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Retrieve File

Returns information about a specific file. https://platform.openai.com/docs/api-reference/files/retrieve

openAi.retrieveFile(file_id)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Retrieve File Content

Returns the contents of the specified file https://platform.openai.com/docs/api-reference/files/retrieve-content

openAi.retrieveFileContent(file_id)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Fine Tune

Creates a job that fine-tunes a specified model from a given dataset. The response includes details of the enqueued job including job status and the name of the fine-tuned models once complete. https://platform.openai.com/docs/api-reference/fine-tunes/create

openAi.createFineTune(training_file, validation_file = null, model = Openai.CURIE, n_epochs = 4, batch_size = null, learning_rate_multiplier = null, prompt_loss_weight = 0.01, compute_classification_metrics = null, classification_n_classes = null, classification_positive_class = null, classification_betas = null, suffix = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

List Fine Tunes

List your organization’s fine-tuning jobs https://platform.openai.com/docs/api-reference/fine-tunes/list

openAi.listFineTunes()
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Retrieve Fine Tune

Gets info about the fine-tune job. https://platform.openai.com/docs/api-reference/fine-tunes/retrieve

openAi.retrieveFineTune(fine_tune_id)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Cancel Fine Tune

Immediately cancel a fine-tune job. https://platform.openai.com/docs/api-reference/fine-tunes/cancel

openAi.cancelFineTune(fine_tune_id)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

List Fine Tune Events

Get fine-grained status updates for a fine-tune job. https://platform.openai.com/docs/api-reference/fine-tunes/events

openAi.listFineTuneEvents(fine_tune_id, stream = false)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Delete Fine Tune Model

Delete a fine-tuned model. You must have the Owner role in your organization. https://platform.openai.com/docs/api-reference/fine-tunes/delete-model

openAi.deleteFineTunemodel(model)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

Create Moderation

Classifies if text violates OpenAI’s Content Policy. https://platform.openai.com/docs/api-reference/moderations/create


openAi.createModeration(input, model = null)
.then(function (value) {

//returns json
console.log(value);

}, function (error) {

});

--

--