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

fixed optional+default bug on llm schema #955

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion apps/api/src/scraper/scrapeURL/transformers/llmExtract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export async function generateOpenAICompletions(logger: Logger, options: Extract
}

let schema = options.schema;
if (schema) {
schema = removeDefaultProperty(schema);
}

if (schema && schema.type === "array") {
schema = {
type: "object",
Expand All @@ -112,7 +116,9 @@ export async function generateOpenAICompletions(logger: Logger, options: Extract
schema = {
type: "object",
properties: Object.fromEntries(
Object.entries(schema).map(([key, value]) => [key, { type: value }])
Object.entries(schema).map(([key, value]) => {
return [key, removeDefaultProperty(value)];
})
),
required: Object.keys(schema),
additionalProperties: false
Expand Down Expand Up @@ -192,3 +198,19 @@ export async function performLLMExtract(meta: Meta, document: Document): Promise

return document;
}

function removeDefaultProperty(schema: any): any {
if (typeof schema !== 'object' || schema === null) return schema;

const { default: _, ...rest } = schema;

for (const key in rest) {
if (Array.isArray(rest[key])) {
rest[key] = rest[key].map((item: any) => removeDefaultProperty(item));
} else if (typeof rest[key] === 'object' && rest[key] !== null) {
rest[key] = removeDefaultProperty(rest[key]);
}
}

return rest;
}
Loading