Skip to content

Commit b806d3a

Browse files
authored
✨ add RAG metadata in v2 results (#398)
1 parent 57e2151 commit b806d3a

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
InferenceResponse,
1515
JobResponse,
1616
RawText,
17+
RagMetadata,
1718
} from "./parsing/v2";
1819
export {
1920
InputSource,

src/parsing/v2/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export { Job } from "./job";
1111
export { JobResponse } from "./jobResponse";
1212
export { RawText } from "./rawText";
1313
export { JobWebhook } from "./jobWebhook";
14+
export { RagMetadata } from "./ragMetadata";

src/parsing/v2/inferenceResult.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { InferenceFields } from "./field";
22
import { StringDict } from "../common";
33
import { RawText } from "./rawText";
4+
import { RagMetadata } from "./ragMetadata";
45

56
export class InferenceResult {
67
/**
@@ -13,11 +14,19 @@ export class InferenceResult {
1314
*/
1415
public rawText?: RawText;
1516

17+
/**
18+
* RAG metadata.
19+
*/
20+
public rag?: RagMetadata;
21+
1622
constructor(serverResponse: StringDict) {
1723
this.fields = new InferenceFields(serverResponse["fields"]);
1824
if (serverResponse["raw_text"]) {
1925
this.rawText = new RawText(serverResponse["raw_text"]);
2026
}
27+
if (serverResponse["rag"]) {
28+
this.rag = new RagMetadata(serverResponse["rag"]);
29+
}
2130
}
2231

2332
toString(): string {

src/parsing/v2/ragMetadata.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { StringDict } from "../common";
2+
3+
export class RagMetadata {
4+
/**
5+
* The UUID of the matched document used during the RAG operation.
6+
*/
7+
retrievedDocumentId?: string;
8+
9+
constructor(serverResponse: StringDict) {
10+
this.retrievedDocumentId = serverResponse["retrieved_document_id"] ?? undefined;
11+
}
12+
}

tests/v2/parsing/inference.spec.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "chai";
22
import path from "node:path";
3-
import { LocalResponse, InferenceResponse, RawText } from "../../../src";
3+
import { LocalResponse, InferenceResponse, RawText, RagMetadata } from "../../../src";
44
import { FieldConfidence, ListField, ObjectField, SimpleField } from "../../../src/parsing/v2/field";
55
import { promises as fs } from "node:fs";
66
import { Polygon } from "../../../src/geometry";
@@ -12,20 +12,19 @@ const deepNestedFieldPath = path.join(inferencePath, "deep_nested_fields.json");
1212
const standardFieldPath = path.join(inferencePath, "standard_field_types.json");
1313
const standardFieldRstPath = path.join(inferencePath, "standard_field_types.rst");
1414
const locationFieldPath = path.join(findocPath, "complete_with_coordinates.json");
15-
const rawTextPath = path.join(inferencePath, "raw_texts.json");
16-
const blankPath = path.join(findocPath, "blank.json");
17-
const completePath = path.join(findocPath, "complete.json");
1815

1916
async function loadV2Inference(resourcePath: string): Promise<InferenceResponse> {
2017
const localResponse = new LocalResponse(resourcePath);
2118
await localResponse.init();
2219
return localResponse.deserializeResponse(InferenceResponse);
2320
}
2421

25-
describe("inference", async () => {
26-
describe("simple", async () => {
22+
describe("MindeeV2 - Inference Response", async () => {
23+
describe("Financial Document", async () => {
2724
it("should load a blank inference with valid properties", async () => {
28-
const response = await loadV2Inference(blankPath);
25+
const response = await loadV2Inference(
26+
path.join(findocPath, "blank.json")
27+
);
2928
const fields = response.inference.result.fields;
3029

3130
expect(fields).to.be.not.empty;
@@ -55,7 +54,9 @@ describe("inference", async () => {
5554
});
5655

5756
it("should load a complete inference with valid properties", async () => {
58-
const response = await loadV2Inference(completePath);
57+
const response = await loadV2Inference(
58+
path.join(findocPath, "complete.json")
59+
);
5960
const inference = response.inference;
6061

6162
expect(inference).to.not.be.undefined;
@@ -116,7 +117,7 @@ describe("inference", async () => {
116117
});
117118
});
118119

119-
describe("nested", async () => {
120+
describe("Deeply Nested", async () => {
120121
it("should load a deep nested object", async () => {
121122
const response = await loadV2Inference(deepNestedFieldPath);
122123
const fields = response.inference.result.fields;
@@ -152,7 +153,7 @@ describe("inference", async () => {
152153
});
153154
});
154155

155-
describe("standard field types", async () => {
156+
describe("Standard Field Types", async () => {
156157
it("should recognize simple fields", async () => {
157158
const response = await loadV2Inference(standardFieldPath);
158159
const fields = response.inference.result.fields;
@@ -259,11 +260,14 @@ describe("inference", async () => {
259260
});
260261
});
261262

262-
describe("raw text", async () => {
263+
describe("Raw Text", async () => {
263264
it("raw text should be exposed", async () => {
264-
const response = await loadV2Inference(rawTextPath);
265-
const rawText = response.inference.result.rawText;
265+
const response = await loadV2Inference(
266+
path.join(inferencePath, "raw_texts.json")
267+
);
268+
expect(response.inference.result.rag).to.be.undefined;
266269

270+
const rawText = response.inference.result.rawText;
267271
expect(rawText).to.be.instanceOf(RawText);
268272

269273
const pages = rawText?.pages;
@@ -275,7 +279,18 @@ describe("inference", async () => {
275279
});
276280
});
277281

278-
describe("rst display", async () => {
282+
describe("RAG Metadata", async () => {
283+
it("RAG metadata should be exposed", async () => {
284+
const response = await loadV2Inference(
285+
path.join(inferencePath, "rag_matched.json")
286+
);
287+
const rag = response.inference.result.rag;
288+
expect(rag).to.be.instanceOf(RagMetadata);
289+
expect(rag?.retrievedDocumentId).to.eq("12345abc-1234-1234-1234-123456789abc");
290+
});
291+
});
292+
293+
describe("RST Display", async () => {
279294
it("to be properly exposed", async () => {
280295
const response = await loadV2Inference(standardFieldPath);
281296
const rstString = await fs.readFile(standardFieldRstPath, "utf8");
@@ -285,7 +300,7 @@ describe("inference", async () => {
285300
}).timeout(10000);
286301
});
287302

288-
describe("field locations and confidence", async () => {
303+
describe("Field Locations and Confidence", async () => {
289304
it("to be properly exposed", async () => {
290305
const response = await loadV2Inference(locationFieldPath);
291306

tests/v2/parsing/job.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function loadV2Job(resourcePath: string): Promise<JobResponse> {
1212
return localResponse.deserializeResponse(JobResponse);
1313
}
1414

15-
describe("job", async () => {
15+
describe("MindeeV2 - Job Response", async () => {
1616
describe("OK", async () => {
1717
it("should load when status is Processing", async () => {
1818
const response = await loadV2Job(

0 commit comments

Comments
 (0)