import { VectorAIClient } from '@actian/vectorai-client';
const DIMENSION = 384;
const COLLECTION = 'documents';
async function main() {
const client = new VectorAIClient('localhost:6574');
// Create the collection
await client.collections.create(COLLECTION, { dimension: DIMENSION, distanceMetric: 'COSINE' });
// Batch insert multiple document vectors
const payloads = [
{ text: 'Machine learning fundamentals', category: 'education' },
{ text: 'Neural networks explained', category: 'education' },
{ text: 'Cooking recipes collection', category: 'lifestyle' },
{ text: 'Travel guide to Europe', category: 'travel' },
{ text: 'Deep learning architectures', category: 'education' },
];
// Create points with vectors and metadata
const points = payloads.map((payload, i) => ({
id: i + 1, // Point ID
vector: Array.from({ length: DIMENSION }, () => Math.random() * 2 - 1), // Generate vector
payload: payload, // Attach metadata (optional)
}));
// Batch upsert points
await client.points.upsert(COLLECTION, points, { wait: true });
// Search for similar documents using a query vector
const queryVector = Array.from({ length: DIMENSION }, () => Math.random() * 2 - 1);
// Perform similarity search
const results = await client.points.search(COLLECTION, queryVector, {
limit: 3, // Top 3 results
withPayload: true,
});
// Display results
console.log('Top 3 similar documents:');
for (const result of results) {
console.log(` - ${result.payload.text} (score: ${result.score.toFixed(4)})`);
}
}
main().catch(console.error);