fixed optional+default bug on llm schema

This commit is contained in:
rafaelmmiller 2024-12-09 15:34:50 -03:00
parent f007f2439e
commit 5c81ea1803

View File

@ -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",
@ -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
@ -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;
}