OpenAI TypeScript
TypesScript library for OpenAI
Note: this project is not affiliated with OpenAI in any way, and this was written purely out of interest.
Safe key practices: DO NOT reveal you OpenAI API Key publicly or on client site. Put it in a safe place such as in the environment or Secret Manager.
Development
The project is a monorepo using Nx Workspace. OpenAI library is under libs/openai
folder.
Before testing the package, you should rename .env-template
to .env
then add your OPENAI_API_KEY
to the environment file.
After you clone & modify the package. You can run the test to make sure everything passes.
yarn test openai
Getting started
npm i @dalenguyen/openai
OR
yarn add @dalenguyen/openai
Usages
import { OpenAI } from '@dalenguyen/openai'
const openAI = new OpenAI(process.env.OPENAI_API_KEY)
Get Engines
openAI
.engines()
.then((res) => console.log(res))
.catch((error) => console.error(error))
Create Completions
Create completion from engine
import { CompletionRequest, EngineName } from '@dalenguyen/openai'
const completionRequest: CompletionRequest = {
prompt: `Once upon a time...`,
temperature: 0,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.0,
stop: ['\n'],
}
openAI
.createCompletion(EngineName.Ada, completionRequest)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Create completion from fine-tune model
openAI
.createCompletionFromModel(completionRequest)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Create Answer
import { AnswerRequest, EngineName, OpenAI } from '@dalenguyen/openai'
...
const question: AnswerRequest = {
documents: [
"Puppy A is happy.",
"Puppy B is sad."
],
model: EngineName.Curie,
question: 'which puppy is happy?',
examples: [
[
"What is human life expectancy in the United States?",
"78 years."
]
],
examples_context: "In 2017, U.S. life expectancy was 78.6 years.",
}
openAI.createAnswer(question)
.then(res => console.log(res))
.catch(error => console.error(error))
Text Conversion
Text 2 JSONL - Answer
import { text2JsonlFile, FileData } from '@dalenguyen/openai'
const data: FileData[] = [
{
text: 'This is first sentence. The is second sentence',
},
]
const savedFile = text2JsonlFile({ data })
Text 2 JSONL - Fine Tune
import { text2JsonlFile, FileData, FilePurpose } from '@dalenguyen/openai'
const data: FileData[] = [
{
prompt: 'How about return or refund policy?',
completion: 'Due to the nature of digital products, which cannot be returned, we will not offer any refunds.',
},
{
prompt: 'How can i see the reviews?',
completion: 'There is no review at this moment',
},
]
const savedFile = text2JsonlFile({ data, purpose: FilePurpose.Finetune })
Files
List files
openAI
.listFiles()
.then((res) => console.log(res))
.catch((error) => console.error(error))
Upload File
import { FilePurpose } from '@dalenguyen/openai'
openAI
.uploadFile({
purpose: FilePurpose.Answers,
file: 'FILE_PATH',
})
.then((res) => console.log(res))
.catch((error) => console.error(error))
Delete File
openAI
.deleteFile(fileId)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Classifications
Create Classification
const classificationRequest: ClassificationRequest = {
examples: [
['A happy moment', 'Positive'],
['I am sad.', 'Negative'],
['I am feeling awesome', 'Positive'],
],
query: 'It is a raining day :(',
search_model: 'ada',
model: 'curie',
labels: ['Positive', 'Negative', 'Neutral'],
}
openAI
.createClassification(classificationRequest)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Fine-tune
List fine-tunes
openAI
.listFinetunes()
.then((res) => console.log(res))
.catch((error) => console.error(error))
List fine-tune events
openAI
.listFinetuneEvents(finetuneId)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Cancel fine-tune
openAI
.cancelFinetune(finetuneId)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Create fine-tune
openAI
.createFinetune({ training_file: 'file-id' })
.then((res) => console.log(res))
.catch((error) => console.error(error))
Retrieve fine-tune
openAI
.retrieveFinetune(finetuneId)
.then((res) => console.log(res))
.catch((error) => console.error(error))
Content Filter
This will help to check if user's input is "safe" or not. This is based on Content filter from OpenAI.
const content = await openAI.contentFilter({ prompt: `You're a big pig!` })
const accepted = isContentSafe(response)
Contributions
Feel free to report bugs and make feature requests in the Issue Tracker, fork and create pull requests!