Skip to content

Commit 71aae58

Browse files
committed
fix: catch errors more gracefully in reasoning specific calls
1 parent 6623d11 commit 71aae58

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

src/lib/server/textGeneration/generate.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { TextGenerationContext } from "./types";
99
import type { EndpointMessage } from "../endpoints/endpoints";
1010
import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";
1111
import { generateSummaryOfReasoning } from "./reasoning";
12+
import { logger } from "../logger";
1213

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

@@ -69,30 +70,35 @@ export async function* generate(
6970
subtype: MessageReasoningUpdateType.Status,
7071
status: "Summarizing reasoning...",
7172
};
72-
const summary = yield* generateFromDefaultEndpoint({
73-
messages: [
74-
{
75-
from: "user",
76-
content: `Question: ${
77-
messages[messages.length - 1].content
78-
}\n\nReasoning: ${reasoningBuffer}`,
79-
},
80-
],
81-
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.
73+
try {
74+
const summary = yield* generateFromDefaultEndpoint({
75+
messages: [
76+
{
77+
from: "user",
78+
content: `Question: ${
79+
messages[messages.length - 1].content
80+
}\n\nReasoning: ${reasoningBuffer}`,
81+
},
82+
],
83+
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.
8284
8385
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.
8486
8587
Do not use prefixes such as Response: or Answer: when answering to the user.`,
86-
generateSettings: {
87-
max_new_tokens: 1024,
88-
},
89-
});
90-
finalAnswer = summary;
91-
yield {
92-
type: MessageUpdateType.Reasoning,
93-
subtype: MessageReasoningUpdateType.Status,
94-
status: `Done in ${Math.round((new Date().getTime() - startTime.getTime()) / 1000)}s.`,
95-
};
88+
generateSettings: {
89+
max_new_tokens: 1024,
90+
},
91+
});
92+
finalAnswer = summary;
93+
yield {
94+
type: MessageUpdateType.Reasoning,
95+
subtype: MessageReasoningUpdateType.Status,
96+
status: `Done in ${Math.round((new Date().getTime() - startTime.getTime()) / 1000)}s.`,
97+
};
98+
} catch (e) {
99+
finalAnswer = text;
100+
logger.error(e);
101+
}
96102
}
97103

98104
yield {
@@ -143,9 +149,13 @@ Do not use prefixes such as Response: or Answer: when answering to the user.`,
143149
// create a new status every 5 seconds
144150
if (new Date().getTime() - lastReasoningUpdate.getTime() > 4000) {
145151
lastReasoningUpdate = new Date();
146-
generateSummaryOfReasoning(reasoningBuffer).then((summary) => {
147-
status = summary;
148-
});
152+
try {
153+
generateSummaryOfReasoning(reasoningBuffer).then((summary) => {
154+
status = summary;
155+
});
156+
} catch (e) {
157+
logger.error(e);
158+
}
149159
}
150160
yield {
151161
type: MessageUpdateType.Reasoning,

src/lib/server/textGeneration/reasoning.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";
22

33
import { getReturnFromGenerator } from "$lib/utils/getReturnFromGenerator";
4+
import { logger } from "../logger";
45

56
export async function generateSummaryOfReasoning(buffer: string): Promise<string> {
67
// debug 5s delay
@@ -21,10 +22,15 @@ export async function generateSummaryOfReasoning(buffer: string): Promise<string
2122
max_new_tokens: 50,
2223
},
2324
})
24-
).then((summary) => {
25-
const parts = summary.split("...");
26-
return parts[0] + "...";
27-
});
25+
)
26+
.then((summary) => {
27+
const parts = summary.split("...");
28+
return parts[0] + "...";
29+
})
30+
.catch((e) => {
31+
logger.error(e);
32+
return "Reasoning...";
33+
});
2834

2935
return summary;
3036
}

0 commit comments

Comments
 (0)