-
Notifications
You must be signed in to change notification settings - Fork 0
/
Panversal-QDrantDB-client-integration.js
156 lines (136 loc) · 5.73 KB
/
Panversal-QDrantDB-client-integration.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// This temlpamte is tailored for the Node.js ecosystem, using appropriate modules and syntax. It includes instructions
// for setting up the environment, connecting to a Qdrant server, defining functions for interacting with Qdrant, and
// handling errors. The script also provides further resources and best practices for securing API keys and sensitive
// data. Users can uncomment and edit sections according to their needs, and the comments provide guidance on how to
// use each part of the script.
// Qdrant Docs:
// https://qdrant-client.readthedocs.io/en/latest/quickstart.html
// https://github.com/qdrant/qdrant-client
// Master 'qdrant_client_import' Script Template for Node.js
// ---------------------------
// Environment Setup Instructions
// ---------------------------
// Ensure Node.js (LTS version) is installed on your system.
// Initialize your Node.js project with `npm init`.
// Install the necessary packages using the package.json file:
// npm install
// ---------------------------
// Importing Modules
// ---------------------------
// Import the necessary modules
const { QdrantClient, models } = require('qdrant_client'); // Import Qdrant client library
const axios = require('axios'); // Axios is used for HTTP requests
const dotenv = require('dotenv'); // Dotenv for loading environment variables
dotenv.config();
// ---------------------------
// Configuration Section
// ---------------------------
// Configure your settings here, such as collection names, vector sizes, and connection parameters
const COLLECTION_NAME = "my_collection";
const VECTOR_SIZE = 100;
const HOST = "localhost";
const PORT = 6333;
const GRPC_PORT = 6334;
// ---------------------------
// Error Handling
// ---------------------------
// Wrap function calls in try-catch blocks to handle any potential errors
try {
// ---------------------------
// Connecting to Qdrant Server
// ---------------------------
// Replace "localhost" and "6333" with your server's host and port
const client = new QdrantClient(`${HOST}:${PORT}`);
// ---------------------------
// Function Call Examples
// ---------------------------
// Uncomment the lines below to call functions and perform operations
// Create a new collection
// createCollection(client);
// Insert vectors into a collection
// insertVectors(client);
// Search for similar vectors
// const hits = searchVectors(client);
// Search for similar vectors with filtering condition
// const filteredHits = searchVectorsWithFilter(client);
// ---------------------------
// Resource Cleanup
// ---------------------------
// Properly close connections and clean up resources after operations are complete
} catch (error) {
console.error(`An error occurred: ${error}`);
}
// ---------------------------
// Function Definitions
// ---------------------------
// Definitions of functions to interact with Qdrant
function createCollection(client) {
// Create a new collection in Qdrant
client.recreateCollection(COLLECTION_NAME, {
vector_params: new models.VectorParams(VECTOR_SIZE, models.Distance.COSINE)
}).then(() => {
console.log(`Created collection '${COLLECTION_NAME}'.`);
}).catch(error => {
console.error(`Error creating collection: ${error}`);
});
}
function insertVectors(client) {
// Insert vectors into the specified collection
const vectors = Array.from({ length: 100 }, () => Array.from({ length: VECTOR_SIZE }, Math.random));
client.upsert(COLLECTION_NAME, {
points: vectors.map((vector, idx) => new models.PointStruct(
idx,
vector,
{ color: "red", rand_number: idx % 10 }
))
}).then(() => {
console.log(`Inserted vectors into collection '${COLLECTION_NAME}'.`);
}).catch(error => {
console.error(`Error inserting vectors: ${error}`);
});
}
function searchVectors(client) {
// Search for similar vectors in the collection
const queryVector = Array.from({ length: VECTOR_SIZE }, Math.random);
client.search(COLLECTION_NAME, {
query_vector: queryVector,
limit: 5 // Return 5 closest points
}).then(hits => {
console.log("Performed vector search.");
return hits;
}).catch(error => {
console.error(`Error searching vectors: ${error}`);
});
}
function searchVectorsWithFilter(client) {
// Search for similar vectors with filtering condition
const queryVector = Array.from({ length: VECTOR_SIZE }, Math.random);
client.search(COLLECTION_NAME, {
query_vector: queryVector,
query_filter: new models.Filter([
new models.FieldCondition('rand_number', new models.Range(null, 3)) // Select only those results where `rand_number` >= 3
]),
limit: 5 // Return 5 closest points
}).then(hits => {
console.log("Performed filtered vector search.");
return hits;
}).catch(error => {
console.error(`Error searching vectors with filter: ${error}`);
});
}
// ---------------------------
// Further Resources
// ---------------------------
// For more detailed documentation, tutorials, and community support, visit:
// Qdrant Documentation: https://qdrant.tech/documentation/
// Qdrant Tutorials: https://qdrant.tech/tutorials/
// Qdrant GitHub Repository: https://github.com/qdrant/qdrant
// ---------------------------
// Security Best Practices
// ---------------------------
// Secure your API keys and sensitive data.
// Consider using environment variables or a .env file to store them.
// Example:
// const QDRANT_URL = process.env.QDRANT_URL;
// const QDRANT_API_KEY = process.env.QDRANT_API_KEY;
// const qdrantClient = new QdrantClient(QDRANT_URL, { headers: { 'Authorization': `Bearer ${QDRANT_API_KEY}` } });