Skip to content

Commit

Permalink
fix: various cleanups
Browse files Browse the repository at this point in the history
- pass content & status to result dropdown
- dont store streaming updates in db
- make status generation non blocking
  • Loading branch information
nsarrazin committed Nov 30, 2024
1 parent 0b3206b commit e62d992
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 41 deletions.
8 changes: 7 additions & 1 deletion src/lib/components/chat/ChatMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
type MessageWebSearchUpdate,
type MessageFinalAnswerUpdate,
type MessageReasoningUpdate,
MessageReasoningUpdateType,
} from "$lib/types/MessageUpdate";
import { base } from "$app/paths";
import { useConvTreeStore } from "$lib/stores/convTree";
Expand Down Expand Up @@ -217,8 +218,13 @@
<OpenWebSearchResults webSearchMessages={searchUpdates} />
{/if}
{#if reasoningUpdates && reasoningUpdates.length > 0}
{@const summaries = reasoningUpdates
.filter((u) => u.subtype === MessageReasoningUpdateType.Status)
.map((u) => u.status)}

<OpenReasoningResults
updates={reasoningUpdates}
summary={summaries[summaries.length - 1] || ""}
content={message.reasoning || ""}
loading={loading && message.content.length === 0}
/>
{/if}
Expand Down
35 changes: 17 additions & 18 deletions src/lib/components/chat/OpenReasoningResults.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
<script lang="ts">
import {
MessageReasoningUpdateType,
type MessageReasoningUpdate,
} from "$lib/types/MessageUpdate";
import IconThought from "~icons/carbon/circle-packing";
import MarkdownRenderer from "./MarkdownRenderer.svelte";
export let updates: MessageReasoningUpdate[];
export let summary: string;
export let content: string;
export let loading: boolean = false;
$: summaries = updates
.filter((u) => u.subtype === MessageReasoningUpdateType.Status)
.map((u) => u.status);
$: content = updates
.filter((u) => u.subtype === MessageReasoningUpdateType.Stream)
.reduce((acc, u) => acc + u.token, "");
$: lastSummary = summaries[summaries.length - 1] || "";
</script>

<details
Expand All @@ -30,15 +16,15 @@
<div
class="relative grid aspect-square place-content-center overflow-hidden rounded-lg bg-gray-100 dark:bg-gray-800"
>
<IconThought class="text-lg {loading ? 'animate-spin' : ''}" />
<IconThought class="text-lg {loading ? 'animate-spin-slow' : ''}" />
</div>
<dl class="leading-4">
<dd class="text-sm">Reasoning</dd>
<dt
class="flex items-center gap-1 truncate whitespace-nowrap text-[.82rem] text-gray-400"
class:animate-pulse={loading}
>
{lastSummary}
{summary}
</dt>
</dl>
</summary>
Expand All @@ -54,4 +40,17 @@
details summary::-webkit-details-marker {
display: none;
}
:global(.animate-spin-slow) {
animation: spin 3s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>
17 changes: 15 additions & 2 deletions src/lib/server/textGeneration/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function* generate(
let reasoning = false;
let reasoningBuffer = "";
let lastReasoningUpdate = new Date();

let status = "";
if (
model.reasoning &&
(model.reasoning.type === "regex" || model.reasoning.type === "summarize")
Expand Down Expand Up @@ -129,9 +129,22 @@ Do not use prefixes such as Response: or Answer: when answering to the user.`,
if (reasoning) {
reasoningBuffer += output.token.text;

// yield status update if it has changed
if (status !== "") {
yield {
type: MessageUpdateType.Reasoning,
subtype: MessageReasoningUpdateType.Status,
status,
};
status = "";
}

// create a new status every 5 seconds
if (new Date().getTime() - lastReasoningUpdate.getTime() > 5000) {
lastReasoningUpdate = new Date();
yield* generateSummaryOfReasoning(reasoningBuffer);
generateSummaryOfReasoning(reasoningBuffer).then((summary) => {
status = summary;
});
}
yield {
type: MessageUpdateType.Reasoning,
Expand Down
17 changes: 5 additions & 12 deletions src/lib/server/textGeneration/reasoning.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { MessageReasoningUpdateType, type MessageReasoningUpdate } from "$lib/types/MessageUpdate";

import { MessageUpdateType } from "$lib/types/MessageUpdate";

import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";

import { getReturnFromGenerator } from "$lib/utils/getReturnFromGenerator";

export async function* generateSummaryOfReasoning(
buffer: string
): AsyncGenerator<MessageReasoningUpdate> {
export async function generateSummaryOfReasoning(buffer: string): Promise<string> {
// debug 5s delay
await new Promise((resolve) => setTimeout(resolve, 3000));

const summary = await getReturnFromGenerator(
generateFromDefaultEndpoint({
messages: [
Expand All @@ -29,9 +26,5 @@ export async function* generateSummaryOfReasoning(
return parts[0] + "...";
});

yield {
type: MessageUpdateType.Reasoning,
subtype: MessageReasoningUpdateType.Status,
status: summary,
};
return summary;
}
2 changes: 2 additions & 0 deletions src/lib/types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type Message = Partial<Timestamps> & {
updates?: MessageUpdate[];
webSearchId?: WebSearch["_id"]; // legacy version
webSearch?: WebSearch;

reasoning?: string;
score?: -1 | 0 | 1;
/**
* Either contains the base64 encoded image data
Expand Down
19 changes: 11 additions & 8 deletions src/routes/conversation/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import { webSearchParameters } from "$lib/stores/webSearchParameters";
import type { Message } from "$lib/types/Message";
import {
MessageReasoningUpdateType,
MessageUpdateStatus,
MessageUpdateType,
type MessageUpdate,
} from "$lib/types/MessageUpdate";
import titleUpdate from "$lib/stores/titleUpdate";
import file2base64 from "$lib/utils/file2base64";
Expand Down Expand Up @@ -215,8 +215,6 @@
files = [];
const messageUpdates: MessageUpdate[] = [];
for await (const update of messageUpdatesIterator) {
if ($isAborted) {
messageUpdatesAbortController.abort();
Expand All @@ -228,7 +226,8 @@
if (update.type === MessageUpdateType.Stream) {
update.token = update.token.replaceAll("\0", "");
}
messageUpdates.push(update);
messageToWriteTo.updates = [...(messageToWriteTo.updates ?? []), update];
if (update.type === MessageUpdateType.Stream && !$settings.disableStream) {
messageToWriteTo.content += update.token;
Expand All @@ -238,7 +237,6 @@
update.type === MessageUpdateType.WebSearch ||
update.type === MessageUpdateType.Tool
) {
messageToWriteTo.updates = [...(messageToWriteTo.updates ?? []), update];
messages = [...messages];
} else if (
update.type === MessageUpdateType.Status &&
Expand All @@ -262,12 +260,17 @@
];
messages = [...messages];
} else if (update.type === MessageUpdateType.Reasoning) {
messageToWriteTo.updates = [...(messageToWriteTo.updates ?? []), update];
if (!messageToWriteTo.reasoning) {
messageToWriteTo.reasoning = "";
}
if (update.subtype === MessageReasoningUpdateType.Stream) {
messageToWriteTo.reasoning += update.token;
} else {
messageToWriteTo.updates = [...(messageToWriteTo.updates ?? []), update];
}
messages = [...messages];
}
}
messageToWriteTo.updates = messageUpdates;
} catch (err) {
if (err instanceof Error && err.message.includes("overloaded")) {
$error = "Too much traffic, please try again.";
Expand Down
7 changes: 7 additions & 0 deletions src/routes/conversation/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
import { z } from "zod";
import {
MessageReasoningUpdateType,
MessageUpdateStatus,
MessageUpdateType,
type MessageUpdate,
Expand Down Expand Up @@ -355,6 +356,12 @@ export async function POST({ request, locals, params, getClientAddress }) {
Date.now() - (lastTokenTimestamp ?? promptedAt).getTime()
);
lastTokenTimestamp = new Date();
} else if (
event.type === MessageUpdateType.Reasoning &&
event.subtype === MessageReasoningUpdateType.Stream
) {
messageToWriteTo.reasoning ??= "";
messageToWriteTo.reasoning += event.token;
}

// Set the title
Expand Down

0 comments on commit e62d992

Please sign in to comment.