JS client for ChatGPT API
npm install chatgpt-api-clientnpm install chatgpt-api-client yarn add chatgpt-api-clientEnsure you're using node >= 14 to prevent compatibility issues with obsolete node versions
javascript
import { ChatGPTApi } from "chatgpt-api-client";async function try() {
const api = new ChatGPTApi({ apiKey: process.env.OPENAI_API_KEY });
const response = await api.sendMessage({ prompt: "Hi! How is the weather today?" });
console.info(response);
}
`
For users who belong to multiple organizations, you can pass a this property to specify which organization is used for an API request.`javascript
const api = new ChatGPTApi({ apiKey: process.env.OPENAI_API_KEY, organization: "custom-org" });
`You can specify which AI model will be used in the processing of your requests.
`javascript
const response = await api.sendMessage({ model: "model-id-0", prompt: "Hi! How is the weather today?" });
`
text-davinci-003 is the default model that is specified in case of non-mentioned model property.
There are a bit of parameters to pass in the sendMessage function namely the max of tokens, temperature and number of completions.
`javascript
const response = await api.sendMessage({
prompt: "Hi! How is the weather today?", //The prompt(s) to generate completions for
model: "text-davinci-003", //ID of the model to use
max_tokens: 500, //The maximum number of tokens to generate in the completion
temperature: 0, //What sampling temperature to use, between 0 and 2, Default is 1
nCompeletions: 3 //How many completions to generate for each prompt. Default is 1
});
`
You can use the List of models API (api.getModels) to see all of your available models.`javascript
const api = new ChatGPTApi({ apiKey: process.env.OPENAI_API_KEY });
//Retrieve the list of available OpenAI models
let models = await api.getModels();
`
Usage with Promise return
The previous examples were made with Async/Await calls through asynchronos promise. We could explicitly handle the return of Promise Object.`javascript
api.sendMessage({ prompt: "Hello! this is a test" }).then((response) => {
//Proceed with success
console.info("Response data", response);
}, error => {
//An error has occured
console.error("Error", error);
});
`For Common.JS usage
`javascript
const { ChatGPTApi } = require("chatgpt-api-client");
``