chatgpt-official
Advanced tools
Comparing version 1.1.3 to 1.1.4
@@ -20,2 +20,3 @@ import Options from "../models/options.js"; | ||
private aksRevProxy; | ||
private isJSON; | ||
private generatePrompt; | ||
@@ -22,0 +23,0 @@ moderate(prompt: string): Promise<boolean>; |
@@ -28,2 +28,5 @@ import { encode } from "gpt-3-encoder"; | ||
this.openAi = new OpenAIApi(new Configuration({ apiKey: this.key })); | ||
if (!this.key.startsWith("sk-")) | ||
if (!this.accessToken || !this.validateToken(this.accessToken)) | ||
this.getTokens(); | ||
} | ||
@@ -230,3 +233,2 @@ async *chunksToLines(chunksAsync) { | ||
stop: [this.options.stop], | ||
stream: true, | ||
}, { | ||
@@ -246,3 +248,10 @@ responseType: "stream", | ||
await new Promise((resolve) => response.data.on("end", resolve)); | ||
return responseStr; | ||
responseStr = responseStr.trim(); | ||
if (this.isJSON(responseStr)) { | ||
let jsonData = JSON.parse(responseStr); | ||
let response = jsonData?.choices[0]?.text; | ||
return response ?? ""; | ||
} | ||
else | ||
return responseStr; | ||
} | ||
@@ -253,2 +262,11 @@ catch (error) { | ||
} | ||
isJSON(str) { | ||
try { | ||
JSON.parse(str); | ||
return true; | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
generatePrompt(conversation, prompt) { | ||
@@ -313,8 +331,11 @@ prompt = [",", "!", "?", "."].includes(prompt[prompt.length - 1]) ? prompt : `${prompt}.`; | ||
validateToken(token) { | ||
console.log("validating access token..."); | ||
if (!token) | ||
return false; | ||
const parsed = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString()); | ||
console.log(Date.now() <= parsed.exp * 1000 ? "valid" : "invalid"); | ||
return Date.now() <= parsed.exp * 1000; | ||
} | ||
async getTokens() { | ||
console.log("getting new access token..."); | ||
if (!this.key) { | ||
@@ -336,2 +357,4 @@ throw new Error("No session token provided"); | ||
this.accessToken = response.data.accessToken; | ||
console.log(this.key); | ||
console.log(this.accessToken); | ||
} | ||
@@ -338,0 +361,0 @@ catch (err) { |
{ | ||
"name": "chatgpt-official", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "ChatGPT Client using official OpenAI API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -32,2 +32,3 @@ import { encode } from "gpt-3-encoder"; | ||
this.openAi = new OpenAIApi(new Configuration({ apiKey: this.key })); | ||
if (!this.key.startsWith("sk-")) if (!this.accessToken || !this.validateToken(this.accessToken)) this.getTokens(); | ||
} | ||
@@ -250,3 +251,3 @@ | ||
stop: [this.options.stop], | ||
stream: true, | ||
// stream: true, | ||
}, | ||
@@ -271,4 +272,8 @@ { | ||
await new Promise((resolve) => response.data.on("end", resolve)); | ||
return responseStr; | ||
responseStr = responseStr.trim(); | ||
if (this.isJSON(responseStr)) { | ||
let jsonData = JSON.parse(responseStr); | ||
let response = jsonData?.choices[0]?.text; | ||
return response ?? ""; | ||
} else return responseStr; | ||
} catch (error: any) { | ||
@@ -279,2 +284,11 @@ throw new Error(error?.response?.data?.error?.message); | ||
private isJSON(str: string) { | ||
try { | ||
JSON.parse(str); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
private generatePrompt(conversation: Conversation, prompt: string) { | ||
@@ -281,0 +295,0 @@ prompt = [",", "!", "?", "."].includes(prompt[prompt.length - 1]) ? prompt : `${prompt}.`; // Thanks to https://github.com/optionsx |
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
80507
867