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

ts-chatgpt-api

v1.1.3

Published

ChatGPT API is a powerful tool developed on TypeScript that allows developers to easily integrate natural language processing capabilities into their applications. As a large language model trained by OpenAI, ChatGPT is capable of understanding and gener

Downloads

261

Readme

Chat GPT TypeScript API

ChatGPT API is a powerful tool developed on TypeScript that allows developers to easily integrate natural language processing capabilities into their applications. As a large language model trained by OpenAI, ChatGPT is capable of understanding and generating human-like responses to a wide variety of queries.

With the ChatGPT API, developers can easily create conversational interfaces that can understand and respond to natural language queries in a way that feels intuitive and natural to users. This makes it an ideal tool for building chatbots, virtual assistants, and other applications that rely on natural language processing.

Because ChatGPT API is built on TypeScript, developers can take advantage of its strong typing and other features that make it easier to write reliable and maintainable code.

Install

Run next command

npm i ts-chatgpt-api

How to get my ChatGPT API key

  • Create account and login by this link
  • Go to Manage Account -> API Keys or go to this link
  • Create new secret key by clicking + Create new secret key

API

enableDebugging(): void

Print debug information.

disableDebugging(): void

Stop printing debug information.

setAiModel(aiModel: string): void

Set AI model, right now the latest AI model is gpt-3.5-turbo. It used by default.

setRole(role: ChatGptRoles): void

Set role for message. Possible roles are "system", "user", or "assistant". There is pre-defined enum with values. It calls ChatGptRoleList

addMessage(role: ChatGptRoles, content: string): void

Add message which will be sent to ChatGPT.

getAnswer(content: string): Promise

Get answer for single request. Response example is:

{
 "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
 "object": "chat.completion",
 "created": 1677649420,
 "model": "gpt-3.5-turbo",
 "usage": {"prompt_tokens": 56, "completion_tokens": 31, "total_tokens": 87},
 "choices": [
   {
    "message": {
      "role": "assistant",
      "content": "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, which was the new home stadium for the Texas Rangers."},
    "finish_reason": "stop",
    "index": 0
   }
  ]
}

processMultipleMessages(): Promise

Get answer for multiple request.

Usage

Single request

const chatGptApi = new ChatGptApi('MY_GPT_API_KEY');
const response = await chatGptApi.getAnswer('tell me about yourself');

Multiple requests

const chatGptApi = new ChatGptApi('MY_GPT_API_KEY');
chatGptApi.addMessage(ChatGptRoleList.SYSTEM, 'tell me about yourself');
chatGptApi.addMessage(ChatGptRoleList.USER, 'Translate from English to French');
const response = await chatGptApi.processMultipleMessages();

Override completion params

You can override the default model (gpt-3.5-turbo-0301) and any OpenAI chat completion params using completionParam

Single request with completion parameters

const chatGptApi = new ChatGptApi('MY_GPT_API_KEY');
const response = await chatGptApi.getAnswer('tell me about yourself', {top_n: 0.4});

Multiple requests with completion parameters

const chatGptApi = new ChatGptApi('MY_GPT_API_KEY');
chatGptApi.addMessage(ChatGptRoleList.SYSTEM, 'tell me about yourself');
chatGptApi.addMessage(ChatGptRoleList.USER, 'Translate from English to French');
const response = await chatGptApi.processMultipleMessages({
    stream: true
});