mirror of
https://github.com/baptisteArno/typebot.io.git
synced 2026-06-19 21:04:33 +08:00
🚸 Improve error handling in AI generation functions
This commit is contained in:
parent
c33ba97b36
commit
a634bb3076
@ -51,14 +51,6 @@ export const runChatCompletion = async ({
|
||||
variables.set([{ id: mapping.variableId, value: usage.totalTokens }]);
|
||||
});
|
||||
} catch (err) {
|
||||
if (err instanceof APICallError) {
|
||||
logs.add({
|
||||
status: "error",
|
||||
description: "An API call error occured while generating the response",
|
||||
details: err.message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
logs.add(
|
||||
await parseUnknownError({
|
||||
err,
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import type { LogsStore, VariableStore } from "@typebot.io/forge/types";
|
||||
import { parseUnknownError } from "@typebot.io/lib/parseUnknownError";
|
||||
import { isNotEmpty } from "@typebot.io/lib/utils";
|
||||
import type { Variable } from "@typebot.io/variables/schemas";
|
||||
import { z } from "@typebot.io/zod";
|
||||
@ -37,22 +38,25 @@ export const runGenerateVariables = async ({
|
||||
(variableToExtract) => variableToExtract.isRequired === false,
|
||||
);
|
||||
|
||||
const { object } = await generateObject({
|
||||
model,
|
||||
schema,
|
||||
prompt:
|
||||
`${prompt}\n\nYou should generate a JSON object` +
|
||||
(hasOptionalVariables
|
||||
? " and provide empty values if the information is not there or if you are unsure."
|
||||
: "."),
|
||||
});
|
||||
|
||||
Object.entries(object).forEach(([key, value]) => {
|
||||
if (value === null) return;
|
||||
const existingVariable = variables.find((v) => v.name === key);
|
||||
if (!existingVariable) return;
|
||||
variablesStore.set([{ id: existingVariable.id, value }]);
|
||||
});
|
||||
try {
|
||||
const { object } = await generateObject({
|
||||
model,
|
||||
schema,
|
||||
prompt:
|
||||
`${prompt}\n\nYou should generate a JSON object` +
|
||||
(hasOptionalVariables
|
||||
? " and provide empty values if the information is not there or if you are unsure."
|
||||
: "."),
|
||||
});
|
||||
Object.entries(object).forEach(([key, value]) => {
|
||||
if (value === null) return;
|
||||
const existingVariable = variables.find((v) => v.name === key);
|
||||
if (!existingVariable) return;
|
||||
variablesStore.set([{ id: existingVariable.id, value }]);
|
||||
});
|
||||
} catch (error) {
|
||||
logs.add(await parseUnknownError({ err: error }));
|
||||
}
|
||||
};
|
||||
|
||||
const convertVariablesToExtractToSchema = ({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user