add additional finish conditions to provide better ollama support
This commit is contained in:
parent
d6b536ace9
commit
c2799c9494
@ -545,6 +545,16 @@ const ChatPanel: FC = observer(() => {
|
|||||||
commonStore.setConversationOrder(commonStore.conversationOrder);
|
commonStore.setConversationOrder(commonStore.conversationOrder);
|
||||||
setTimeout(scrollToBottom);
|
setTimeout(scrollToBottom);
|
||||||
let answer = '';
|
let answer = '';
|
||||||
|
let finished = false;
|
||||||
|
const finish = () => {
|
||||||
|
finished = true;
|
||||||
|
if (answerId! in chatSseControllers)
|
||||||
|
delete chatSseControllers[answerId!];
|
||||||
|
commonStore.conversation[answerId!].done = true;
|
||||||
|
commonStore.conversation[answerId!].content = commonStore.conversation[answerId!].content.trim();
|
||||||
|
commonStore.setConversation(commonStore.conversation);
|
||||||
|
commonStore.setConversationOrder([...commonStore.conversationOrder]);
|
||||||
|
};
|
||||||
const chatSseController = new AbortController();
|
const chatSseController = new AbortController();
|
||||||
chatSseControllers[answerId] = chatSseController;
|
chatSseControllers[answerId] = chatSseController;
|
||||||
fetchEventSource( // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/v1/chat/completions
|
fetchEventSource( // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/v1/chat/completions
|
||||||
@ -572,13 +582,8 @@ const ChatPanel: FC = observer(() => {
|
|||||||
signal: chatSseController?.signal,
|
signal: chatSseController?.signal,
|
||||||
onmessage(e) {
|
onmessage(e) {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
if (e.data.trim() === '[DONE]') {
|
if (!finished && e.data.trim() === '[DONE]') {
|
||||||
if (answerId! in chatSseControllers)
|
finish();
|
||||||
delete chatSseControllers[answerId!];
|
|
||||||
commonStore.conversation[answerId!].done = true;
|
|
||||||
commonStore.conversation[answerId!].content = commonStore.conversation[answerId!].content.trim();
|
|
||||||
commonStore.setConversation(commonStore.conversation);
|
|
||||||
commonStore.setConversationOrder([...commonStore.conversationOrder]);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let data;
|
let data;
|
||||||
@ -591,6 +596,10 @@ const ChatPanel: FC = observer(() => {
|
|||||||
if (data.model)
|
if (data.model)
|
||||||
commonStore.setLastModelName(data.model);
|
commonStore.setLastModelName(data.model);
|
||||||
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
||||||
|
if (!finished && data.choices[0]?.finish_reason) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
answer += data.choices[0]?.delta?.content || '';
|
answer += data.choices[0]?.delta?.content || '';
|
||||||
commonStore.conversation[answerId!].content = answer;
|
commonStore.conversation[answerId!].content = answer;
|
||||||
commonStore.setConversation(commonStore.conversation);
|
commonStore.setConversation(commonStore.conversation);
|
||||||
|
@ -80,6 +80,11 @@ const CompletionPanel: FC = observer(() => {
|
|||||||
prompt += params.injectStart.replaceAll('\\n', '\n');
|
prompt += params.injectStart.replaceAll('\\n', '\n');
|
||||||
|
|
||||||
let answer = '';
|
let answer = '';
|
||||||
|
let finished = false;
|
||||||
|
const finish = () => {
|
||||||
|
finished = true;
|
||||||
|
commonStore.setCompletionGenerating(false);
|
||||||
|
};
|
||||||
completionSseController = new AbortController();
|
completionSseController = new AbortController();
|
||||||
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
|
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
|
||||||
getServerRoot(port, true) + '/v1/completions',
|
getServerRoot(port, true) + '/v1/completions',
|
||||||
@ -103,8 +108,8 @@ const CompletionPanel: FC = observer(() => {
|
|||||||
signal: completionSseController?.signal,
|
signal: completionSseController?.signal,
|
||||||
onmessage(e) {
|
onmessage(e) {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
if (e.data.trim() === '[DONE]') {
|
if (!finished && e.data.trim() === '[DONE]') {
|
||||||
commonStore.setCompletionGenerating(false);
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let data;
|
let data;
|
||||||
@ -117,6 +122,10 @@ const CompletionPanel: FC = observer(() => {
|
|||||||
if (data.model)
|
if (data.model)
|
||||||
commonStore.setLastModelName(data.model);
|
commonStore.setLastModelName(data.model);
|
||||||
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
||||||
|
if (!finished && data.choices[0]?.finish_reason) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
answer += data.choices[0]?.text || data.choices[0]?.delta?.content || '';
|
answer += data.choices[0]?.text || data.choices[0]?.delta?.content || '';
|
||||||
setPrompt(prompt + answer.replace(/\s+$/, '') + params.injectEnd.replaceAll('\\n', '\n'));
|
setPrompt(prompt + answer.replace(/\s+$/, '') + params.injectEnd.replaceAll('\\n', '\n'));
|
||||||
}
|
}
|
||||||
|
@ -185,6 +185,12 @@ const CompositionPanel: FC = observer(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let answer = '';
|
let answer = '';
|
||||||
|
let finished = false;
|
||||||
|
const finish = () => {
|
||||||
|
finished = true;
|
||||||
|
commonStore.setCompositionGenerating(false);
|
||||||
|
generateNs(commonStore.compositionParams.autoPlay);
|
||||||
|
};
|
||||||
compositionSseController = new AbortController();
|
compositionSseController = new AbortController();
|
||||||
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
|
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
|
||||||
getServerRoot(port, true) + '/v1/completions',
|
getServerRoot(port, true) + '/v1/completions',
|
||||||
@ -205,9 +211,8 @@ const CompositionPanel: FC = observer(() => {
|
|||||||
signal: compositionSseController?.signal,
|
signal: compositionSseController?.signal,
|
||||||
onmessage(e) {
|
onmessage(e) {
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
if (e.data.trim() === '[DONE]') {
|
if (!finished && e.data.trim() === '[DONE]') {
|
||||||
commonStore.setCompositionGenerating(false);
|
finish();
|
||||||
generateNs(commonStore.compositionParams.autoPlay);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let data;
|
let data;
|
||||||
@ -220,6 +225,10 @@ const CompositionPanel: FC = observer(() => {
|
|||||||
if (data.model)
|
if (data.model)
|
||||||
commonStore.setLastModelName(data.model);
|
commonStore.setLastModelName(data.model);
|
||||||
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
||||||
|
if (!finished && data.choices[0]?.finish_reason) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
answer += data.choices[0]?.text || data.choices[0]?.delta?.content || '';
|
answer += data.choices[0]?.text || data.choices[0]?.delta?.content || '';
|
||||||
setPrompt(prompt + answer.replace(/\s+$/, ''));
|
setPrompt(prompt + answer.replace(/\s+$/, ''));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user