improve sse fetch

This commit is contained in:
josc146 2023-07-25 15:59:37 +08:00
parent 5ae683e915
commit 211ae342af
2 changed files with 10 additions and 6 deletions

View File

@ -184,7 +184,9 @@ const ChatPanel: FC = observer(() => {
const bodyRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const mq = useMediaQuery('(min-width: 640px)');
const port = commonStore.getCurrentModelConfig().apiParameters.apiPort;
const currentConfig = commonStore.getCurrentModelConfig();
const apiParams = currentConfig.apiParameters;
const port = apiParams.apiPort;
let lastMessageId: string;
let generating: boolean = false;
@ -308,12 +310,14 @@ const ChatPanel: FC = observer(() => {
body: JSON.stringify({
messages,
stream: true,
model: commonStore.settings.apiChatModelName // 'gpt-3.5-turbo'
model: commonStore.settings.apiChatModelName, // 'gpt-3.5-turbo'
temperature: apiParams.temperature,
top_p: apiParams.topP
}),
signal: chatSseController?.signal,
onmessage(e) {
scrollToBottom();
if (e.data === '[DONE]') {
if (e.data.trim() === '[DONE]') {
commonStore.conversation[answerId!].done = true;
commonStore.conversation[answerId!].content = commonStore.conversation[answerId!].content.trim();
commonStore.setConversation(commonStore.conversation);

View File

@ -219,7 +219,7 @@ const CompletionPanel: FC = observer(() => {
signal: completionSseController?.signal,
onmessage(e) {
scrollToBottom();
if (e.data === '[DONE]') {
if (e.data.trim() === '[DONE]') {
commonStore.setCompletionGenerating(false);
return;
}
@ -231,8 +231,8 @@ const CompletionPanel: FC = observer(() => {
return;
}
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
answer += data.choices[0].text;
setPrompt(prompt + answer.trim() + params.injectEnd.replaceAll('\\n', '\n'));
answer += data.choices[0]?.text || data.choices[0]?.delta?.content || '';
setPrompt(prompt + answer.replace(/\s+$/, '') + params.injectEnd.replaceAll('\\n', '\n'));
}
},
async onopen(response) {