Skip to content

Commit

Permalink
Fix chatgpt (#772)
Browse files Browse the repository at this point in the history
* Fix chatgpt

* Fix chatgpt
  • Loading branch information
asyncButNeverAwaits authored Apr 14, 2024
1 parent 275039b commit cdfc4d0
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/bots/openai/ChatGPTBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,40 @@ export default class ChatGPTBot extends Bot {
// Make sure the access token is available
if (!this.accessToken) await this.checkAvailability();

// Send the prompt to the ChatGPT API
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.accessToken}`,
};

let requirement;
try {
const result = await axios.post(
"https://chat.openai.com/backend-api/sentinel/chat-requirements",
undefined,
{ headers },
);
if (result) {
requirement = result.data;
}
} catch (error) {
console.error("Error get chat-requirements token:", error);
console.error("ChatGPT response:", event);
}

if (requirement.token) {
headers["Openai-Sentinel-Chat-Requirements-Token"] = requirement.token;
}

// Send the prompt to the ChatGPT API
const context = await this.getChatContext();
const payload = JSON.stringify({
action: "next",
arkose_token: await this.getArkoseToken(),
conversation_mode: {
kind: "primary_assistant",
},
arkose_token: requirement?.arkose?.required
? await this.getArkoseToken()
: undefined,
messages: [
{
id: uuidv4(),
Expand All @@ -178,6 +203,7 @@ export default class ChatGPTBot extends Bot {
content_type: "text",
parts: [prompt],
},
metadata: {},
},
],
conversation_id: context.conversationId,
Expand All @@ -190,7 +216,13 @@ export default class ChatGPTBot extends Bot {
try {
const source = new SSE(
"https://chat.openai.com/backend-api/conversation",
{ headers, payload },
{
headers: {
...headers,
accept: "text/event-stream",
},
payload,
},
);

let preInfo = [];
Expand Down Expand Up @@ -253,7 +285,7 @@ export default class ChatGPTBot extends Bot {
} catch (error) {
console.error("Error parsing ChatGPT response:", error);
console.error("ChatGPT response:", event);
return;
reject(error);
}
});

Expand All @@ -280,6 +312,16 @@ export default class ChatGPTBot extends Bot {
reject(new Error(message));
});

source.addEventListener("readystatechange", (event) => {
if (event.readyState === source.CLOSED) {
// after stream closed, done
onUpdateResponse(callbackParam, {
done: true,
});
resolve();
}
});

source.stream();
} catch (error) {
reject(error);
Expand Down

0 comments on commit cdfc4d0

Please sign in to comment.