mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: cancellable api request
This commit is contained in:
parent
66ad54168a
commit
ef9f1ee1cf
11 changed files with 93 additions and 67 deletions
|
|
@ -10,7 +10,7 @@ const clientOptions = {
|
||||||
proxy: process.env.PROXY || null,
|
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 { ChatGPTBrowserClient } = await import('@waylaidwanderer/chatgpt-api');
|
||||||
|
|
||||||
const store = {
|
const store = {
|
||||||
|
|
@ -18,7 +18,7 @@ const browserClient = async ({ text, onProgress, convo }) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const client = new ChatGPTBrowserClient(clientOptions, store);
|
const client = new ChatGPTBrowserClient(clientOptions, store);
|
||||||
let options = { onProgress };
|
let options = { onProgress, abortController };
|
||||||
|
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
if (!!convo.parentMessageId && !!convo.conversationId) {
|
||||||
options = { ...options, ...convo };
|
options = { ...options, ...convo };
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,14 @@ const clientOptions = {
|
||||||
debug: false
|
debug: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const askClient = async ({ text, onProgress, convo }) => {
|
const askClient = async ({ text, onProgress, convo, abortController }) => {
|
||||||
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
||||||
const store = {
|
const store = {
|
||||||
store: new KeyvFile({ filename: './data/cache.json' })
|
store: new KeyvFile({ filename: './data/cache.json' })
|
||||||
};
|
};
|
||||||
|
|
||||||
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
||||||
let options = { onProgress };
|
let options = { onProgress, abortController };
|
||||||
|
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
if (!!convo.parentMessageId && !!convo.conversationId) {
|
||||||
options = { ...options, ...convo };
|
options = { ...options, ...convo };
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ const clientOptions = {
|
||||||
debug: false
|
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 ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
||||||
const store = {
|
const store = {
|
||||||
store: new KeyvFile({ filename: './data/cache.json' })
|
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);
|
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
||||||
|
|
||||||
let options = { onProgress };
|
let options = { onProgress, abortController };
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
if (!!convo.parentMessageId && !!convo.conversationId) {
|
||||||
options = { ...options, ...convo };
|
options = { ...options, ...convo };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,14 @@ const ask = async ({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const progressCallback = createOnProgress();
|
const progressCallback = createOnProgress();
|
||||||
|
|
||||||
|
const abortController = new AbortController();
|
||||||
|
res.on('close', () => {
|
||||||
|
console.log('The client has disconnected.');
|
||||||
|
// 执行其他操作
|
||||||
|
abortController.abort();
|
||||||
|
})
|
||||||
|
|
||||||
let gptResponse = await client({
|
let gptResponse = await client({
|
||||||
text,
|
text,
|
||||||
onProgress: progressCallback.call(null, model, { res, text }),
|
onProgress: progressCallback.call(null, model, { res, text }),
|
||||||
|
|
@ -98,7 +106,8 @@ const ask = async ({
|
||||||
conversationId,
|
conversationId,
|
||||||
...convo
|
...convo
|
||||||
},
|
},
|
||||||
...convo
|
...convo,
|
||||||
|
abortController
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('CLIENT RESPONSE', gptResponse);
|
console.log('CLIENT RESPONSE', gptResponse);
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,14 @@ const ask = async ({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const progressCallback = createOnProgress();
|
const progressCallback = createOnProgress();
|
||||||
|
|
||||||
|
const abortController = new AbortController();
|
||||||
|
res.on('close', () => {
|
||||||
|
console.log('The client has disconnected.');
|
||||||
|
// 执行其他操作
|
||||||
|
abortController.abort();
|
||||||
|
})
|
||||||
|
|
||||||
let response = await askBing({
|
let response = await askBing({
|
||||||
text,
|
text,
|
||||||
onProgress: progressCallback.call(null, model, {
|
onProgress: progressCallback.call(null, model, {
|
||||||
|
|
@ -95,7 +103,8 @@ const ask = async ({
|
||||||
...convo,
|
...convo,
|
||||||
parentMessageId: userParentMessageId,
|
parentMessageId: userParentMessageId,
|
||||||
conversationId
|
conversationId
|
||||||
}
|
},
|
||||||
|
abortController
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('BING RESPONSE', response);
|
console.log('BING RESPONSE', response);
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,14 @@ const ask = async ({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const progressCallback = createOnProgress();
|
const progressCallback = createOnProgress();
|
||||||
|
|
||||||
|
const abortController = new AbortController();
|
||||||
|
res.on('close', () => {
|
||||||
|
console.log('The client has disconnected.');
|
||||||
|
// 执行其他操作
|
||||||
|
abortController.abort();
|
||||||
|
})
|
||||||
|
|
||||||
let response = await askSydney({
|
let response = await askSydney({
|
||||||
text,
|
text,
|
||||||
onProgress: progressCallback.call(null, model, {
|
onProgress: progressCallback.call(null, model, {
|
||||||
|
|
@ -95,7 +103,8 @@ const ask = async ({
|
||||||
parentMessageId: userParentMessageId,
|
parentMessageId: userParentMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
...convo
|
...convo
|
||||||
}
|
},
|
||||||
|
abortController
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('SYDNEY RESPONSE', response);
|
console.log('SYDNEY RESPONSE', response);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useSelector } from 'react-redux';
|
import { useSelector } from 'react-redux';
|
||||||
|
|
||||||
export default function SubmitButton({ submitMessage }) {
|
export default function SubmitButton({ submitMessage, disabled }) {
|
||||||
const { isSubmitting, disabled } = useSelector((state) => state.submit);
|
const { isSubmitting } = useSelector((state) => state.submit);
|
||||||
|
const { error, latestMessage } = useSelector((state) => state.convo);
|
||||||
|
|
||||||
const clickHandler = (e) => {
|
const clickHandler = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
submitMessage();
|
submitMessage();
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,9 @@ export default function TextChat({ messages }) {
|
||||||
useSelector((state) => state.submit);
|
useSelector((state) => state.submit);
|
||||||
const { text } = useSelector((state) => state.text);
|
const { text } = useSelector((state) => state.text);
|
||||||
const { error, latestMessage } = convo;
|
const { error, latestMessage } = convo;
|
||||||
const { ask, regenerate } = useMessageHandler();
|
const { ask, regenerate, stopGenerating } = useMessageHandler();
|
||||||
|
|
||||||
|
const isNotAppendable = (!isSubmitting && latestMessage?.submitting) || latestMessage?.error;
|
||||||
|
|
||||||
// auto focus to input, when enter a conversation.
|
// auto focus to input, when enter a conversation.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -231,6 +233,10 @@ export default function TextChat({ messages }) {
|
||||||
regenerate(latestMessage)
|
regenerate(latestMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleStopGenerating = () => {
|
||||||
|
stopGenerating()
|
||||||
|
}
|
||||||
|
|
||||||
const handleKeyDown = (e) => {
|
const handleKeyDown = (e) => {
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
if (e.key === 'Enter' && !e.shiftKey) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
@ -273,31 +279,23 @@ export default function TextChat({ messages }) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
dispatch(setError(false));
|
dispatch(setError(false));
|
||||||
};
|
};
|
||||||
|
isNotAppendable
|
||||||
return (
|
return (
|
||||||
<div className="md:bg-vert-light-gradient dark:md:bg-vert-dark-gradient absolute bottom-0 left-0 w-full border-t bg-white dark:border-white/20 dark:bg-gray-800 md:border-t-0 md:border-transparent md:!bg-transparent md:dark:border-transparent">
|
<div className="md:bg-vert-light-gradient dark:md:bg-vert-dark-gradient absolute bottom-0 left-0 w-full border-t bg-white dark:border-white/20 dark:bg-gray-800 md:border-t-0 md:border-transparent md:!bg-transparent md:dark:border-transparent">
|
||||||
<form className="stretch mx-2 flex flex-row gap-3 pt-2 last:mb-2 md:last:mb-6 lg:mx-auto lg:max-w-3xl lg:pt-6">
|
<form className="stretch mx-2 flex flex-row gap-3 pt-2 last:mb-2 md:last:mb-6 lg:mx-auto lg:max-w-3xl lg:pt-6">
|
||||||
<div className="relative flex h-full flex-1 md:flex-col">
|
<div className="relative flex h-full flex-1 md:flex-col">
|
||||||
<div className="ml-1 mt-1.5 flex justify-center gap-0 md:m-auto md:mb-2 md:w-full md:gap-2" />
|
<div className="ml-1 mt-1.5 flex justify-center gap-0 md:m-auto md:mb-2 md:w-full md:gap-2" />
|
||||||
{error ? (
|
|
||||||
<Regenerate
|
|
||||||
submitMessage={submitMessage}
|
|
||||||
tryAgain={tryAgain}
|
|
||||||
errorMessage={errorMessage}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<span className="flex ml-1 md:w-full md:m-auto md:mb-2 gap-0 md:gap-2 justify-center">
|
<span className="flex ml-1 md:w-full md:m-auto md:mb-2 gap-0 md:gap-2 justify-center">
|
||||||
{(latestMessage&&!latestMessage?.isCreatedByUser)?
|
{isSubmitting?
|
||||||
isSubmitting?
|
|
||||||
<button
|
<button
|
||||||
onClick={null}
|
onClick={handleStopGenerating}
|
||||||
className="btn btn-neutral flex justify-center gap-2 border-0 md:border"
|
className="btn btn-neutral flex justify-center gap-2 border-0 md:border"
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
<StopGeneratingIcon />
|
<StopGeneratingIcon />
|
||||||
Stop Generating
|
Stop generating
|
||||||
</button>:
|
</button>
|
||||||
|
:(latestMessage&&!latestMessage?.isCreatedByUser)?
|
||||||
<button
|
<button
|
||||||
onClick={handleRegenerate}
|
onClick={handleRegenerate}
|
||||||
className="btn btn-neutral flex justify-center gap-2 border-0 md:border"
|
className="btn btn-neutral flex justify-center gap-2 border-0 md:border"
|
||||||
|
|
@ -329,14 +327,12 @@ export default function TextChat({ messages }) {
|
||||||
onChange={changeHandler}
|
onChange={changeHandler}
|
||||||
onCompositionStart={handleCompositionStart}
|
onCompositionStart={handleCompositionStart}
|
||||||
onCompositionEnd={handleCompositionEnd}
|
onCompositionEnd={handleCompositionEnd}
|
||||||
placeholder={disabled ? 'Choose another model or customize GPT again' : ''}
|
placeholder={disabled ? 'Choose another model or customize GPT again' : isNotAppendable ? 'Can not send new message after an error or unfinished response.' : ''}
|
||||||
disabled={disabled}
|
disabled={disabled || isNotAppendable}
|
||||||
className="m-0 h-auto max-h-52 resize-none overflow-auto border-0 bg-transparent p-0 pl-9 pr-8 leading-6 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent md:pl-8"
|
className="m-0 h-auto max-h-52 resize-none overflow-auto border-0 bg-transparent p-0 pl-9 pr-8 leading-6 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent md:pl-8"
|
||||||
/>
|
/>
|
||||||
<SubmitButton submitMessage={submitMessage} />
|
<SubmitButton submitMessage={submitMessage} disabled={disabled || isNotAppendable} />
|
||||||
</div>
|
</div>
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ export default function Message({
|
||||||
dispatch(setConversation({ parentMessageId: message?.messageId }));
|
dispatch(setConversation({ parentMessageId: message?.messageId }));
|
||||||
dispatch(setLatestMessage({ ...message }));
|
dispatch(setLatestMessage({ ...message }));
|
||||||
}
|
}
|
||||||
}, [last]);
|
}, [last, message]);
|
||||||
|
|
||||||
const enterEdit = (cancel) => setCurrentEditId(cancel ? -1 : message.messageId);
|
const enterEdit = (cancel) => setCurrentEditId(cancel ? -1 : message.messageId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ const Messages = ({ messages, messageTree }) => {
|
||||||
<div className="flex w-full items-center justify-center gap-1 border-b border-black/10 bg-gray-50 p-3 text-gray-500 dark:border-gray-900/50 dark:bg-gray-700 dark:text-gray-300 text-sm">
|
<div className="flex w-full items-center justify-center gap-1 border-b border-black/10 bg-gray-50 p-3 text-gray-500 dark:border-gray-900/50 dark:bg-gray-700 dark:text-gray-300 text-sm">
|
||||||
Model: {modelName} {customModel?`(${customModel})`:null}
|
Model: {modelName} {customModel?`(${customModel})`:null}
|
||||||
</div>
|
</div>
|
||||||
<div className="dark:gpt-dark-gray flex h-full flex-col items-center text-sm">
|
<div className="dark:gpt-dark-gray flex flex-col items-center text-sm">
|
||||||
{messageTree.length === 0 ? (
|
{messageTree.length === 0 ? (
|
||||||
<Spinner />
|
<Spinner />
|
||||||
) : (
|
) : (
|
||||||
|
|
|
||||||
|
|
@ -42,10 +42,8 @@ const useMessageHandler = () => {
|
||||||
|
|
||||||
dispatch(setSubmitState(true));
|
dispatch(setSubmitState(true));
|
||||||
if (isRegenerate) {
|
if (isRegenerate) {
|
||||||
console.log([...currentMessages, initialResponse])
|
|
||||||
dispatch(setMessages([...currentMessages, initialResponse]));
|
dispatch(setMessages([...currentMessages, initialResponse]));
|
||||||
} else {
|
} else {
|
||||||
console.log([...currentMessages, currentMsg, initialResponse])
|
|
||||||
dispatch(setMessages([...currentMessages, currentMsg, initialResponse]));
|
dispatch(setMessages([...currentMessages, currentMsg, initialResponse]));
|
||||||
}
|
}
|
||||||
dispatch(setText(''));
|
dispatch(setText(''));
|
||||||
|
|
@ -78,8 +76,11 @@ const useMessageHandler = () => {
|
||||||
console.error('Failed to regenerate the message: parentMessage not found or not created by user.', message);
|
console.error('Failed to regenerate the message: parentMessage not found or not created by user.', message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const stopGenerating = () => {
|
||||||
|
dispatch(setSubmission({}));
|
||||||
|
}
|
||||||
|
|
||||||
return { ask, regenerate }
|
return { ask, regenerate, stopGenerating }
|
||||||
}
|
}
|
||||||
|
|
||||||
export { useMessageHandler };
|
export { useMessageHandler };
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue