Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { JsonSchema7Type } from 'zod-to-json-schema'
export type ScraperCompletionResult<T extends z.ZodSchema<any>> = {
data: z.infer<T> | null
url: string
error: any
}

const defaultPrompt =
Expand Down Expand Up @@ -66,11 +67,20 @@ export async function generateOpenAICompletions<T extends z.ZodSchema<any>>(
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,
}
}

Expand All @@ -85,15 +95,24 @@ export async function generateLlamaCompletions<T extends z.ZodSchema<any>>(
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,
}
}