enh: stream=false support

This commit is contained in:
Timothy J. Baek 2024-09-19 16:25:59 +02:00
parent 628d7ae72d
commit e99cba53fe
2 changed files with 248 additions and 187 deletions

View File

@ -115,8 +115,6 @@
$: if (history.currentId !== null) { $: if (history.currentId !== null) {
let _messages = []; let _messages = [];
console.log(history.currentId);
let currentMessage = history.messages[history.currentId]; let currentMessage = history.messages[history.currentId];
while (currentMessage) { while (currentMessage) {
_messages.unshift({ ...currentMessage }); _messages.unshift({ ...currentMessage });
@ -885,8 +883,9 @@
await tick(); await tick();
const stream = $settings?.streamResponse ?? true;
const [res, controller] = await generateChatCompletion(localStorage.token, { const [res, controller] = await generateChatCompletion(localStorage.token, {
stream: true, stream: stream,
model: model.id, model: model.id,
messages: messagesBody, messages: messagesBody,
options: { options: {
@ -911,6 +910,21 @@
}); });
if (res && res.ok) { if (res && res.ok) {
if (!stream) {
const response = await res.json();
console.log(response);
responseMessage.content = response.message.content;
responseMessage.info = {
eval_count: response.eval_count,
eval_duration: response.eval_duration,
load_duration: response.load_duration,
prompt_eval_count: response.prompt_eval_count,
prompt_eval_duration: response.prompt_eval_duration,
total_duration: response.total_duration
};
responseMessage.done = true;
} else {
console.log('controller', controller); console.log('controller', controller);
const reader = res.body const reader = res.body
@ -926,9 +940,6 @@
if (stopResponseFlag) { if (stopResponseFlag) {
controller.abort('User: Stop Response'); controller.abort('User: Stop Response');
} else {
const messages = createMessagesList(responseMessageId);
await chatCompletedHandler(_chatId, model.id, responseMessageId, messages);
} }
_response = responseMessage.content; _response = responseMessage.content;
@ -1047,6 +1058,14 @@
scrollToBottom(); scrollToBottom();
} }
} }
}
await chatCompletedHandler(
_chatId,
model.id,
responseMessageId,
createMessagesList(responseMessageId)
);
} else { } else {
if (res !== null) { if (res !== null) {
const error = await res.json(); const error = await res.json();
@ -1158,17 +1177,19 @@
await tick(); await tick();
try { try {
const stream = $settings?.streamResponse ?? true;
const [res, controller] = await generateOpenAIChatCompletion( const [res, controller] = await generateOpenAIChatCompletion(
localStorage.token, localStorage.token,
{ {
stream: true, stream: stream,
model: model.id, model: model.id,
stream_options: ...(stream && (model.info?.meta?.capabilities?.usage ?? false)
(model.info?.meta?.capabilities?.usage ?? false)
? { ? {
stream_options: {
include_usage: true include_usage: true
} }
: undefined, }
: {}),
messages: [ messages: [
params?.system || $settings.system || (responseMessage?.userContext ?? null) params?.system || $settings.system || (responseMessage?.userContext ?? null)
? { ? {
@ -1246,6 +1267,14 @@
scrollToBottom(); scrollToBottom();
if (res && res.ok && res.body) { if (res && res.ok && res.body) {
if (!stream) {
const response = await res.json();
console.log(response);
responseMessage.content = response.choices[0].message.content;
responseMessage.info = { ...response.usage, openai: true };
responseMessage.done = true;
} else {
const textStream = await createOpenAITextStream(res.body, $settings.splitLargeChunks); const textStream = await createOpenAITextStream(res.body, $settings.splitLargeChunks);
for await (const update of textStream) { for await (const update of textStream) {
@ -1260,14 +1289,8 @@
if (stopResponseFlag) { if (stopResponseFlag) {
controller.abort('User: Stop Response'); controller.abort('User: Stop Response');
} else {
const messages = createMessagesList(responseMessageId);
await chatCompletedHandler(_chatId, model.id, responseMessageId, messages);
} }
_response = responseMessage.content; _response = responseMessage.content;
break; break;
} }
@ -1324,6 +1347,14 @@
scrollToBottom(); scrollToBottom();
} }
} }
}
await chatCompletedHandler(
_chatId,
model.id,
responseMessageId,
createMessagesList(responseMessageId)
);
if ($settings.notificationEnabled && !document.hasFocus()) { if ($settings.notificationEnabled && !document.hasFocus()) {
const notification = new Notification(`${model.id}`, { const notification = new Notification(`${model.id}`, {

View File

@ -36,11 +36,18 @@
let voiceInterruption = false; let voiceInterruption = false;
let hapticFeedback = false; let hapticFeedback = false;
let streamResponse = true;
const toggleSplitLargeChunks = async () => { const toggleSplitLargeChunks = async () => {
splitLargeChunks = !splitLargeChunks; splitLargeChunks = !splitLargeChunks;
saveSettings({ splitLargeChunks: splitLargeChunks }); saveSettings({ splitLargeChunks: splitLargeChunks });
}; };
const toggleStreamResponse = async () => {
streamResponse = !streamResponse;
saveSettings({ streamResponse: streamResponse });
};
const togglesScrollOnBranchChange = async () => { const togglesScrollOnBranchChange = async () => {
scrollOnBranchChange = !scrollOnBranchChange; scrollOnBranchChange = !scrollOnBranchChange;
saveSettings({ scrollOnBranchChange: scrollOnBranchChange }); saveSettings({ scrollOnBranchChange: scrollOnBranchChange });
@ -158,6 +165,7 @@
userLocation = $settings.userLocation ?? false; userLocation = $settings.userLocation ?? false;
hapticFeedback = $settings.hapticFeedback ?? false; hapticFeedback = $settings.hapticFeedback ?? false;
streamResponse = $settings?.streamResponse ?? true;
defaultModelId = $settings?.models?.at(0) ?? ''; defaultModelId = $settings?.models?.at(0) ?? '';
if ($config?.default_models) { if ($config?.default_models) {
@ -311,6 +319,28 @@
</div> </div>
</div> </div>
<div>
<div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs">
{$i18n.t('Stream Chat Response')}
</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
on:click={() => {
toggleStreamResponse();
}}
type="button"
>
{#if streamResponse === true}
<span class="ml-2 self-center">{$i18n.t('On')}</span>
{:else}
<span class="ml-2 self-center">{$i18n.t('Off')}</span>
{/if}
</button>
</div>
</div>
<div> <div>
<div class=" py-0.5 flex w-full justify-between"> <div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs"> <div class=" self-center text-xs">