Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempted Fix: Sometimes tool_calls is undefined and crashes server #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,
}
}