Skip to content

Commit

Permalink
fix: catch errors more gracefully in reasoning specific calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin committed Dec 16, 2024
1 parent 6623d11 commit 71aae58
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
56 changes: 33 additions & 23 deletions src/lib/server/textGeneration/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { TextGenerationContext } from "./types";
import type { EndpointMessage } from "../endpoints/endpoints";
import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";
import { generateSummaryOfReasoning } from "./reasoning";
import { logger } from "../logger";

type GenerateContext = Omit<TextGenerationContext, "messages"> & { messages: EndpointMessage[] };

Expand Down Expand Up @@ -69,30 +70,35 @@ export async function* generate(
subtype: MessageReasoningUpdateType.Status,
status: "Summarizing reasoning...",
};
const summary = yield* generateFromDefaultEndpoint({
messages: [
{
from: "user",
content: `Question: ${
messages[messages.length - 1].content
}\n\nReasoning: ${reasoningBuffer}`,
},
],
preprompt: `Your task is to summarize concisely all your reasoning steps and then give the final answer. Keep it short, one short paragraph at most. If the reasoning steps explicitly include a code solution, make sure to include it in your answer.
try {
const summary = yield* generateFromDefaultEndpoint({
messages: [
{
from: "user",
content: `Question: ${
messages[messages.length - 1].content
}\n\nReasoning: ${reasoningBuffer}`,
},
],
preprompt: `Your task is to summarize concisely all your reasoning steps and then give the final answer. Keep it short, one short paragraph at most. If the reasoning steps explicitly include a code solution, make sure to include it in your answer.
If the user is just having a casual conversation that doesn't require explanations, answer directly without explaining your steps, otherwise make sure to summarize step by step, make sure to skip dead-ends in your reasoning and removing excess detail.
Do not use prefixes such as Response: or Answer: when answering to the user.`,
generateSettings: {
max_new_tokens: 1024,
},
});
finalAnswer = summary;
yield {
type: MessageUpdateType.Reasoning,
subtype: MessageReasoningUpdateType.Status,
status: `Done in ${Math.round((new Date().getTime() - startTime.getTime()) / 1000)}s.`,
};
generateSettings: {
max_new_tokens: 1024,
},
});
finalAnswer = summary;
yield {
type: MessageUpdateType.Reasoning,
subtype: MessageReasoningUpdateType.Status,
status: `Done in ${Math.round((new Date().getTime() - startTime.getTime()) / 1000)}s.`,
};
} catch (e) {
finalAnswer = text;
logger.error(e);
}
}

yield {
Expand Down Expand Up @@ -143,9 +149,13 @@ Do not use prefixes such as Response: or Answer: when answering to the user.`,
// create a new status every 5 seconds
if (new Date().getTime() - lastReasoningUpdate.getTime() > 4000) {
lastReasoningUpdate = new Date();
generateSummaryOfReasoning(reasoningBuffer).then((summary) => {
status = summary;
});
try {
generateSummaryOfReasoning(reasoningBuffer).then((summary) => {
status = summary;
});
} catch (e) {
logger.error(e);
}
}
yield {
type: MessageUpdateType.Reasoning,
Expand Down
14 changes: 10 additions & 4 deletions src/lib/server/textGeneration/reasoning.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";

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

export async function generateSummaryOfReasoning(buffer: string): Promise<string> {
// debug 5s delay
Expand All @@ -21,10 +22,15 @@ export async function generateSummaryOfReasoning(buffer: string): Promise<string
max_new_tokens: 50,
},
})
).then((summary) => {
const parts = summary.split("...");
return parts[0] + "...";
});
)
.then((summary) => {
const parts = summary.split("...");
return parts[0] + "...";
})
.catch((e) => {
logger.error(e);
return "Reasoning...";
});

return summary;
}

0 comments on commit 71aae58

Please sign in to comment.