feat: cancellable api request

This commit is contained in:
Wentao Lyu 2023-03-17 03:13:42 +08:00
parent 66ad54168a
commit ef9f1ee1cf
11 changed files with 93 additions and 67 deletions

View file

@ -10,7 +10,7 @@ const clientOptions = {
proxy: process.env.PROXY || null,
};
const browserClient = async ({ text, onProgress, convo }) => {
const browserClient = async ({ text, onProgress, convo, abortController }) => {
const { ChatGPTBrowserClient } = await import('@waylaidwanderer/chatgpt-api');
const store = {
@ -18,7 +18,7 @@ const browserClient = async ({ text, onProgress, convo }) => {
};
const client = new ChatGPTBrowserClient(clientOptions, store);
let options = { onProgress };
let options = { onProgress, abortController };
if (!!convo.parentMessageId && !!convo.conversationId) {
options = { ...options, ...convo };

View file

@ -9,14 +9,14 @@ const clientOptions = {
debug: false
};
const askClient = async ({ text, onProgress, convo }) => {
const askClient = async ({ text, onProgress, convo, abortController }) => {
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
const store = {
store: new KeyvFile({ filename: './data/cache.json' })
};
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
let options = { onProgress };
let options = { onProgress, abortController };
if (!!convo.parentMessageId && !!convo.conversationId) {
options = { ...options, ...convo };

View file

@ -9,7 +9,7 @@ const clientOptions = {
debug: false
};
const customClient = async ({ text, onProgress, convo, promptPrefix, chatGptLabel }) => {
const customClient = async ({ text, onProgress, convo, promptPrefix, chatGptLabel, abortController }) => {
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
const store = {
store: new KeyvFile({ filename: './data/cache.json' })
@ -23,7 +23,7 @@ const customClient = async ({ text, onProgress, convo, promptPrefix, chatGptLabe
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
let options = { onProgress };
let options = { onProgress, abortController };
if (!!convo.parentMessageId && !!convo.conversationId) {
options = { ...options, ...convo };
}

View file

@ -90,6 +90,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let gptResponse = await client({
text,
onProgress: progressCallback.call(null, model, { res, text }),
@ -98,7 +106,8 @@ const ask = async ({
conversationId,
...convo
},
...convo
...convo,
abortController
});
console.log('CLIENT RESPONSE', gptResponse);

View file

@ -84,6 +84,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let response = await askBing({
text,
onProgress: progressCallback.call(null, model, {
@ -95,7 +103,8 @@ const ask = async ({
...convo,
parentMessageId: userParentMessageId,
conversationId
}
},
abortController
});
console.log('BING RESPONSE', response);

View file

@ -84,6 +84,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let response = await askSydney({
text,
onProgress: progressCallback.call(null, model, {
@ -95,7 +103,8 @@ const ask = async ({
parentMessageId: userParentMessageId,
conversationId,
...convo
}
},
abortController
});
console.log('SYDNEY RESPONSE', response);