import asyncio
import random
from actian_vectorai import AsyncVectorAIClient, VectorParams, Distance, PointStruct, reciprocal_rank_fusion
COLLECTION = "documents"
DIMENSION = 128
async def main():
async with AsyncVectorAIClient("localhost:50051") as client:
# Create collection if it doesn't exist
if not await client.collections.exists(COLLECTION):
await client.collections.create(
COLLECTION,
vectors_config=VectorParams(size=DIMENSION, distance=Distance.Cosine)
)
# Insert sample points
points = [
PointStruct(
id=i,
vector=[random.gauss(0, 1) for _ in range(DIMENSION)],
payload={
"text": f"Document {i} about {['AI', 'ML', 'NLP', 'CV'][i % 4]}",
"category": ["AI", "ML", "NLP", "CV"][i % 4]
}
)
for i in range(1, 101)
]
await client.points.upsert(COLLECTION, points)
print(f"✓ Inserted {len(points)} points")
# Generate multiple query vectors (e.g., from different models)
query_dense = [random.gauss(0, 1) for _ in range(DIMENSION)]
query_semantic = [random.gauss(0, 1) for _ in range(DIMENSION)]
# Perform separate searches
print("Dense search #1")
results_a = await client.points.search(
COLLECTION,
vector=query_dense,
limit=20
)
for r in results_a[:5]:
print(f" id={r.id:3d} score={r.score:.4f}")
print("\nDense search #2 (different vector)")
results_b = await client.points.search(
COLLECTION,
vector=query_semantic,
limit=20
)
for r in results_b[:5]:
print(f" id={r.id:3d} score={r.score:.4f}")
# Fuse results using RRF
print("\nRRF fusion (k=60)")
fused_results = reciprocal_rank_fusion(
[results_a, results_b],
limit=10,
ranking_constant_k=60
)
for i, point in enumerate(fused_results[:5], 1):
print(f"{i}. ID: {point.id}, Fused Score: {point.score:.4f}")
asyncio.run(main())