chatgpt-official
Advanced tools
Comparing version 1.0.2 to 1.0.3
import options from "../models/options.js"; | ||
import conversation from "../models/conversation.js"; | ||
declare class ChatGPT { | ||
key: string; | ||
messages: string[]; | ||
conversations: conversation[]; | ||
options: options; | ||
constructor(key: string, options?: options); | ||
addMessage(message: string): void; | ||
ask(prompt: any): Promise<any>; | ||
addConversation(conversationId: string): { | ||
id: string; | ||
messages: any[]; | ||
}; | ||
getConversation(conversationId: string): conversation; | ||
resetConversation(conversationId: string): conversation; | ||
ask(prompt: any, conversationId?: string): Promise<any>; | ||
private getToday; | ||
} | ||
export default ChatGPT; |
import axios from "axios"; | ||
class ChatGPT { | ||
key; | ||
messages; | ||
conversations; | ||
options; | ||
@@ -16,14 +16,42 @@ constructor(key, options) { | ||
}; | ||
this.messages = []; | ||
this.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
} | ||
addConversation(conversationId) { | ||
let conversation = { | ||
id: conversationId, | ||
messages: [], | ||
}; | ||
conversation.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
Knowledge cutoff: 2021-09 | ||
Current date: ${this.getToday()}\n\n`); | ||
this.conversations.push(conversation); | ||
return conversation; | ||
} | ||
addMessage(message) { | ||
this.messages.push(message); | ||
getConversation(conversationId) { | ||
let conversation = this.conversations.find((conversation) => conversation.id === conversationId); | ||
if (!conversation) { | ||
conversation = this.addConversation(conversationId); | ||
} | ||
else { | ||
conversation.lastActive = Date.now(); | ||
} | ||
return conversation; | ||
} | ||
async ask(prompt) { | ||
resetConversation(conversationId) { | ||
let conversation = this.conversations.find((conversation) => conversation.id === conversationId); | ||
if (conversation) { | ||
conversation.messages = []; | ||
conversation.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
Knowledge cutoff: 2021-09 | ||
Current date: ${this.getToday()}\n\n`); | ||
conversation.lastActive = Date.now(); | ||
} | ||
return conversation; | ||
} | ||
async ask(prompt, conversationId = "default") { | ||
let conversation = this.getConversation(conversationId); | ||
prompt = prompt[prompt.length - 1].includes([",", "!", "?", "."]) ? prompt : `${prompt}.`; | ||
this.messages.push(`${prompt}\n\n`); | ||
let promptStr = this.messages.join("\n"); | ||
conversation.messages.push(`${prompt}\n\n`); | ||
conversation.messages = conversation.messages.slice(-this.options.historySize); | ||
conversation.lastActive = Date.now(); | ||
let promptStr = conversation.messages.join("\n"); | ||
const response = await axios.post("https://api.openai.com/v1/completions", { | ||
@@ -48,3 +76,3 @@ model: "text-chat-davinci-002-20230126", | ||
.trim(); | ||
this.messages.push(`${responseStr}\n\n`); | ||
conversation.messages.push(`${responseStr}\n\n`); | ||
return responseStr; | ||
@@ -51,0 +79,0 @@ } |
{ | ||
"name": "chatgpt-official", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "ChatGPT Client using official OpenAI API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import axios from "axios"; | ||
import options from "../models/options.js"; | ||
import conversation from "../models/conversation.js"; | ||
class ChatGPT { | ||
public key: string; | ||
public messages: string[]; | ||
public conversations: conversation[]; | ||
public options: options; | ||
constructor(key: string, options?: options) { | ||
@@ -18,14 +20,48 @@ this.key = key; | ||
}; | ||
this.messages = []; | ||
this.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
} | ||
public addConversation(conversationId: string) { | ||
let conversation = { | ||
id: conversationId, | ||
messages: [], | ||
}; | ||
conversation.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
Knowledge cutoff: 2021-09 | ||
Current date: ${this.getToday()}\n\n`); | ||
this.conversations.push(conversation); | ||
return conversation; | ||
} | ||
public addMessage(message: string) { | ||
this.messages.push(message); | ||
public getConversation(conversationId: string) { | ||
let conversation = this.conversations.find((conversation) => conversation.id === conversationId); | ||
if (!conversation) { | ||
conversation = this.addConversation(conversationId); | ||
} else { | ||
conversation.lastActive = Date.now(); | ||
} | ||
return conversation; | ||
} | ||
public async ask(prompt: any) { | ||
prompt = prompt[prompt.length - 1].includes([",", "!", "?", "."]) ? prompt : `${prompt}.`; | ||
this.messages.push(`${prompt}\n\n`); | ||
let promptStr = this.messages.join("\n"); | ||
public resetConversation(conversationId: string) { | ||
let conversation = this.conversations.find((conversation) => conversation.id === conversationId); | ||
if (conversation) { | ||
conversation.messages = []; | ||
conversation.messages.push(`You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. | ||
Knowledge cutoff: 2021-09 | ||
Current date: ${this.getToday()}\n\n`); | ||
conversation.lastActive = Date.now(); | ||
} | ||
return conversation; | ||
} | ||
public async ask(prompt: any, conversationId: string = "default") { | ||
let conversation = this.getConversation(conversationId); | ||
prompt = prompt[prompt.length - 1].includes([",", "!", "?", "."]) ? prompt : `${prompt}.`; // Thanks to https://github.com/optionsx | ||
conversation.messages.push(`${prompt}\n\n`); | ||
conversation.messages = conversation.messages.slice(-this.options.historySize); | ||
conversation.lastActive = Date.now(); | ||
let promptStr = conversation.messages.join("\n"); | ||
const response = await axios.post( | ||
@@ -55,3 +91,3 @@ "https://api.openai.com/v1/completions", | ||
.trim(); | ||
this.messages.push(`${responseStr}\n\n`); | ||
conversation.messages.push(`${responseStr}\n\n`); | ||
return responseStr; | ||
@@ -58,0 +94,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45084
22
281