forked from i-am-alice/3rd-devs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorService.ts
115 lines (101 loc) · 2.67 KB
/
VectorService.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { QdrantClient } from "@qdrant/js-client-rest";
import { v4 as uuidv4 } from "uuid";
import { OpenAIService } from "./OpenAIService";
export class VectorService {
private client: QdrantClient;
private openAIService: OpenAIService;
constructor(openAIService: OpenAIService) {
this.client = new QdrantClient({
url: process.env.QDRANT_URL!,
apiKey: process.env.QDRANT_API_KEY!,
});
this.openAIService = openAIService;
}
async ensureCollection(name: string) {
const collections = await this.client.getCollections();
if (!collections.collections.some((c) => c.name === name)) {
await this.client.createCollection(name, {
vectors: { size: 3072, distance: "Cosine" },
});
}
}
async addPoints(
collectionName: string,
points: Array<{
id?: string;
text: string;
metadata?: Record<string, any>;
}>
) {
await this.ensureCollection(collectionName);
const pointsToUpsert = await Promise.all(
points.map(async (point) => {
const embedding = await this.openAIService.createEmbedding(point.text);
return {
id: point.id || uuidv4(),
vector: embedding,
payload: {
text: point.text,
metadata: point.metadata,
},
};
})
);
await this.client.upsert(collectionName, {
wait: true,
points: pointsToUpsert,
});
}
async updatePoint(
collectionName: string,
point: {
id: string;
text: string;
metadata?: Record<string, any>;
}
) {
await this.addPoints(collectionName, [point]);
}
async deletePoint(collectionName: string, id: string) {
await this.client.delete(collectionName, {
wait: true,
points: [id],
});
}
async performSearch(
collectionName: string,
query: string,
filter: Record<string, any> = {},
limit: number = 5
) {
const queryEmbedding = await this.openAIService.createJinaEmbedding(query);
return this.client.search(collectionName, {
vector: queryEmbedding,
limit,
with_payload: true,
filter,
});
}
async getAllPoints(collectionName: string) {
const points: any[] = [];
let hasMore = true;
let offset = undefined;
while (hasMore) {
const response = await this.client.scroll(collectionName, {
limit: 100,
offset,
with_payload: true,
});
points.push(...response.points);
if (
response.next_page_offset !== null &&
response.next_page_offset !== undefined
) {
offset = response.next_page_offset;
} else {
hasMore = false;
}
}
return points;
}
}