Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 46c6725

Browse files
committed
add getExtractionPolicies and update tests
1 parent be6ad96 commit 46c6725

File tree

7 files changed

+123
-52
lines changed

7 files changed

+123
-52
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
# Indexify TypeScript Client
22

3+
[![npm version](https://badge.fury.io/js/getindexify.svg)](https://badge.fury.io/js/getindexify)
4+
35
## Installation
46

57
This is the TypeScript client for interacting with the Indexify service.
68

79
To install it, simply run:
810

911
```shell
10-
npm install indexify
12+
npm install getindexify
13+
```
14+
15+
To initialize a client run the following
16+
```typescript
17+
import { IndexifyClient } from "getindexify";
18+
19+
const client = await IndexifyClient.createClient();
1120
```
1221

1322
## Usage

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "getindexify",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "This is the TypeScript client for interacting with the Indexify service.",
55
"main": "./dist/index.ts",
66
"module": "./dist/index.mjs",

src/client.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import axios, { AxiosInstance, AxiosResponse } from "axios";
2-
import * as fs from "fs";
3-
import FormData from "form-data";
42
import Extractor from "./extractor";
53
import {
64
IContent,
@@ -12,7 +10,7 @@ import {
1210
ITask,
1311
IAddExtractorPolicyResponse,
1412
IDocument,
15-
ISearchDocument,
13+
ISearchIndexResponse,
1614
} from "./types";
1715

1816
const DEFAULT_SERVICE_URL = "http://localhost:8900"; // Set your default service URL
@@ -126,7 +124,7 @@ class IndexifyClient {
126124
name: string,
127125
query: string,
128126
topK: number
129-
): Promise<ISearchDocument[]> {
127+
): Promise<ISearchIndexResponse[]> {
130128
const resp = await this.client.post("search", {
131129
index: name,
132130
query,
@@ -143,8 +141,12 @@ class IndexifyClient {
143141
name: extractionPolicy.name,
144142
input_params: extractionPolicy.input_params,
145143
filters_eq: extractionPolicy.labels_eq,
146-
content_source: extractionPolicy.content_source,
144+
content_source: extractionPolicy.content_source ?? "ingestion",
147145
});
146+
147+
// update this.extractor_bindings
148+
await this.getExtractionPolicies()
149+
148150
return resp.data;
149151
}
150152

@@ -214,14 +216,41 @@ class IndexifyClient {
214216
return resp.data.tasks;
215217
}
216218

217-
async uploadFile(filePath: string): Promise<any> {
218-
const formData = new FormData();
219-
formData.append("file", fs.createReadStream(filePath));
220-
await this.client.post("upload_file", formData, {
221-
headers: {
222-
...formData.getHeaders(),
223-
},
224-
});
219+
async uploadFile(fileInput: string | Blob): Promise<any> {
220+
function isBlob(input: any): input is Blob {
221+
return input instanceof Blob;
222+
}
223+
224+
if (typeof window === "undefined") {
225+
// node
226+
if (typeof fileInput !== "string") {
227+
throw Error("Expected string")
228+
}
229+
const FormData = require("form-data");
230+
const fs = require("fs");
231+
const formData = new FormData();
232+
formData.append("file", fs.createReadStream(fileInput as string));
233+
await this.client.post("upload_file", formData, {
234+
headers: {
235+
...formData.getHeaders(),
236+
},
237+
});
238+
} else {
239+
// browser
240+
if (!isBlob(fileInput)) {
241+
throw Error("Expected blob")
242+
}
243+
const formData = new FormData();
244+
formData.append("file", fileInput);
245+
await this.client.post("/upload_file", formData);
246+
}
247+
}
248+
249+
async getExtractionPolicies(): Promise<IExtractionPolicy[]> {
250+
const resp = await this.client.get("")
251+
const policies = resp.data.namespace?.extraction_policies ?? []
252+
this.extractionPolicies = policies
253+
return policies
225254
}
226255
}
227256

src/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
import IndexifyClient from "./client";
2+
import Extractor from "./extractor";
3+
import {
4+
INamespace,
5+
IEmbeddingSchema,
6+
IExtractorSchema,
7+
IExtractor,
8+
IIndex,
9+
IContentMetadata,
10+
IContent,
11+
IExtractionPolicy,
12+
ITask,
13+
IDocument,
14+
} from "./types";
215

3-
export { IndexifyClient };
16+
export {
17+
IndexifyClient,
18+
Extractor,
19+
INamespace,
20+
IEmbeddingSchema,
21+
IExtractorSchema,
22+
IExtractor,
23+
IIndex,
24+
IContentMetadata,
25+
IContent,
26+
IExtractionPolicy,
27+
ITask,
28+
IDocument,
29+
};

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export interface IExtractionPolicy {
4545
extractor: string;
4646
name: string;
4747
labels_eq?: string;
48-
input_params: Record<string, string | number>;
49-
content_source: string;
48+
input_params?: Record<string, string | number>;
49+
content_source?: string;
5050
}
5151

5252
export interface ITask {
@@ -67,7 +67,7 @@ export interface IDocument {
6767
labels: Record<string, string>;
6868
}
6969

70-
export interface ISearchDocument {
70+
export interface ISearchIndexResponse {
7171
content_id: string;
7272
text: string;
7373
confidence_score: number;

tests/client.test.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ test("Create Client", async () => {
99
});
1010

1111
test("Create Namespace", async () => {
12-
const policy: IExtractionPolicy = {
13-
extractor: "tensorlake/minilm-l6",
14-
name: "testpolicy",
15-
content_source: "ingestion",
16-
input_params: {},
17-
};
18-
const client = await IndexifyClient.createNamespace(
19-
"testnamespace",
20-
[policy],
21-
{}
22-
);
12+
const client = await IndexifyClient.createNamespace("testnamespace", [
13+
{
14+
extractor: "tensorlake/minilm-l6",
15+
name: "testpolicy",
16+
},
17+
]);
18+
2319
expect(client.namespace).toBe("testnamespace");
2420
// test get namespaces
2521
const namespaces = await IndexifyClient.namespaces();
@@ -75,8 +71,6 @@ test("Search", async () => {
7571
const policy: IExtractionPolicy = {
7672
extractor: "tensorlake/minilm-l6",
7773
name: "minilml6",
78-
content_source: "ingestion",
79-
input_params: {},
8074
labels_eq: "source:test",
8175
};
8276

@@ -87,14 +81,14 @@ test("Search", async () => {
8781
const indexName = resp.index_names[0];
8882

8983
await client.addDocuments([
90-
{ text: "This is a test1", labels: {source:"test"} },
91-
{ text: "This is a test2", labels: {source:"test"} },
84+
{ text: "This is a test1", labels: { source: "test" } },
85+
{ text: "This is a test2", labels: { source: "test" } },
9286
]);
9387

9488
await new Promise((r) => setTimeout(r, 10000));
9589

9690
const searchResult = await client.searchIndex(indexName, "test", 3);
97-
expect(searchResult.length).toBe(2)
91+
expect(searchResult.length).toBe(2);
9892
});
9993

10094
test("Upload file", async () => {
@@ -106,26 +100,39 @@ test("Upload file", async () => {
106100
};
107101

108102
const client = await IndexifyClient.createNamespace("testuploadfile");
109-
client.addExtractionPolicy(policy)
110-
await client.uploadFile(`${__dirname}/files/test.txt`)
111-
})
112-
103+
client.addExtractionPolicy(policy);
104+
await client.uploadFile(`${__dirname}/files/test.txt`);
105+
console.log("done");
106+
});
113107

114108
test("Get content", async () => {
115109
const client = await IndexifyClient.createNamespace("testgetcontent");
116110
await client.addDocuments([
117-
{ text: "This is a test1", labels: {source:"test"} },
118-
{ text: "This is a test2", labels: {source:"test"} },
111+
{ text: "This is a test1", labels: { source: "test" } },
112+
{ text: "This is a test2", labels: { source: "test" } },
119113
]);
120-
121-
let content
122114

123-
content = await client.getContent("idontexist")
124-
expect(content.length).toBe(0)
115+
let content;
125116

126-
content = await client.getContent(undefined,"source:test")
127-
expect(content.length).toBe(2)
117+
content = await client.getContent("idontexist");
118+
expect(content.length).toBe(0);
119+
120+
content = await client.getContent(undefined, "source:test");
121+
expect(content.length).toBe(2);
122+
123+
content = await client.getContent(undefined, "source:nothing");
124+
expect(content.length).toBe(0);
125+
});
128126

129-
content = await client.getContent(undefined,"source:nothing")
130-
expect(content.length).toBe(0)
131-
})
127+
test("Get Extraction Policies", async () => {
128+
const client = await IndexifyClient.createNamespace("testgetpolicies");
129+
130+
await client.addExtractionPolicy({
131+
extractor: "tensorlake/minilm-l6",
132+
name: "minilml6",
133+
});
134+
expect(client.extractionPolicies.length).toBe(1);
135+
136+
const policies = await client.getExtractionPolicies();
137+
expect(policies.length).toBe(1);
138+
});

0 commit comments

Comments
 (0)