Skip to content

Commit 1bfc8b3

Browse files
Lex Limhyperzlibwaylaidwanderer
authored
feat: add proxy option to ChatGPT clients and 'Generate title' request to browser client (#146)
* Add proxy option and 'Generate title' request * Remove unnecessary try-catch blocks * Remove unnecessary try-catch blocks in genTitle. * fix(client): merge import * fix: add support for reverseProxyUrl --------- Co-authored-by: Lex Lim <hyperzlib@outlook.com> Co-authored-by: Joel <jcz@hey.com>
1 parent 779bee5 commit 1bfc8b3

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/ChatGPTBrowserClient.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './fetch-polyfill.js';
22
import crypto from 'crypto';
33
import Keyv from 'keyv';
44
import { fetchEventSource } from '@waylaidwanderer/fetch-event-source';
5+
import { ProxyAgent } from 'undici';
56

67
export default class ChatGPTBrowserClient {
78
constructor(
@@ -38,6 +39,7 @@ export default class ChatGPTBrowserClient {
3839
Authorization: `Bearer ${this.accessToken}`,
3940
Cookie: this.cookies || undefined,
4041
},
42+
4143
body: JSON.stringify({
4244
conversation_id: conversationId,
4345
action,
@@ -55,14 +57,20 @@ export default class ChatGPTBrowserClient {
5557
model: this.model,
5658
}),
5759
};
60+
61+
if (this.options.proxy) {
62+
opts.dispatcher = new ProxyAgent(this.options.proxy);
63+
}
64+
5865
if (debug) {
5966
console.debug();
6067
console.debug(url);
6168
console.debug(opts);
6269
console.debug();
6370
}
71+
6472
// data: {"message": {"id": "UUID", "role": "assistant", "user": null, "create_time": null, "update_time": null, "content": {"content_type": "text", "parts": ["That's alright! If you don't have a specific question or topic in mind, I can suggest some general conversation starters or topics to explore. \n\nFor example, we could talk about your interests, hobbies, or goals. Alternatively, we could discuss current events, pop culture, or science and technology. Is there anything in particular that you're curious about or would like to learn more about?"]}, "end_turn": true, "weight": 1.0, "metadata": {"message_type": "next", "model_slug": "text-davinci-002-render-sha", "finish_details": {"type": "stop", "stop": "<|im_end|>"}}, "recipient": "all"}, "conversation_id": "UUID", "error": null}
65-
return new Promise(async (resolve, reject) => {
73+
const response = await new Promise(async (resolve, reject) => {
6674
let lastEvent = null;
6775
try {
6876
let done = false;
@@ -144,6 +152,12 @@ export default class ChatGPTBrowserClient {
144152
reject(err);
145153
}
146154
});
155+
156+
if (!conversationId) {
157+
this.genTitle(response);
158+
}
159+
160+
return response;
147161
}
148162

149163
async sendMessage(
@@ -209,4 +223,48 @@ export default class ChatGPTBrowserClient {
209223
details: result,
210224
};
211225
}
226+
227+
genTitle(event) {
228+
const debug = this.options.debug;
229+
if (debug) {
230+
console.log('Generate title: ', event);
231+
}
232+
if (!event || !event.conversation_id || !event.message || !event.message.id) {
233+
return;
234+
}
235+
236+
const conversationId = event.conversation_id;
237+
const messageId = event.message.id;
238+
239+
const baseUrl = this.options.reverseProxyUrl || 'https://chat.openai.com/backend-api/conversation';
240+
const url = `${baseUrl}/gen_title/${conversationId}`;
241+
const opts = {
242+
method: 'POST',
243+
headers: {
244+
'Content-Type': 'application/json',
245+
Authorization: `Bearer ${this.accessToken}`,
246+
Cookie: this.cookies || undefined,
247+
},
248+
body: JSON.stringify({
249+
message_id: messageId,
250+
model: this.model,
251+
}),
252+
};
253+
254+
if (debug) {
255+
console.log('gen title fetch opts: ', opts);
256+
}
257+
258+
if (this.options.proxy) {
259+
opts.dispatcher = new ProxyAgent(this.options.proxy);
260+
}
261+
262+
fetch(url, opts).then((ret) => {
263+
if (debug) {
264+
ret.json().then((data) => {
265+
console.log('Gen title response: ', data);
266+
}).catch(console.error);
267+
}
268+
}).catch(console.error);
269+
}
212270
}

src/ChatGPTClient.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import crypto from 'crypto';
33
import Keyv from 'keyv';
44
import { encoding_for_model, get_encoding } from '@dqbd/tiktoken';
55
import { fetchEventSource } from '@waylaidwanderer/fetch-event-source';
6-
import { Agent } from 'undici';
6+
import { Agent, ProxyAgent } from 'undici';
77

88
const CHATGPT_MODEL = 'text-chat-davinci-002-sh-alpha-aoruigiofdj83';
99

@@ -129,6 +129,11 @@ export default class ChatGPTClient {
129129
headersTimeout: 0,
130130
}),
131131
};
132+
133+
if (this.options.proxy) {
134+
opts.dispatcher = new ProxyAgent(this.options.proxy);
135+
}
136+
132137
if (modelOptions.stream) {
133138
return new Promise(async (resolve, reject) => {
134139
try {

0 commit comments

Comments
 (0)