New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-chatgpt-api

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-chatgpt-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 gener

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 18 Jun 2023

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