chatgpt-official
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -5,2 +5,3 @@ import Options from "../models/options.js"; | ||
key: string; | ||
accessToken: string; | ||
conversations: Conversation[]; | ||
@@ -26,3 +27,5 @@ options: Options; | ||
private wait; | ||
private validateToken; | ||
getTokens(): Promise<void>; | ||
} | ||
export default ChatGPT; |
@@ -7,2 +7,3 @@ import { encode } from "gpt-3-encoder"; | ||
key; | ||
accessToken; | ||
conversations; | ||
@@ -15,3 +16,3 @@ options; | ||
this.options = { | ||
model: options?.model || "text-chat-davinci-002-20221122", | ||
model: options?.model || "text-davinci-003", | ||
temperature: options?.temperature || 0.7, | ||
@@ -91,5 +92,8 @@ max_tokens: options?.max_tokens || 512, | ||
async ask(prompt, conversationId = "default", userName = "User") { | ||
if (!this.key.startsWith("sk-")) | ||
if (!this.accessToken || !this.validateToken(this.accessToken)) | ||
await this.getTokens(); | ||
let conversation = this.getConversation(conversationId, userName); | ||
let promptStr = this.generatePrompt(conversation, prompt); | ||
if (this.options.moderation) { | ||
if (this.options.moderation && this.key.startsWith("sk-")) { | ||
let flagged = await this.moderate(promptStr); | ||
@@ -137,4 +141,7 @@ if (flagged) { | ||
async askStream(data, prompt, conversationId = "default", userName = "User") { | ||
if (!this.key.startsWith("sk-")) | ||
if (!this.accessToken || !this.validateToken(this.accessToken)) | ||
await this.getTokens(); | ||
let conversation = this.getConversation(conversationId, userName); | ||
if (this.options.moderation) { | ||
if (this.options.moderation && this.key.startsWith("sk-")) { | ||
let flagged = await this.moderate(prompt); | ||
@@ -213,2 +220,5 @@ if (flagged) { | ||
async aksRevProxy(prompt, data = (_) => { }) { | ||
if (!this.key.startsWith("sk-")) | ||
if (!this.accessToken || !this.validateToken(this.accessToken)) | ||
await this.getTokens(); | ||
try { | ||
@@ -224,2 +234,3 @@ const response = await axios.post(this.options.revProxy, { | ||
stop: [this.options.stop], | ||
stream: true, | ||
}, { | ||
@@ -230,3 +241,3 @@ responseType: "stream", | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.key}`, | ||
Authorization: `Bearer ${this.key.startsWith("sk-") ? this.key : this.accessToken}`, | ||
}, | ||
@@ -275,3 +286,3 @@ }); | ||
let message = conversation.messages[i]; | ||
if (i === conversation.messages.length - 1) { | ||
if (i === 0) { | ||
messages.push(this.getInstructions(conversation.userName)); | ||
@@ -305,4 +316,32 @@ } | ||
} | ||
validateToken(token) { | ||
if (!token) | ||
return false; | ||
const parsed = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString()); | ||
return Date.now() <= parsed.exp * 1000; | ||
} | ||
async getTokens() { | ||
if (!this.key) { | ||
throw new Error("No session token provided"); | ||
} | ||
const response = await axios.request({ | ||
method: "GET", | ||
url: Buffer.from("aHR0cHM6Ly9leHBsb3Jlci5hcGkub3BlbmFpLmNvbS9hcGkvYXV0aC9zZXNzaW9u", "base64").toString("ascii"), | ||
headers: { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0", | ||
Cookie: `__Secure-next-auth.session-token=${this.key}`, | ||
}, | ||
}); | ||
try { | ||
const cookies = response.headers["set-cookie"]; | ||
const sessionCookie = cookies.find((cookie) => cookie.startsWith("__Secure-next-auth.session-token")); | ||
this.key = sessionCookie.split("=")[1]; | ||
this.accessToken = response.data.accessToken; | ||
} | ||
catch (err) { | ||
throw new Error(`Failed to fetch new session tokens due to: ${err}`); | ||
} | ||
} | ||
} | ||
export default ChatGPT; | ||
//# sourceMappingURL=chatgpt.js.map |
{ | ||
"name": "chatgpt-official", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "ChatGPT Client using official OpenAI API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,2 @@ | ||
# chatgpt-official - Unofficial API client for ChatGPT that uses OpenAI official API [[Discord](https://discord.pawan.krd)] | ||
# chatgpt-official - a simple library to create ChatGPT that uses OpenAI official API [[Discord](https://discord.pawan.krd)] | ||
@@ -11,3 +11,3 @@ [![NPM](https://img.shields.io/npm/v/chatgpt-official.svg?label=NPM&logo=npm&color=CB3837)](https://www.npmjs.com/package/chatgpt-official) | ||
A simple Node.js module for interacting with the ChatGPT without using any **~~Browser~~** using OpenAI official API. | ||
A simple Node.js module for creating ChatGPT using OpenAI official API. | ||
@@ -42,3 +42,3 @@ ## Installation | ||
instructions: `You are ChatGPT, a large language model trained by OpenAI.`, // initial instructions for the bot | ||
model: "text-chat-davinci-002-20230126", // OpenAI parameter | ||
model: "text-davinci-003", // OpenAI parameter `text-davinci-003` is PAID | ||
stop: "<|im_end|>", // OpenAI parameter | ||
@@ -60,3 +60,1 @@ } | ||
``` | ||
##### Thanks to [waylaidwanderer](https://github.com/waylaidwanderer) for finding this method |
@@ -11,2 +11,3 @@ import { encode } from "gpt-3-encoder"; | ||
public key: string; | ||
public accessToken: string; | ||
public conversations: Conversation[]; | ||
@@ -19,3 +20,3 @@ public options: Options; | ||
this.options = { | ||
model: options?.model || "text-chat-davinci-002-20221122", // default model updated to an older model (2022-11-22) found by @canfam - Discord:pig#8932 // you can use the newest model (2023-01-26) using my private API https://gist.github.com/PawanOsman/be803be44caed2449927860956b240ad | ||
model: options?.model || "text-davinci-003", // default model | ||
temperature: options?.temperature || 0.7, | ||
@@ -106,6 +107,7 @@ max_tokens: options?.max_tokens || 512, | ||
public async ask(prompt: string, conversationId: string = "default", userName: string = "User") { | ||
if (!this.key.startsWith("sk-")) if (!this.accessToken || !this.validateToken(this.accessToken)) await this.getTokens(); | ||
let conversation = this.getConversation(conversationId, userName); | ||
let promptStr = this.generatePrompt(conversation, prompt); | ||
if (this.options.moderation) { | ||
if (this.options.moderation && this.key.startsWith("sk-")) { | ||
let flagged = await this.moderate(promptStr); | ||
@@ -156,5 +158,6 @@ if (flagged) { | ||
public async askStream(data: (arg0: string) => void, prompt: string, conversationId: string = "default", userName: string = "User") { | ||
if (!this.key.startsWith("sk-")) if (!this.accessToken || !this.validateToken(this.accessToken)) await this.getTokens(); | ||
let conversation = this.getConversation(conversationId, userName); | ||
if (this.options.moderation) { | ||
if (this.options.moderation && this.key.startsWith("sk-")) { | ||
let flagged = await this.moderate(prompt); | ||
@@ -237,2 +240,3 @@ if (flagged) { | ||
private async aksRevProxy(prompt: string, data: (arg0: string) => void = (_) => {}) { | ||
if (!this.key.startsWith("sk-")) if (!this.accessToken || !this.validateToken(this.accessToken)) await this.getTokens(); | ||
try { | ||
@@ -250,2 +254,3 @@ const response = await axios.post( | ||
stop: [this.options.stop], | ||
stream: true, | ||
}, | ||
@@ -257,3 +262,3 @@ { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.key}`, | ||
Authorization: `Bearer ${this.key.startsWith("sk-") ? this.key : this.accessToken}`, | ||
}, | ||
@@ -313,3 +318,3 @@ }, | ||
let message = conversation.messages[i]; | ||
if (i === conversation.messages.length - 1) { | ||
if (i === 0) { | ||
messages.push(this.getInstructions(conversation.userName)); | ||
@@ -346,4 +351,36 @@ } | ||
} | ||
private validateToken(token: string) { | ||
if (!token) return false; | ||
const parsed = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString()); | ||
return Date.now() <= parsed.exp * 1000; | ||
} | ||
async getTokens() { | ||
if (!this.key) { | ||
throw new Error("No session token provided"); | ||
} | ||
const response = await axios.request({ | ||
method: "GET", | ||
url: Buffer.from("aHR0cHM6Ly9leHBsb3Jlci5hcGkub3BlbmFpLmNvbS9hcGkvYXV0aC9zZXNzaW9u", "base64").toString("ascii"), | ||
headers: { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0", | ||
Cookie: `__Secure-next-auth.session-token=${this.key}`, | ||
}, | ||
}); | ||
try { | ||
const cookies = response.headers["set-cookie"]; | ||
const sessionCookie = cookies.find((cookie) => cookie.startsWith("__Secure-next-auth.session-token")); | ||
this.key = sessionCookie.split("=")[1]; | ||
this.accessToken = response.data.accessToken; | ||
} catch (err) { | ||
throw new Error(`Failed to fetch new session tokens due to: ${err}`); | ||
} | ||
} | ||
} | ||
export default ChatGPT; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78378
829
58