import { VectorAIClient, reciprocalRankFusion } from '@actian/vectorai-client';
const COLLECTION = "documents";
const DIMENSION = 128;
async function main() {
const client = new VectorAIClient('localhost:6574');
// Create collection if it doesn't exist
await client.collections.create(COLLECTION, {
dimension: DIMENSION,
distanceMetric: 'COSINE'
});
// Insert sample points
const points = Array.from({ length: 100 }, (_, i) => ({
id: i + 1,
vector: Array.from({ length: DIMENSION }, () => Math.random() * 2 - 1),
payload: {
text: `Document ${i + 1} about ${['AI', 'ML', 'NLP', 'CV'][i % 4]}`,
category: ['AI', 'ML', 'NLP', 'CV'][i % 4]
}
}));
await client.points.upsert(COLLECTION, points, { wait: true });
console.log(`Inserted ${points.length} points`);
// Generate multiple query vectors (e.g., from different models)
const queryDense = Array.from({ length: DIMENSION }, () => Math.random() * 2 - 1);
const querySemantic = Array.from({ length: DIMENSION }, () => Math.random() * 2 - 1);
// Perform separate searches
console.log("Dense search #1");
const resultsA = await client.points.search(COLLECTION, queryDense, {
limit: 20
});
resultsA.slice(0, 5).forEach(r => {
console.log(` id=${r.id} score=${r.score.toFixed(4)}`);
});
console.log("\nDense search #2 (different vector)");
const resultsB = await client.points.search(COLLECTION, querySemantic, {
limit: 20
});
resultsB.slice(0, 5).forEach(r => {
console.log(` id=${r.id} score=${r.score.toFixed(4)}`);
});
// Fuse results using RRF
console.log("\nRRF fusion (k=60)");
const fusedResults = reciprocalRankFusion(
[resultsA, resultsB],
{ k: 60, limit: 10 }
);
fusedResults.slice(0, 5).forEach((point, i) => {
console.log(`${i + 1}. ID: ${point.id}, Fused Score: ${point.score.toFixed(4)}`);
});
}
main().catch(console.error);