Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 59dc0c7

Browse files
committed
Fix document endpoints
1 parent f2d60cb commit 59dc0c7

File tree

4 files changed

+119
-19
lines changed

4 files changed

+119
-19
lines changed

__tests__/r2rClientIntegration.test.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { r2rClient } from "../src/index";
2+
import { FilterCriteria, AnalysisTypes } from "../src/models";
3+
const fs = require("fs");
24

35
const baseUrl = "http://localhost:8000";
46

@@ -13,6 +15,33 @@ describe("r2rClient Integration Tests", () => {
1315
await expect(client.healthCheck()).resolves.not.toThrow();
1416
});
1517

18+
test("Ingest documents", async () => {
19+
const documentsToIngest = [
20+
{
21+
type: "txt",
22+
data: fs.readFileSync("examples/data/myshkin.txt", "utf8"),
23+
metadata: { title: "myshkin.txt" },
24+
},
25+
];
26+
await expect(
27+
client.ingestDocuments(documentsToIngest),
28+
).resolves.not.toThrow();
29+
});
30+
31+
test("Update documents", async () => {
32+
const documentsToUpdate = [
33+
{
34+
id: "4430ddbf-4323-5a14-9205-c092920e9321",
35+
type: "txt",
36+
data: fs.readFileSync("examples/data/raskolnikov.txt", "utf8"),
37+
metadata: { title: "updated_myshkin.txt" },
38+
},
39+
];
40+
await expect(
41+
client.updateDocuments(documentsToUpdate),
42+
).resolves.not.toThrow();
43+
});
44+
1645
test("Ingest files", async () => {
1746
const files = [
1847
{ path: "examples/data/raskolnikov.txt", name: "raskolnikov.txt" },
@@ -37,7 +66,7 @@ describe("r2rClient Integration Tests", () => {
3766
await expect(
3867
client.updateFiles(updated_file, {
3968
document_ids: ["48e29904-3010-54fe-abe5-a4f3fba59110"],
40-
metadatas: [{ title: "myshkin.txt" }],
69+
metadatas: [{ title: "updated_karamozov.txt" }],
4170
}),
4271
).resolves.not.toThrow();
4372
});
@@ -64,19 +93,23 @@ describe("r2rClient Integration Tests", () => {
6493
await expect(client.appSettings()).resolves.not.toThrow();
6594
});
6695

67-
// test('Get analytics', async () => {
68-
// const filterCriteria = {
69-
// filters: {
70-
// "search_latencies": "search_latency"
71-
// }
72-
// };
96+
test("Get analytics", async () => {
97+
const filterCriteria: FilterCriteria = {
98+
filters: {
99+
search_latencies: "search_latency",
100+
},
101+
};
73102

74-
// const analysisTypes = {
75-
// "search_latencies": ["basic_statistics", "search_latency"]
76-
// };
103+
const analysisTypes: AnalysisTypes = {
104+
analysis_types: {
105+
search_latencies: ["basic_statistics", "search_latency"],
106+
},
107+
};
77108

78-
// await expect(client.analytics(filterCriteria, analysisTypes)).resolves.not.toThrow();
79-
// });
109+
await expect(
110+
client.analytics(filterCriteria, analysisTypes),
111+
).resolves.not.toThrow();
112+
});
80113

81114
test("Get users overview", async () => {
82115
await expect(client.usersOverview()).resolves.not.toThrow();
@@ -95,8 +128,11 @@ describe("r2rClient Integration Tests", () => {
95128
afterAll(async () => {
96129
// Clean up
97130
await client.delete(
98-
["document_id"],
99-
["48e29904-3010-54fe-abe5-a4f3fba59110"],
131+
["document_id", "document_id"],
132+
[
133+
"48e29904-3010-54fe-abe5-a4f3fba59110",
134+
"4430ddbf-4323-5a14-9205-c092920e9321",
135+
],
100136
);
101137
});
102138
});

src/models.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export interface KGSearchSettings {
3737
}
3838

3939
export interface Document {
40-
// Define the properties of Document
40+
id?: string;
41+
type: string;
42+
data: string;
43+
metadata: Record<string, any>;
4144
}
4245

4346
export interface R2RUpdatePromptRequest {
@@ -53,8 +56,8 @@ export interface R2RIngestDocumentsRequest {
5356

5457
export interface R2RUpdateDocumentsRequest {
5558
documents: Document[];
56-
versions?: string[];
57-
metadatas?: Record<string, any>[];
59+
versions?: string[] | null;
60+
metadatas?: Record<string, any>[] | null;
5861
}
5962

6063
export interface R2RIngestFilesRequest {

src/r2rClient.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if (typeof window === "undefined") {
88

99
import { feature, initializeTelemetry } from "./feature";
1010
import {
11+
Document,
1112
R2RUpdatePromptRequest,
1213
R2RIngestDocumentsRequest,
1314
R2RIngestFilesRequest,
@@ -85,7 +86,22 @@ export class r2rClient {
8586
}
8687

8788
@feature("ingestDocuments")
88-
async ingestDocuments(request: R2RIngestDocumentsRequest): Promise<any> {
89+
async ingestDocuments(
90+
documents: Document[],
91+
versions?: string[],
92+
): Promise<any> {
93+
const processedDocuments = documents.map((doc) => ({
94+
type: doc.type,
95+
data: Buffer.from(doc.data).toString("base64"),
96+
metadata: doc.metadata,
97+
...(doc.id && { id: doc.id }),
98+
}));
99+
100+
const request: R2RIngestDocumentsRequest = {
101+
documents: processedDocuments,
102+
...(versions && { versions }),
103+
};
104+
89105
const response = await this.axiosInstance.post(
90106
"/ingest_documents",
91107
request,
@@ -149,7 +165,31 @@ export class r2rClient {
149165
}
150166

151167
@feature("updateDocuments")
152-
async updateDocuments(request: R2RUpdateDocumentsRequest): Promise<any> {
168+
async updateDocuments(
169+
documents: Document[],
170+
versions?: string[] | null,
171+
metadatas?: Record<string, any>[] | null,
172+
): Promise<any> {
173+
const processedDocuments = documents.map((doc) => ({
174+
id: doc.id,
175+
type: doc.type,
176+
data: Buffer.from(doc.data).toString("base64"),
177+
metadata: doc.metadata,
178+
}));
179+
180+
const request: R2RUpdateDocumentsRequest = {
181+
documents: processedDocuments,
182+
};
183+
184+
// Only include versions and metadatas if they're explicitly provided
185+
if (versions !== undefined && versions !== null) {
186+
request.versions = versions;
187+
}
188+
189+
if (metadatas !== undefined && metadatas !== null) {
190+
request.metadatas = metadatas;
191+
}
192+
153193
const response = await this.axiosInstance.post(
154194
"/update_documents",
155195
request,

testr2r.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { r2rClient } = require("./dist/r2rClient");
2+
const fs = require("fs");
3+
4+
const client = new r2rClient("http://localhost:8000");
5+
6+
async function main() {
7+
const filterCriteria = {
8+
filters: {
9+
search_latencies: "search_latency",
10+
},
11+
};
12+
13+
const analysisTypes = {
14+
search_latencies: ["basic_statistics", "search_latency"],
15+
};
16+
17+
result = await client.analytics(filterCriteria, analysisTypes);
18+
console.log(result);
19+
}
20+
21+
main().catch(console.error);

0 commit comments

Comments
 (0)