@@ -6,9 +6,9 @@ import { CategoryTypes, DriaParams, ModelTypes } from "./types";
6
6
import constants from "./constants" ;
7
7
8
8
/**
9
- * ## Dria JS Client
9
+ * Dria JS Client
10
10
*
11
- * @param params optional API key and contract txID .
11
+ * @param params optional API key and contract ID .
12
12
*
13
13
* - `apiKey`: User API key.
14
14
*
@@ -78,13 +78,15 @@ export class Dria<T extends MetadataType = any> {
78
78
*/
79
79
async search ( text : string , options : SearchOptions = { } ) {
80
80
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" , {
82
83
query : text ,
83
84
top_n : options . topK ,
84
85
level : options . level ,
85
86
rerank : options . rerank ,
86
87
field : options . field ,
87
- contract_id : this . getContractId ( ) ,
88
+ contract_id : contractId ,
89
+ model : await this . getModel ( contractId ) ,
88
90
} ) ;
89
91
}
90
92
@@ -101,7 +103,7 @@ export class Dria<T extends MetadataType = any> {
101
103
async query < M extends MetadataType = T > ( vector : number [ ] , options : QueryOptions = { } ) {
102
104
options = QueryOptions . parse ( options ) ;
103
105
const data = await this . post < { id : number ; metadata : string ; score : number } [ ] > (
104
- constants . DRIA_BASE_URL + "/query" ,
106
+ constants . DRIA_SEARCH_URL + "/query" ,
105
107
{ vector, contract_id : this . getContractId ( ) , top_n : options . topK } ,
106
108
) ;
107
109
return data . map ( ( d ) => ( { ...d , metadata : JSON . parse ( d . metadata ) as M } ) ) ;
@@ -117,7 +119,7 @@ export class Dria<T extends MetadataType = any> {
117
119
*/
118
120
async fetch < M extends MetadataType = T > ( ids : number [ ] ) {
119
121
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" , {
121
123
id : ids ,
122
124
contract_id : this . getContractId ( ) ,
123
125
} ) ;
@@ -128,7 +130,7 @@ export class Dria<T extends MetadataType = any> {
128
130
}
129
131
130
132
/**
131
- * Insert a batch of vectors to an existing knowledge.
133
+ * Insert a batch of vectors to your existing knowledge.
132
134
* @param items batch of vectors with optional metadatas
133
135
* @returns a string indicating success
134
136
* @example
@@ -139,20 +141,21 @@ export class Dria<T extends MetadataType = any> {
139
141
* ]
140
142
* await dria.insertVectors(batch);
141
143
*/
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 > ;
144
146
const encodedData = encodeBatchVectors ( items ) ;
147
+ const contractId = this . getContractId ( ) ;
145
148
const data = await this . post < string > ( constants . DRIA_INSERT_URL + "/insert_vector" , {
146
149
data : encodedData ,
147
- model : await this . getModel ( this . getContractId ( ) ) ,
148
150
batch_size : items . length ,
149
- contract_id : this . getContractId ( ) ,
151
+ model : await this . getModel ( contractId ) ,
152
+ contract_id : contractId ,
150
153
} ) ;
151
154
return data ;
152
155
}
153
156
154
157
/**
155
- * Insert a batch of texts to an existing knowledge.
158
+ * Insert a batch of texts to your existing knowledge.
156
159
* @param items batch of texts with optional metadatas
157
160
* @returns a string indicating success
158
161
* @example
@@ -163,14 +166,15 @@ export class Dria<T extends MetadataType = any> {
163
166
* ]
164
167
* await dria.insertTexts(batch);
165
168
*/
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 > ;
168
171
const encodedData = encodeBatchTexts ( items ) ;
172
+ const contractId = this . getContractId ( ) ;
169
173
const data = await this . post < string > ( constants . DRIA_INSERT_URL + "/insert_text" , {
170
174
data : encodedData ,
171
- model : await this . getModel ( this . getContractId ( ) ) ,
172
- contract_id : this . getContractId ( ) ,
173
175
batch_size : items . length ,
176
+ model : await this . getModel ( contractId ) ,
177
+ contract_id : contractId ,
174
178
} ) ;
175
179
return data ;
176
180
}
@@ -180,7 +184,7 @@ export class Dria<T extends MetadataType = any> {
180
184
* @param embedding model name, can be any string but we provide some preset models.
181
185
* @param category type of the knowledge, can be any string but we provide some preset names.
182
186
* @param description (optional) description of the knowledge.
183
- * @returns contract txID of the created contract.
187
+ * @returns contract ID of the created contract.
184
188
* @example
185
189
* const dria = new Dria({apiKey: "your-api-key"});
186
190
* const contractId = await dria.create(
@@ -192,7 +196,7 @@ export class Dria<T extends MetadataType = any> {
192
196
* // you can now make queries, or insert data there
193
197
*/
194
198
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" , {
196
200
name,
197
201
embedding,
198
202
category,
@@ -201,8 +205,23 @@ export class Dria<T extends MetadataType = any> {
201
205
return data . contract_id ;
202
206
}
203
207
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
+
204
223
/** Get the embedding model used by a contract.
205
- * @param contractId contract txID
224
+ * @param contractId contract ID
206
225
* @returns name of the embedding model used by the contract
207
226
* @example
208
227
* const model = await dria.getModel("contract-id-here");
@@ -212,17 +231,17 @@ export class Dria<T extends MetadataType = any> {
212
231
if ( contractId in this . models ) {
213
232
return this . models [ contractId ] ;
214
233
} 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" , {
216
235
contract_id : contractId ,
217
236
} ) ;
218
237
// 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 ;
221
240
}
222
241
}
223
242
224
243
/** 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
226
245
*/
227
246
private getContractId ( ) {
228
247
if ( this . contractId ) return this . contractId ;
@@ -239,6 +258,8 @@ export class Dria<T extends MetadataType = any> {
239
258
private async post < T = unknown > ( url : string , body : unknown ) {
240
259
const res = await this . client . post < { success : boolean ; data : T ; code : number } > ( url , body ) ;
241
260
if ( res . status !== 200 ) {
261
+ console . log ( { url, body } ) ;
262
+ // console.log(res);
242
263
throw `Dria API (POST) failed with ${ res . statusText } (${ res . status } ).\n${ res . data } ` ;
243
264
}
244
265
return res . data . data ;
@@ -254,6 +275,7 @@ export class Dria<T extends MetadataType = any> {
254
275
private async get < T = unknown > ( url : string , params : Record < string , unknown > = { } ) {
255
276
const res = await this . client . get < { success : boolean ; data : T ; code : number } > ( url , { params } ) ;
256
277
if ( res . status !== 200 ) {
278
+ console . log ( res . request ) ;
257
279
throw `Dria API (GET) failed with ${ res . statusText } (${ res . status } ).\n${ res . data } ` ;
258
280
}
259
281
return res . data . data ;
0 commit comments