Hi everyone,
Just want to share an NPM package that I wrote for personal projects that supports OpenAI & TypeScript. I know that we have another JavaScript support package. And it doesn’t hurt when we have another one with type support for TypeScript lovers.
You can read more about the package at @dalenguyen/openai - npm
Here are some example usages
Install the package
npm i @dalenguyen/openai
OR
yarn add @dalenguyen/openai
Initialize OpenAI
import { OpenAI } from '@dalenguyen/openai'
const openAI = new OpenAI(process.env.OPENAI_API_KEY)
Create Completions
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 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 2 JSONL Conversion
import { text2JsonlFile } from '@dalenguyen/openai'
const text = 'This is first sentence. The is second sentence'
const savedFile = text2JsonlFile(text)
// Response
//
// {
// "status": "success",
// "filePath": "abspath/converted.jsonl",
// "fileName": "converted.jsonl"
// }
Content Filter
This will help to check if the 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) // true or false
If you are interested, please give it a try. I’m actively managing the package. Feel free to give any suggestions & feedback