add additional finish conditions to provide better ollama support

This commit is contained in:
josc146 2024-03-26 15:02:27 +08:00
parent d6b536ace9
commit c2799c9494
3 changed files with 39 additions and 12 deletions

View File

@ -545,6 +545,16 @@ const ChatPanel: FC = observer(() => {
commonStore.setConversationOrder(commonStore.conversationOrder);
setTimeout(scrollToBottom);
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();
chatSseControllers[answerId] = chatSseController;
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,
onmessage(e) {
scrollToBottom();
if (e.data.trim() === '[DONE]') {
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]);
if (!finished && e.data.trim() === '[DONE]') {
finish();
return;
}
let data;
@ -591,6 +596,10 @@ const ChatPanel: FC = observer(() => {
if (data.model)
commonStore.setLastModelName(data.model);
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 || '';
commonStore.conversation[answerId!].content = answer;
commonStore.setConversation(commonStore.conversation);

View File

@ -80,6 +80,11 @@ const CompletionPanel: FC = observer(() => {
prompt += params.injectStart.replaceAll('\\n', '\n');
let answer = '';
let finished = false;
const finish = () => {
finished = true;
commonStore.setCompletionGenerating(false);
};
completionSseController = new AbortController();
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
getServerRoot(port, true) + '/v1/completions',
@ -103,8 +108,8 @@ const CompletionPanel: FC = observer(() => {
signal: completionSseController?.signal,
onmessage(e) {
scrollToBottom();
if (e.data.trim() === '[DONE]') {
commonStore.setCompletionGenerating(false);
if (!finished && e.data.trim() === '[DONE]') {
finish();
return;
}
let data;
@ -117,6 +122,10 @@ const CompletionPanel: FC = observer(() => {
if (data.model)
commonStore.setLastModelName(data.model);
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 || '';
setPrompt(prompt + answer.replace(/\s+$/, '') + params.injectEnd.replaceAll('\\n', '\n'));
}

View File

@ -185,6 +185,12 @@ const CompositionPanel: FC = observer(() => {
}
let answer = '';
let finished = false;
const finish = () => {
finished = true;
commonStore.setCompositionGenerating(false);
generateNs(commonStore.compositionParams.autoPlay);
};
compositionSseController = new AbortController();
fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions
getServerRoot(port, true) + '/v1/completions',
@ -205,9 +211,8 @@ const CompositionPanel: FC = observer(() => {
signal: compositionSseController?.signal,
onmessage(e) {
scrollToBottom();
if (e.data.trim() === '[DONE]') {
commonStore.setCompositionGenerating(false);
generateNs(commonStore.compositionParams.autoPlay);
if (!finished && e.data.trim() === '[DONE]') {
finish();
return;
}
let data;
@ -220,6 +225,10 @@ const CompositionPanel: FC = observer(() => {
if (data.model)
commonStore.setLastModelName(data.model);
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 || '';
setPrompt(prompt + answer.replace(/\s+$/, ''));
}