Skip to content
Snippets Groups Projects
Unverified Commit 17724d96 authored by Diego Ferreiro Val's avatar Diego Ferreiro Val Committed by GitHub
Browse files

feat: add support for buffer on loadJSON API on LlamaParseReader (#1069)


Co-authored-by: default avatarAlex Yang <himself65@outlook.com>
parent 6776910c
No related branches found
No related tags found
No related merge requests found
...@@ -330,13 +330,18 @@ export class LlamaParseReader extends FileReader { ...@@ -330,13 +330,18 @@ export class LlamaParseReader extends FileReader {
* Loads data from a file and returns an array of JSON objects. * Loads data from a file and returns an array of JSON objects.
* To be used with resultType = "json" * To be used with resultType = "json"
* *
* @param {string} file - The path to the file to be loaded. * @param {string} filePathOrContent - The file path to the file or the content of the file as a Buffer
* @return {Promise<Record<string, any>[]>} A Promise that resolves to an array of JSON objects. * @return {Promise<Record<string, any>[]>} A Promise that resolves to an array of JSON objects.
*/ */
async loadJson(file: string): Promise<Record<string, any>[]> { async loadJson(
filePathOrContent: string | Uint8Array,
): Promise<Record<string, any>[]> {
let jobId; let jobId;
const isFilePath = typeof filePathOrContent === "string";
try { try {
const data = await fs.readFile(file); const data = isFilePath
? await fs.readFile(filePathOrContent)
: filePathOrContent;
// Creates a job for the file // Creates a job for the file
jobId = await this.createJob(data); jobId = await this.createJob(data);
if (this.verbose) { if (this.verbose) {
...@@ -346,7 +351,7 @@ export class LlamaParseReader extends FileReader { ...@@ -346,7 +351,7 @@ export class LlamaParseReader extends FileReader {
// Return results as an array of JSON objects (same format as Python version of the reader) // Return results as an array of JSON objects (same format as Python version of the reader)
const resultJson = await this.getJobResult(jobId, "json"); const resultJson = await this.getJobResult(jobId, "json");
resultJson.job_id = jobId; resultJson.job_id = jobId;
resultJson.file_path = file; resultJson.file_path = isFilePath ? filePathOrContent : undefined;
return [resultJson]; return [resultJson];
} catch (e) { } catch (e) {
console.error(`Error while parsing the file under job id ${jobId}`, e); console.error(`Error while parsing the file under job id ${jobId}`, e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment