From b9b4d08d93f6784c5accaebbb580f59e7679e877 Mon Sep 17 00:00:00 2001 From: Ademsk1 Date: Wed, 1 May 2024 15:39:42 +0100 Subject: [PATCH 1/2] fix: add try/catch --- src/models.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/models.ts b/src/models.ts index 0dc5df8..1e96c64 100644 --- a/src/models.ts +++ b/src/models.ts @@ -9,10 +9,12 @@ import { GbnfJsonSchema, } from 'node-llama-cpp' import { JsonSchema7Type } from 'zod-to-json-schema' +import { error } from 'console' export type ScraperCompletionResult> = { data: z.infer | null url: string + error: any } const defaultPrompt = @@ -66,11 +68,20 @@ export async function generateOpenAICompletions>( tool_choice: 'auto', temperature, }) + let c: any + let errorMessage: any + try { + c = JSON.parse(completion.choices[0].message.tool_calls[0].function.arguments) + } catch (e) { + console.error('Error parsing OpenAI completion', e) + e ? errorMessage = JSON.stringify(e) : errorMessage = "Something went wrong" + c = completion.choices[0].message.content + } - const c = completion.choices[0].message.tool_calls[0].function.arguments return { - data: JSON.parse(c), + data: c, url: page.url, + error: errorMessage, } } @@ -85,15 +96,24 @@ export async function generateLlamaCompletions>( const context = new LlamaContext({ model }) const session = new LlamaChatSession({ context }) const pagePrompt = `${prompt}\n${page.content}` + let result: string + let errorMessage: any + try { + result = await session.prompt(pagePrompt, { + grammar, + temperature, + }) + } catch (e) { + console.error('Error generating Llama completions', e) + e ? errorMessage = JSON.stringify(e) : errorMessage = "Something went wrong" + result = "Error" + } - const result = await session.prompt(pagePrompt, { - grammar, - temperature, - }) const parsed = grammar.parse(result) return { data: parsed, url: page.url, + error: errorMessage, } } From b9be3c36d9d1ff0826b52c2345a8b2c0fd27c67a Mon Sep 17 00:00:00 2001 From: Ademsk1 Date: Wed, 1 May 2024 15:40:53 +0100 Subject: [PATCH 2/2] fix: remove unused import --- src/models.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/models.ts b/src/models.ts index 1e96c64..86fe1b8 100644 --- a/src/models.ts +++ b/src/models.ts @@ -9,7 +9,6 @@ import { GbnfJsonSchema, } from 'node-llama-cpp' import { JsonSchema7Type } from 'zod-to-json-schema' -import { error } from 'console' export type ScraperCompletionResult> = { data: z.infer | null