forked from maxdotio/node-qdrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
79 lines (68 loc) · 2.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { body_request, url_request } from "./request.js";
const base_url = "http://localhost:6333/";
class QdrantResponse {
err: any
response: any
constructor(response) {
this.err = response[0];
this.response = response[1];
}
}
export class Qdrant {
url: string
api_key?: string
constructor({ url, api_key }: {url?: string, api_key?: string}) {
this.url = url || base_url;
if(!this.url.endsWith("/")) {
this.url += "/";
}
if(!this.url.startsWith("http")) {
this.url = "https://" + this.url;
}
this.api_key = api_key;
}
async req(path, body, method) {
let qdrant_url = this.url;
let url = `${qdrant_url}${path}`;
return new QdrantResponse(await body_request(url, body, method, this.api_key));
}
//DELETE http://localhost:6333/collections/{collection_name}
async delete_collection(name: string) {
return this.req(`collections/${name}`, null, 'DELETE');
}
//PUT http://localhost:6333/collections/{collection_name}
async create_collection(name: string, body: any) {
return this.req(`collections/${name}`, body, 'PUT');
}
//GET http://localhost:6333/collections/{collection_name}
async get_collection(name: string) {
return this.req(`collections/${name}`, null, 'GET');
}
//PUT http://localhost:6333/collections/{collection_name}/points
async upload_points(name: string, points: any) {
return this.req(`collections/${name}/points`, { points: points }, 'PUT');
}
//POST http://localhost:6333/collections/{collection_name}/points/search
async search_collection(name: string, vector: any, k: number, ef: number, filter: any) {
k = k || 5;
ef = ef || 128;
let query: any = {
"params": {
"hnsw_ef": ef
},
"vector": vector,
"top": k
};
if (filter) query.filter = filter;
return this.req(`collections/${name}/points/search`, query, 'POST');
}
//Same as search_collection but allows free-form query by the client
async query_collection(name: string, query: any) {
return this.req(`collections/${name}/points/search`, query, 'POST');
}
//retrieve the specific points by ids
// ids as numbers don't work. you can't express the full i64 range in JSON
async retrieve_points(name: string, ids: string[], with_payload?: boolean, with_vector?: boolean) {
return this.req(`collections/${name}/points`, { ids, with_payload, with_vector }, 'POST');
}
};