fixed nested data inside extract

This commit is contained in:
rafaelmmiller 2024-11-27 18:29:37 -03:00
parent 53e0cb6b19
commit 943bbae88d
2 changed files with 10 additions and 3 deletions

View File

@ -211,7 +211,9 @@ export async function extractController(
prompt: req.body.prompt,
schema: req.body.schema,
},
docs.map(x => buildDocument(x)).join('\n')
docs.map(x => buildDocument(x)).join('\n'),
undefined,
true // isExtractEndpoint
);
// TODO: change this later

View File

@ -58,7 +58,7 @@ function normalizeSchema(x: any): any {
}
}
export async function generateOpenAICompletions(logger: Logger, options: ExtractOptions, markdown?: string, previousWarning?: string): Promise<{ extract: any, numTokens: number, warning: string | undefined }> {
export async function generateOpenAICompletions(logger: Logger, options: ExtractOptions, markdown?: string, previousWarning?: string, isExtractEndpoint?: boolean): Promise<{ extract: any, numTokens: number, warning: string | undefined }> {
let extract: any;
let warning: string | undefined;
@ -158,7 +158,12 @@ export async function generateOpenAICompletions(logger: Logger, options: Extract
if (extract === null && jsonCompletion.choices[0].message.content !== null) {
try {
extract = JSON.parse(jsonCompletion.choices[0].message.content);
if (!isExtractEndpoint) {
extract = JSON.parse(jsonCompletion.choices[0].message.content);
} else {
const extractData = JSON.parse(jsonCompletion.choices[0].message.content);
extract = extractData.data.extract;
}
} catch (e) {
logger.error("Failed to parse returned JSON, no schema specified.", { error: e });
throw new LLMRefusalError("Failed to parse returned JSON. Please specify a schema in the extract object.");