ts-chatgpt-api
Version:
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
119 lines (118 loc) • 3.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatGptApi = void 0;
const types_1 = require("./types");
const chatGptURL = 'https://api.openai.com/v1/chat/completions';
/**
* Main class for make requests
*/
class ChatGptApi {
constructor(apiKey, aiModel = 'gpt-3.5-turbo', role = types_1.ChatGptRoleList.USER) {
this.apiKey = apiKey;
this.aiModel = aiModel;
this.role = role;
// list of messages which will send by Chat GPT API
this.messages = [];
this.isDebug = false;
}
enableDebugging() {
this.isDebug = true;
}
disableDebugging() {
this.isDebug = false;
}
/**
* Set AI engine model for generating response
* @param aiModel
*/
setAiModel(aiModel) {
this.printLog('call setAiModel');
this.printLog('set AI model as', aiModel);
this.aiModel = aiModel;
}
printLog(...params) {
if (this.isDebug) {
console.log((new Date()).toISOString(), "\t", JSON.stringify(params, null, 4));
}
}
/**
* Set role for single
* @param role
*/
setRole(role) {
this.printLog('call setRole');
this.printLog('set role', role);
this.role = role;
}
/**
* Clean messages
* @private
*/
resetMessages() {
this.printLog('clean message array');
this.messages = [];
}
/**
* Build request data for sending to ChatGPT API
* @param completionParams
* @private
*/
buildRequest(completionParams) {
const requestBody = {
model: this.aiModel,
messages: this.messages,
...completionParams
};
this.printLog('prepare request body', requestBody);
const requestData = {
method: 'POST',
body: JSON.stringify(requestBody),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`
}
};
this.printLog('full request data', requestData);
return requestData;
}
/**
* Add new message
* @param role
* @param content
*/
addMessage(role, content) {
this.printLog('call addMessage');
const newMessage = {
role,
content
};
this.printLog('add new message', newMessage);
this.messages.push(newMessage);
}
/**
* Return answers for multiple requests
* @param completionParams
*/
async processMultipleMessages(completionParams) {
this.printLog('call processMultipleMessages');
const requestData = this.buildRequest(completionParams);
const response = await fetch(chatGptURL, requestData);
const jsonResponse = await response.json();
this.resetMessages();
return jsonResponse;
}
/**
* Return answer for single request
* @param content
* @param completionParams
*/
async getAnswer(content, completionParams) {
this.resetMessages();
this.addMessage(this.role, content);
const requestData = this.buildRequest(completionParams);
const response = await fetch(chatGptURL, requestData);
const jsonResponse = await response.json();
return jsonResponse;
}
}
exports.ChatGptApi = ChatGptApi;