npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dalenguyen/openai

v0.11.3

Published

TypesScript library for OpenAI

Downloads

34

Readme

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!