Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dalenguyen/openai

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dalenguyen/openai

TypesScript library for OpenAI

  • 0.11.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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 })

// Response
//
// {
// "status": "success",
// "filePath": "abspath/converted.jsonl",
// "fileName": "converted.jsonl"
// }

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 })

// Response
//
// {
// "status": "success",
// "filePath": "abspath/converted.jsonl",
// "fileName": "converted.jsonl"
// }

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) // true or false

Contributions

Feel free to report bugs and make feature requests in the Issue Tracker, fork and create pull requests!

Keywords

FAQs

Package last updated on 30 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc