import { SolVec } from "@veclabs/solvec";
const sv = new SolVec({ network: "devnet" });
const collection = sv.collection("product-catalog", { dimensions: 1536 });
// Index products by their description
async function indexProducts(
products: Array<{
id: string;
name: string;
description: string;
category: string;
price: number;
}>,
) {
const embeddings = await batchEmbed(
products.map((p) => `${p.name}. ${p.description}. Category: ${p.category}`),
);
await collection.upsert(
products.map((p, i) => ({
id: p.id,
values: embeddings[i],
metadata: {
name: p.name,
description: p.description,
category: p.category,
price: p.price,
},
})),
);
}
// Get recommendations for a product
async function getSimilarProducts(productId: string, topK = 6) {
// Fetch the product's vector
const product = await collection.fetch(productId);
// Find similar products (exclude the product itself)
const results = await collection.query({
vector: product.values,
topK: topK + 1, // +1 because the product itself will be in results
minScore: 0.7,
});
return results
.filter((r) => r.id !== productId) // exclude self
.slice(0, topK);
}