fix: text completion

This commit is contained in:
Timothy J. Baek 2024-03-02 18:23:27 -08:00
parent 901e7a33fa
commit bf3f3c580e

View file

@ -61,15 +61,26 @@
}; };
const textCompletionHandler = async () => { const textCompletionHandler = async () => {
const [res, controller] = await generateChatCompletion(localStorage.token, { const model = $models.find((model) => model.id === selectedModelId);
model: selectedModelId,
const res = await generateOpenAIChatCompletion(
localStorage.token,
{
model: model.id,
stream: true,
messages: [ messages: [
{ {
role: 'assistant', role: 'assistant',
content: text 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) { if (res && res.ok) {
const reader = res.body const reader = res.body
@ -80,10 +91,6 @@
while (true) { while (true) {
const { value, done } = await reader.read(); const { value, done } = await reader.read();
if (done || stopResponseFlag) { if (done || stopResponseFlag) {
if (stopResponseFlag) {
await cancelChatCompletion(localStorage.token, currentRequestId);
}
currentRequestId = null; currentRequestId = null;
break; break;
} }
@ -93,22 +100,14 @@
for (const line of lines) { for (const line of lines) {
if (line !== '') { if (line !== '') {
console.log(line); if (line === 'data: [DONE]') {
let data = JSON.parse(line); // responseMessage.done = true;
if ('detail' in data) {
throw data;
}
if ('id' in data) {
console.log(data);
currentRequestId = data.id;
} else {
if (data.done == false) {
text += data.message.content;
} else {
console.log('done'); console.log('done');
} } else {
let data = JSON.parse(line.replace(/^data: /, ''));
console.log(data);
text += data.choices[0].delta.content ?? '';
} }
} }
} }