From a634bb3076ca46a1f1ba6772c102cdd7ef189705 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Sat, 15 Feb 2025 11:36:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Improve=20error=20handling=20in?= =?UTF-8?q?=20AI=20generation=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ai/src/runChatCompletion.ts | 8 ------ packages/ai/src/runGenerateVariables.ts | 36 ++++++++++++++----------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/packages/ai/src/runChatCompletion.ts b/packages/ai/src/runChatCompletion.ts index df532ef11..c19d1692c 100644 --- a/packages/ai/src/runChatCompletion.ts +++ b/packages/ai/src/runChatCompletion.ts @@ -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, diff --git a/packages/ai/src/runGenerateVariables.ts b/packages/ai/src/runGenerateVariables.ts index 4ad455f5d..b75ed7178 100644 --- a/packages/ai/src/runGenerateVariables.ts +++ b/packages/ai/src/runGenerateVariables.ts @@ -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 = ({