From bf3f3c580e68c5509136eb42f448a5b656e0d5a8 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 2 Mar 2024 18:23:27 -0800 Subject: [PATCH] fix: text completion --- src/routes/(app)/playground/+page.svelte | 55 ++++++++++++------------ 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/routes/(app)/playground/+page.svelte b/src/routes/(app)/playground/+page.svelte index 58639e61..94e64292 100644 --- a/src/routes/(app)/playground/+page.svelte +++ b/src/routes/(app)/playground/+page.svelte @@ -61,15 +61,26 @@ }; const textCompletionHandler = async () => { - const [res, controller] = await generateChatCompletion(localStorage.token, { - model: selectedModelId, - messages: [ - { - role: 'assistant', - content: text - } - ] - }); + const model = $models.find((model) => model.id === selectedModelId); + + const res = await generateOpenAIChatCompletion( + localStorage.token, + { + model: model.id, + stream: true, + messages: [ + { + role: 'assistant', + content: text + } + ] + }, + model.external + ? model.source === 'litellm' + ? `${LITELLM_API_BASE_URL}/v1` + : `${OPENAI_API_BASE_URL}` + : `${OLLAMA_API_BASE_URL}/v1` + ); if (res && res.ok) { const reader = res.body @@ -80,10 +91,6 @@ while (true) { const { value, done } = await reader.read(); if (done || stopResponseFlag) { - if (stopResponseFlag) { - await cancelChatCompletion(localStorage.token, currentRequestId); - } - currentRequestId = null; break; } @@ -93,22 +100,14 @@ for (const line of lines) { if (line !== '') { - console.log(line); - let data = JSON.parse(line); - - if ('detail' in data) { - throw data; - } - - if ('id' in data) { - console.log(data); - currentRequestId = data.id; + if (line === 'data: [DONE]') { + // responseMessage.done = true; + console.log('done'); } else { - if (data.done == false) { - text += data.message.content; - } else { - console.log('done'); - } + let data = JSON.parse(line.replace(/^data: /, '')); + console.log(data); + + text += data.choices[0].delta.content ?? ''; } } }