chatgpt-official
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -7,2 +7,3 @@ import options from "../models/options.js"; | ||
options: options; | ||
instructionTokens: number; | ||
constructor(key: string, options?: options); | ||
@@ -15,5 +16,6 @@ addConversation(conversationId: string): { | ||
resetConversation(conversationId: string): conversation; | ||
ask(prompt: any, conversationId?: string): Promise<any>; | ||
ask(prompt: string, conversationId?: string): Promise<any>; | ||
private generatePrompt; | ||
private getToday; | ||
} | ||
export default ChatGPT; |
import axios from "axios"; | ||
import { encode } from "gpt-3-encoder"; | ||
class ChatGPT { | ||
@@ -6,2 +7,3 @@ key; | ||
options; | ||
instructionTokens; | ||
constructor(key, options) { | ||
@@ -11,3 +13,3 @@ this.key = key; | ||
this.options = { | ||
historySize: options?.historySize || 50, | ||
model: options?.model || "text-chat-davinci-002-20230126", | ||
temperature: options?.temperature || 0.7, | ||
@@ -21,3 +23,5 @@ max_tokens: options?.max_tokens || 256, | ||
Knowledge cutoff: 2021-09`, | ||
stop: options?.stop || "<|im_end|>", | ||
}; | ||
this.instructionTokens = encode(this.options.instructions).length; | ||
} | ||
@@ -30,3 +34,3 @@ addConversation(conversationId) { | ||
conversation.messages.push(`${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`); | ||
Current date: ${this.getToday()}${this.options.stop}`); | ||
this.conversations.push(conversation); | ||
@@ -50,3 +54,3 @@ return conversation; | ||
conversation.messages.push(`${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`); | ||
Current date: ${this.getToday()}${this.options.stop}`); | ||
conversation.lastActive = Date.now(); | ||
@@ -58,12 +62,5 @@ } | ||
let conversation = this.getConversation(conversationId); | ||
prompt = prompt[prompt.length - 1].includes([",", "!", "?", "."]) ? prompt : `${prompt}.`; | ||
conversation.messages.push(`${prompt}\n\n`); | ||
conversation.messages = conversation.messages.slice(-this.options.historySize); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`; | ||
conversation.lastActive = Date.now(); | ||
let promptStr = conversation.messages.join("\n"); | ||
let promptStr = this.generatePrompt(conversation, prompt); | ||
const response = await axios.post("https://api.openai.com/v1/completions", { | ||
model: "text-chat-davinci-002-20230126", | ||
model: this.options.model, | ||
prompt: promptStr, | ||
@@ -75,3 +72,3 @@ temperature: this.options.temperature, | ||
presence_penalty: this.options.presence_penalty, | ||
stop: ["<|im_end|>"], | ||
stop: [this.options.stop], | ||
}, { | ||
@@ -83,6 +80,30 @@ headers: { | ||
}); | ||
let responseStr = response.data.choices[0].text.replace(/<\|im_end\|>/g, "").trim(); | ||
conversation.messages.push(`${responseStr}<|im_end|>`); | ||
let responseStr = response.data.choices[0].text | ||
.replace(/<\|im_end\|>/g, "") | ||
.replace(this.options.stop, "") | ||
.trim(); | ||
conversation.messages.push(`${responseStr}${this.options.stop}`); | ||
return responseStr; | ||
} | ||
generatePrompt(conversation, prompt) { | ||
prompt = [",", "!", "?", "."].includes(prompt[prompt.length - 1]) ? prompt : `${prompt}.`; | ||
conversation.messages.push(`${prompt}\n\n`); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}${this.options.stop}`; | ||
let promptStr = conversation.messages.join("\n"); | ||
let promptEncodedLength = encode(promptStr).length; | ||
let totalLength = promptEncodedLength + this.options.max_tokens; | ||
while (totalLength > 4096) { | ||
conversation.messages.shift(); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}${this.options.stop}`; | ||
promptStr = conversation.messages.join("\n"); | ||
promptEncodedLength = encode(promptStr).length; | ||
totalLength = promptEncodedLength + this.options.max_tokens; | ||
} | ||
conversation.lastActive = Date.now(); | ||
return promptStr; | ||
} | ||
getToday() { | ||
@@ -89,0 +110,0 @@ let today = new Date(); |
interface options { | ||
historySize?: number; | ||
model?: string; | ||
temperature?: number; | ||
@@ -9,3 +9,4 @@ max_tokens?: number; | ||
instructions?: string; | ||
stop?: string; | ||
} | ||
export default options; |
{ | ||
"name": "chatgpt-official", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "ChatGPT Client using official OpenAI API", | ||
@@ -55,3 +55,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"axios": "1.2.2" | ||
"axios": "1.2.2", | ||
"gpt-3-encoder": "1.1.4" | ||
}, | ||
@@ -58,0 +59,0 @@ "bugs": { |
@@ -29,7 +29,33 @@ # chatgpt-official - Unofficial API client for ChatGPT that uses OpenAI official API [[Discord](https://discord.pawan.krd)] | ||
``` | ||
## Usage | ||
## Note: This is unofficial means may be stop working anytime | ||
```javascript | ||
import ChatGPT from "chatgpt-official"; | ||
# | ||
let options = { | ||
temperature: 0.7, // OpenAI parameter | ||
max_tokens: 256, // OpenAI parameter [Max response size by tokens] | ||
top_p: 1, // OpenAI parameter | ||
frequency_penalty: 0, // OpenAI parameter | ||
presence_penalty: 0, // OpenAI parameter | ||
historySize: 50, // Max messages to remember per conversation | ||
instructions: `You are ChatGPT, a large language model trained by OpenAI.`, // initial instructions for the bot | ||
model: "text-chat-davinci-002-20230126", // OpenAI parameter | ||
stop: "<|im_end|>", // OpenAI parameter | ||
} | ||
### Thanks to [waylaidwanderer](https://github.com/waylaidwanderer) for finding this method | ||
let bot = new ChatGPT("<OPENAI_API_KEY>", options); // Note: options is optional | ||
let response = await bot.ask("Hello?"); | ||
console.log(response); | ||
let conversationId = "conversation name"; | ||
let response1 = await bot.ask("Hello?", conversationId); | ||
console.log(response1); | ||
let conversationId2 = "another conversation name"; | ||
let response2 = await bot.ask("Hello?", conversationId2); | ||
console.log(response2); | ||
``` | ||
##### Thanks to [waylaidwanderer](https://github.com/waylaidwanderer) for finding this method |
import axios from "axios"; | ||
import { encode } from "gpt-3-encoder"; | ||
import options from "../models/options.js"; | ||
@@ -9,3 +11,3 @@ import conversation from "../models/conversation.js"; | ||
public options: options; | ||
public instructionTokens: number; | ||
constructor(key: string, options?: options) { | ||
@@ -15,3 +17,3 @@ this.key = key; | ||
this.options = { | ||
historySize: options?.historySize || 50, | ||
model: options?.model || "text-chat-davinci-002-20230126", | ||
temperature: options?.temperature || 0.7, | ||
@@ -26,3 +28,5 @@ max_tokens: options?.max_tokens || 256, | ||
Knowledge cutoff: 2021-09`, | ||
stop: options?.stop || "<|im_end|>", | ||
}; | ||
this.instructionTokens = encode(this.options.instructions).length; | ||
} | ||
@@ -36,3 +40,3 @@ | ||
conversation.messages.push(`${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`); | ||
Current date: ${this.getToday()}${this.options.stop}`); | ||
this.conversations.push(conversation); | ||
@@ -59,3 +63,3 @@ | ||
conversation.messages.push(`${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`); | ||
Current date: ${this.getToday()}${this.options.stop}`); | ||
conversation.lastActive = Date.now(); | ||
@@ -67,16 +71,10 @@ } | ||
public async ask(prompt: any, conversationId: string = "default") { | ||
public async ask(prompt: string, 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); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}<|im_end|>`; | ||
conversation.lastActive = Date.now(); | ||
let promptStr = conversation.messages.join("\n"); | ||
let promptStr = this.generatePrompt(conversation, prompt); | ||
const response = await axios.post( | ||
"https://api.openai.com/v1/completions", | ||
{ | ||
model: "text-chat-davinci-002-20230126", | ||
model: this.options.model, | ||
prompt: promptStr, | ||
@@ -88,3 +86,3 @@ temperature: this.options.temperature, | ||
presence_penalty: this.options.presence_penalty, | ||
stop: ["<|im_end|>"], | ||
stop: [this.options.stop], | ||
}, | ||
@@ -99,7 +97,36 @@ { | ||
let responseStr = response.data.choices[0].text.replace(/<\|im_end\|>/g, "").trim(); | ||
conversation.messages.push(`${responseStr}<|im_end|>`); | ||
let responseStr = response.data.choices[0].text | ||
.replace(/<\|im_end\|>/g, "") | ||
.replace(this.options.stop, "") | ||
.trim(); | ||
conversation.messages.push(`${responseStr}${this.options.stop}`); | ||
return responseStr; | ||
} | ||
private generatePrompt(conversation: conversation, prompt: string) { | ||
prompt = [",", "!", "?", "."].includes(prompt[prompt.length - 1]) ? prompt : `${prompt}.`; // Thanks to https://github.com/optionsx | ||
conversation.messages.push(`${prompt}\n\n`); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}${this.options.stop}`; | ||
let promptStr = conversation.messages.join("\n"); | ||
let promptEncodedLength = encode(promptStr).length; | ||
let totalLength = promptEncodedLength + this.options.max_tokens; | ||
while (totalLength > 4096) { | ||
conversation.messages.shift(); | ||
if (!conversation.messages[0].includes("Current date:")) | ||
conversation.messages[0] = `${this.options.instructions} | ||
Current date: ${this.getToday()}${this.options.stop}`; | ||
promptStr = conversation.messages.join("\n"); | ||
promptEncodedLength = encode(promptStr).length; | ||
totalLength = promptEncodedLength + this.options.max_tokens; | ||
} | ||
conversation.lastActive = Date.now(); | ||
return promptStr; | ||
} | ||
private getToday() { | ||
@@ -106,0 +133,0 @@ let today = new Date(); |
interface options { | ||
historySize?: number; | ||
model?: string; | ||
temperature?: number; | ||
@@ -9,4 +9,5 @@ max_tokens?: number; | ||
instructions?: string; | ||
stop?: string; | ||
} | ||
export default options; |
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
49380
332
61
2
+ Addedgpt-3-encoder@1.1.4
+ Addedgpt-3-encoder@1.1.4(transitive)