Skip to content

Commit 222fdda

Browse files
committed
added delete & fixed tests
1 parent da20c1c commit 222fdda

File tree

6 files changed

+68
-38
lines changed

6 files changed

+68
-38
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@ We are using Bun's own [bundler](https://bun.sh/docs/bundler).
154154
155155
## Testing
156156
157-
You can run tests via:
157+
For the tests, you will need an API key at `DRIA_API_KEY` environment variable, which you can provide in an `.env.test` file. You can run tests via:
158158
159159
```sh
160160
bun run test
161161
bun t # alias
162162
```
163163
164-
You will need an API key at `DRIA_API_KEY` environment variable, which you can provide in an `.env.test` file.
164+
You can also specify the test titles (as they appear in `describe`, `it` or `test`).
165+
166+
```sh
167+
bun t -t "test-name"
168+
```

src/constants/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export default {
22
/** URL to make fetch / query / search requests */
3-
DRIA_BASE_URL: "https://search.dria.co/hnsw",
3+
DRIA_SEARCH_URL: "https://search.dria.co/hnsw",
44
/** URL to insert texts and vectors */
5-
DRIA_INSERT_URL: "https://aws-eu-north-1.hollowdb.xyz/hnswt",
6-
/** URL to get model of a contract, or create a contract */
7-
DRIA_CONTRACT_URL: "https://test.dria.co/v1/knowledge/index",
5+
DRIA_INSERT_URL: "https://search.dria.co/hnswt",
6+
/** URL to get model */
7+
DRIA_API_URL: "https://api.dria.co",
88
} as const;

src/dria.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { CategoryTypes, DriaParams, ModelTypes } from "./types";
66
import constants from "./constants";
77

88
/**
9-
* ## Dria JS Client
9+
* Dria JS Client
1010
*
11-
* @param params optional API key and contract txID.
11+
* @param params optional API key and contract ID.
1212
*
1313
* - `apiKey`: User API key.
1414
*
@@ -78,13 +78,15 @@ export class Dria<T extends MetadataType = any> {
7878
*/
7979
async search(text: string, options: SearchOptions = {}) {
8080
options = SearchOptions.parse(options);
81-
return await this.post<{ id: number; metadata: string; score: number }[]>(constants.DRIA_BASE_URL + "/search", {
81+
const contractId = this.getContractId();
82+
return await this.post<{ id: number; metadata: string; score: number }[]>(constants.DRIA_SEARCH_URL + "/search", {
8283
query: text,
8384
top_n: options.topK,
8485
level: options.level,
8586
rerank: options.rerank,
8687
field: options.field,
87-
contract_id: this.getContractId(),
88+
contract_id: contractId,
89+
model: await this.getModel(contractId),
8890
});
8991
}
9092

@@ -101,7 +103,7 @@ export class Dria<T extends MetadataType = any> {
101103
async query<M extends MetadataType = T>(vector: number[], options: QueryOptions = {}) {
102104
options = QueryOptions.parse(options);
103105
const data = await this.post<{ id: number; metadata: string; score: number }[]>(
104-
constants.DRIA_BASE_URL + "/query",
106+
constants.DRIA_SEARCH_URL + "/query",
105107
{ vector, contract_id: this.getContractId(), top_n: options.topK },
106108
);
107109
return data.map((d) => ({ ...d, metadata: JSON.parse(d.metadata) as M }));
@@ -117,7 +119,7 @@ export class Dria<T extends MetadataType = any> {
117119
*/
118120
async fetch<M extends MetadataType = T>(ids: number[]) {
119121
if (ids.length === 0) throw "No IDs provided.";
120-
const data = await this.post<{ metadata: string[]; vectors: number[][] }>(constants.DRIA_BASE_URL + "/fetch", {
122+
const data = await this.post<{ metadata: string[]; vectors: number[][] }>(constants.DRIA_SEARCH_URL + "/fetch", {
121123
id: ids,
122124
contract_id: this.getContractId(),
123125
});
@@ -128,7 +130,7 @@ export class Dria<T extends MetadataType = any> {
128130
}
129131

130132
/**
131-
* Insert a batch of vectors to an existing knowledge.
133+
* Insert a batch of vectors to your existing knowledge.
132134
* @param items batch of vectors with optional metadatas
133135
* @returns a string indicating success
134136
* @example
@@ -139,20 +141,21 @@ export class Dria<T extends MetadataType = any> {
139141
* ]
140142
* await dria.insertVectors(batch);
141143
*/
142-
async insertVectors(items: BatchVectors) {
143-
items = BatchVectors.parse(items);
144+
async insertVectors<M extends MetadataType = T>(items: BatchVectors<M>) {
145+
items = BatchVectors.parse(items) as BatchVectors<M>;
144146
const encodedData = encodeBatchVectors(items);
147+
const contractId = this.getContractId();
145148
const data = await this.post<string>(constants.DRIA_INSERT_URL + "/insert_vector", {
146149
data: encodedData,
147-
model: await this.getModel(this.getContractId()),
148150
batch_size: items.length,
149-
contract_id: this.getContractId(),
151+
model: await this.getModel(contractId),
152+
contract_id: contractId,
150153
});
151154
return data;
152155
}
153156

154157
/**
155-
* Insert a batch of texts to an existing knowledge.
158+
* Insert a batch of texts to your existing knowledge.
156159
* @param items batch of texts with optional metadatas
157160
* @returns a string indicating success
158161
* @example
@@ -163,14 +166,15 @@ export class Dria<T extends MetadataType = any> {
163166
* ]
164167
* await dria.insertTexts(batch);
165168
*/
166-
async insertTexts(items: BatchTexts) {
167-
items = BatchTexts.parse(items);
169+
async insertTexts<M extends MetadataType = T>(items: BatchTexts<M>) {
170+
items = BatchTexts.parse(items) as BatchTexts<M>;
168171
const encodedData = encodeBatchTexts(items);
172+
const contractId = this.getContractId();
169173
const data = await this.post<string>(constants.DRIA_INSERT_URL + "/insert_text", {
170174
data: encodedData,
171-
model: await this.getModel(this.getContractId()),
172-
contract_id: this.getContractId(),
173175
batch_size: items.length,
176+
model: await this.getModel(contractId),
177+
contract_id: contractId,
174178
});
175179
return data;
176180
}
@@ -180,7 +184,7 @@ export class Dria<T extends MetadataType = any> {
180184
* @param embedding model name, can be any string but we provide some preset models.
181185
* @param category type of the knowledge, can be any string but we provide some preset names.
182186
* @param description (optional) description of the knowledge.
183-
* @returns contract txID of the created contract.
187+
* @returns contract ID of the created contract.
184188
* @example
185189
* const dria = new Dria({apiKey: "your-api-key"});
186190
* const contractId = await dria.create(
@@ -192,7 +196,7 @@ export class Dria<T extends MetadataType = any> {
192196
* // you can now make queries, or insert data there
193197
*/
194198
async create(name: string, embedding: ModelTypes, category: CategoryTypes, description: string = "") {
195-
const data = await this.post<{ contract_id: string }>(constants.DRIA_CONTRACT_URL + "/create", {
199+
const data = await this.post<{ contract_id: string }>(constants.DRIA_API_URL + "/v1/knowledge/index/create", {
196200
name,
197201
embedding,
198202
category,
@@ -201,8 +205,23 @@ export class Dria<T extends MetadataType = any> {
201205
return data.contract_id;
202206
}
203207

208+
/** Delete a knowledge.
209+
* @param contractId contract ID of the knowledge.
210+
* @returns boolean that indicates success
211+
* @example
212+
* const dria = new Dria({apiKey: "your-api-key"});
213+
* await dria.delete("your-contract-to-delete");
214+
*/
215+
async delete(contractId: string) {
216+
// expect message to be `true`
217+
const data = await this.post<{ message: boolean }>(constants.DRIA_API_URL + "/v1/knowledge/remove", {
218+
contract_id: contractId,
219+
});
220+
return data.message;
221+
}
222+
204223
/** Get the embedding model used by a contract.
205-
* @param contractId contract txID
224+
* @param contractId contract ID
206225
* @returns name of the embedding model used by the contract
207226
* @example
208227
* const model = await dria.getModel("contract-id-here");
@@ -212,17 +231,17 @@ export class Dria<T extends MetadataType = any> {
212231
if (contractId in this.models) {
213232
return this.models[contractId];
214233
} else {
215-
const data = await this.get<{ model: { embedding: string } }>(constants.DRIA_CONTRACT_URL + "/get_model", {
234+
const data = await this.get<{ model: string }>(constants.DRIA_API_URL + "/v1/knowledge/index/get_model", {
216235
contract_id: contractId,
217236
});
218237
// memoize the model for later
219-
this.models[contractId] = data.model.embedding;
220-
return data.model.embedding;
238+
this.models[contractId] = data.model;
239+
return data.model;
221240
}
222241
}
223242

224243
/** Safely gets the contract ID.
225-
* @returns currently configured contract txID, guaranteed to be not null
244+
* @returns currently configured contract ID, guaranteed to be not null
226245
*/
227246
private getContractId() {
228247
if (this.contractId) return this.contractId;
@@ -239,6 +258,8 @@ export class Dria<T extends MetadataType = any> {
239258
private async post<T = unknown>(url: string, body: unknown) {
240259
const res = await this.client.post<{ success: boolean; data: T; code: number }>(url, body);
241260
if (res.status !== 200) {
261+
console.log({ url, body });
262+
// console.log(res);
242263
throw `Dria API (POST) failed with ${res.statusText} (${res.status}).\n${res.data}`;
243264
}
244265
return res.data.data;
@@ -254,6 +275,7 @@ export class Dria<T extends MetadataType = any> {
254275
private async get<T = unknown>(url: string, params: Record<string, unknown> = {}) {
255276
const res = await this.client.get<{ success: boolean; data: T; code: number }>(url, { params });
256277
if (res.status !== 200) {
278+
console.log(res.request);
257279
throw `Dria API (GET) failed with ${res.statusText} (${res.status}).\n${res.data}`;
258280
}
259281
return res.data.data;

src/types/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export interface DriaParams {
66

77
/** Model types supported by Dria, although this type allows any string. */
88
export type ModelTypes =
9-
| "jinaai/jina-embeddings-v2-base-en"
10-
| "jinaai/jina-embeddings-v2-small-en"
11-
| "openai/text-embedding-ada-002"
12-
| "openai/text-embedding-3-small"
13-
| "openai/text-embedding-3-large"
9+
| "jina-embeddings-v2-base-en"
10+
| "jina-embeddings-v2-small-en"
11+
| "text-embedding-ada-002"
12+
| "text-embedding-3-small"
13+
| "text-embedding-3-large"
1414
// allow any string while providing auto-complete
1515
| (string & NonNullable<unknown>);
1616

tests/api.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ describe("API", () => {
117117
});
118118

119119
describe("insert texts", () => {
120-
// TODO: waiting for API fix on this
121-
it.todo("should insert texts", async () => {
120+
it("should insert texts", async () => {
122121
const res = await dria.insertTexts([
123122
{ text: "I am an inserted text.", metadata: { id: 112233, info: "Test_1" } },
124123
{ text: "I am another inserted text.", metadata: { id: 223344, info: "Test_2" } },

tests/create.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { randomVector } from "./utils";
33
import { BatchVectors } from "../src/schemas";
44
import { Dria } from "../src";
55

6-
describe.todo("create knowledge", () => {
6+
describe("create & delete knowledge", () => {
77
type MetadataType = { id: number };
88
const dria = new Dria<MetadataType>();
99
const batchVectors: BatchVectors<MetadataType> = Array.from({ length: 50 }, (_, i) => ({
@@ -17,7 +17,7 @@ describe.todo("create knowledge", () => {
1717
it("should create a new index", async () => {
1818
contractId = await dria.create(
1919
"testContract" + Math.round(Math.random() * 1000),
20-
"jinaai/jina-embeddings-v2-base-en",
20+
"jina-embeddings-v2-base-en",
2121
"Test",
2222
);
2323
expect(contractId).toBeString();
@@ -35,4 +35,9 @@ describe.todo("create knowledge", () => {
3535
const vec0_own = batchVectors[0];
3636
expect(vec0_res.metadata.id).toBe(vec0_own.metadata.id);
3737
});
38+
39+
it("should delete the index", async () => {
40+
const res = await dria.delete(contractId);
41+
expect(res).toBeTrue();
42+
});
3843
});

0 commit comments

Comments
 (0)