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-chatgpt-api

v1.1.2

Published

JavaScript library to access the official OpenAI chat completion API

Downloads

199

Readme

About

The ChatGPT JavaScript class provides an interface for interacting with the OpenAI Chat Completion API. It takes an API access token and an optional set of options for configuring the model and chat behavior. The complete method sends a message to the API and returns the response. The code is designed to be used in a browser environment and can be used to build chatbots or other conversational AI applications.

// Require the ChatGPT class
const ChatGPT = require("opeanai-chatgpt-api");

async function chatWithBot() {
  // Instantiate a new ChatGPT object with your API access token
  const chatgpt = new ChatGPT("your_api_access_token_here");

  // Define a user prompt for the chatbot
  const prompt = "Hi, how can I help you today?";

  try {
    // Call the complete method with the prompt
    const response = await chatgpt.complete(prompt);

    // Log the chatbot's response to the console
    console.log(response);
  } catch (error) {
    // Handle any errors that occur
    console.error(error);
  }
}

// Call the chatWithBot function
chatWithBot();

Options

When we instantiate a new ChatGPT object, we pass in the options object as the second parameter along with our OpenAI API access token. This tells the chatbot to use these messages as the initial part of the conversation.

// Require the ChatGPT class
const ChatGPT = require("opeanai-chatgpt-api");

async function chatWithBot() {
  // Define options for the ChatGPT model
  const options = {
    messages: [
      { role: "system", content: "Welcome to our chatbot!" },
      { role: "system", content: "Please enter your name to get started." },
    ],
  };

  // Instantiate a new ChatGPT object with your API access token and options
  const chatgpt = new ChatGPT("your_api_access_token_here", options);

  // Define a user prompt for the chatbot
  const prompt = "My name is John.";

  try {
    // Call the complete method with the prompt
    const response = await chatgpt.complete(prompt);

    // Log the chatbot's response to the console
    console.log(response);
  } catch (error) {
    // Handle any errors that occur
    console.error(error);
  }
}

// Call the chatWithBot function
chatWithBot();

Here is a list of the full range of options available in the ChatGPT class:

options.messages

  • Type: Array
  • Required: No
  • Default value: [{ role: 'system', content: '' }]
  • Description: The user prompts.

options.model

  • Type: string
  • Required: No
  • Default value: "gpt-3.5-turbo"
  • Description: Which machine learning model to use.

options.temperature

  • Type: number
  • Required: No
  • Description: What sampling temperature to use.

options.top_p

  • Type: number
  • Required: No
  • Description: An alternative to sampling with temperature.

options.n

  • Type: number
  • Required: No
  • Description: How many chat completion choices to generate for each input message.

options.stream

  • Type: boolean
  • Required: No
  • Description: Should send partial message deltas?

options.stop

  • Type: number
  • Required: No
  • Description: How many tokens to stop the chat completion after.

options.max_tokens

  • Type: number
  • Required: No
  • Description: How many tokens to generate for each chat completion.

options.presence_penalty

  • Type: number
  • Required: No
  • Description: How much to penalize new tokens based on whether they appear in the text so far.

options.frequency_penalty

  • Type: number
  • Required: No
  • Description: How much to penalize new tokens based on their existing frequency in the text so far.

options.logit_bias

  • Type: Object
  • Required: No -Description: Modify the likelihood of specified tokens appearing in the completion.

options.user

  • Type: string
  • Required: No
  • Description: A unique identifier representing your end-user.

Note: Default values have been provided where applicable.