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

openai-managed-chat-completion

v1.1.5

Published

A simple library to manage a chat completion with OpenAI

Downloads

82

Readme

OpenAI Managed Chat Completion

A TypeScript project that provides a fully managed class, with history (context from you previous prompt) is supported, for chatting with the OpenAI GPT model. It uses the OpenAI API to communicate with the GPT model and provides two modes of interaction: one-time completions and streaming completions. There aslo is a CLI interface that allows you to chat with the GPT model in the command line in demo.ts.

Installation from package manager

Yarn

$ yarn add openai-managed-chat-completion

NPM

$ npm install openai-managed-chat-completion

Installation from repository

  1. Clone the repository:
$ git clone https://github.com/LucasALLOIN/openai-managed-chat-completion.git
  1. Install dependencies:
$ cd openai-managed-chat-completion
$ yarn
  1. (only for demo cli) Set your OpenAI API key as an environment variable in .env:
OPENAI_API_KEY=<YOUR_API_KEY>

Developer Documentation

The main class in this project is GPTCompletion, which handles creating chat completions with the OpenAI API. It defines methods for getting completions both with and without streaming.

Constructor

constructor(openai: OpenAIApi, model = "gpt-3.5-turbo", options: GPTCompletionOptions = {})

Creates a new GPTCompletion instance.

  • openai: An instance of OpenAIApi from the openai package.
  • model: The name of the GPT model to use. Defaults to "gpt-3.5-turbo".

Methods

async getCompletion(prompt: string): Promise<ChatCompletionRequestMessage>

Gets a one-time completion from the GPT model.

  • prompt: The prompt to use for the completion.

Returns a Promise that resolves to the ChatCompletionRequestMessage openai response.

async getStreamCompletion(prompt: string): Promise<Readable>

Gets a streaming completion from the GPT model.

  • prompt: The prompt to use for the completion.

Returns a Promise that resolves to a Readable stream of completed data.

parseStreamCompletion(chunk: Buffer, onParsed?: (parsedData: ParsedData) => void, onFinished?: () => void, onError?: (error: any) => void)

Parses a streamed response from the OpenAI API.

  • chunk: The streamed response chunk to parse.
  • onParsed: A callback function to call when the parsed data is available.
  • onFinished: A callback function to call when the streaming response is finished.
  • onError: A callback function to call when an error occurs while parsing the streamed response.

Example Usage

import {OpenAIApi} from "openai";
import GPTCompletion from "./GPTCompletion";

const openai = new OpenAIApi(process.env.OPENAI_API_KEY);

const gpt = new GPTCompletion(openai);

const completion = gpt.getCompletion("Hello, GPT!");

completion.then(result => {
  console.log(result); // "Hi there!"
});

const stream = gpt.getStreamCompletion("Tell me about yourself.");

stream.on("data", (chunk: Buffer) => {
  gpt.parseStreamCompletion(chunk, (parsedData) => {
    console.log(parsedData);
  });
});

Dependencies

  • dotenv: A zero-dependency module that loads environment variables from a .env file.
  • openai: A client library for the OpenAI API.
  • nodemon: A tool for automatically restarting the server when changes are made.
  • ts-node: A tool that executes TypeScript files directly, without compilation.
  • typescript: A superset of JavaScript that adds typed variables and other features.
  • rollup: A module bundler for JavaScript.
  • rollup-plugin-node-resolve: A Rollup plugin that locates modules using the Node resolution algorithm.
  • rollup-plugin-typescript2: A Rollup plugin that uses the TypeScript compiler to transpile TypeScript files.