|
| 1 | + |
| 2 | +// Copyright 2024-present the vsag project |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#include <vsag/vsag.h> |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +int |
| 22 | +main(int argc, char** argv) { |
| 23 | + vsag::init(); |
| 24 | + |
| 25 | + /******************* Prepare Multi-Vector Base Dataset *****************/ |
| 26 | + // In ColBERT-style retrieval, each document has multiple vectors (one per token) |
| 27 | + // We'll create 100 documents, each with variable number of vectors (5-10) |
| 28 | + int64_t num_docs = 100; |
| 29 | + int64_t dim = 128; |
| 30 | + |
| 31 | + std::vector<int64_t> ids(num_docs); |
| 32 | + std::vector<uint32_t> vector_counts(num_docs); |
| 33 | + std::vector<float> datas; |
| 34 | + std::mt19937 rng(47); |
| 35 | + std::uniform_real_distribution<float> distrib_real; |
| 36 | + std::uniform_int_distribution<int> vec_count_dist(5, 10); |
| 37 | + |
| 38 | + // Generate document IDs and variable vector counts |
| 39 | + uint64_t total_vectors = 0; |
| 40 | + for (int64_t i = 0; i < num_docs; ++i) { |
| 41 | + ids[i] = i; |
| 42 | + vector_counts[i] = vec_count_dist(rng); |
| 43 | + total_vectors += vector_counts[i]; |
| 44 | + } |
| 45 | + |
| 46 | + // Generate all vectors |
| 47 | + datas.reserve(total_vectors * dim); |
| 48 | + for (uint64_t i = 0; i < total_vectors * dim; ++i) { |
| 49 | + datas.push_back(distrib_real(rng)); |
| 50 | + } |
| 51 | + |
| 52 | + // Create dataset with VectorCounts for multi-vector support |
| 53 | + auto base = vsag::Dataset::Make(); |
| 54 | + base->NumElements(num_docs) |
| 55 | + ->Dim(dim) |
| 56 | + ->Ids(ids.data()) |
| 57 | + ->Float32Vectors(datas.data()) |
| 58 | + ->VectorCounts(vector_counts.data()) // Specify number of vectors per document |
| 59 | + ->Owner(false); |
| 60 | + |
| 61 | + std::cout << "Created multi-vector dataset with " << num_docs << " documents" << std::endl; |
| 62 | + std::cout << "Total vectors: " << total_vectors << " (avg " << (double)total_vectors / num_docs |
| 63 | + << " vectors per doc)" << std::endl; |
| 64 | + |
| 65 | + /******************* Create WARP Index *****************/ |
| 66 | + // WARP index for ColBERT-style maxsin similarity |
| 67 | + std::string warp_build_parameters = R"( |
| 68 | + { |
| 69 | + "dtype": "float32", |
| 70 | + "metric_type": "ip", |
| 71 | + "dim": 128 |
| 72 | + } |
| 73 | + )"; |
| 74 | + auto index = vsag::Factory::CreateIndex("warp", warp_build_parameters).value(); |
| 75 | + |
| 76 | + /******************* Build WARP Index *****************/ |
| 77 | + if (auto build_result = index->Build(base); build_result.has_value()) { |
| 78 | + std::cout << "After Build(), Index WARP contains: " << index->GetNumElements() |
| 79 | + << " documents" << std::endl; |
| 80 | + } else { |
| 81 | + std::cerr << "Failed to build index: " << build_result.error().message << std::endl; |
| 82 | + exit(-1); |
| 83 | + } |
| 84 | + |
| 85 | + /******************* Prepare Multi-Vector Query *****************/ |
| 86 | + // Query also has multiple vectors (representing query tokens) |
| 87 | + uint32_t query_vec_count = 3; |
| 88 | + std::vector<float> query_vectors(query_vec_count * dim); |
| 89 | + for (uint32_t i = 0; i < query_vec_count * dim; ++i) { |
| 90 | + query_vectors[i] = distrib_real(rng); |
| 91 | + } |
| 92 | + |
| 93 | + auto query = vsag::Dataset::Make(); |
| 94 | + query->NumElements(1) |
| 95 | + ->Dim(dim) |
| 96 | + ->Float32Vectors(query_vectors.data()) |
| 97 | + ->VectorCounts(&query_vec_count) // Specify query has multiple vectors |
| 98 | + ->Owner(false); |
| 99 | + |
| 100 | + std::cout << "Query has " << query_vec_count << " vectors" << std::endl; |
| 101 | + |
| 102 | + /******************* KnnSearch For WARP Index *****************/ |
| 103 | + // WARP performs maxsin similarity: for each query vector, find max similarity |
| 104 | + // with any document vector, sum across query vectors |
| 105 | + auto warp_search_parameters = R"({})"; |
| 106 | + int64_t topk = 5; |
| 107 | + auto result = index->KnnSearch(query, topk, warp_search_parameters).value(); |
| 108 | + |
| 109 | + /******************* Print Search Result *****************/ |
| 110 | + std::cout << "Top-" << topk << " results (maxsin similarity): " << std::endl; |
| 111 | + for (int64_t i = 0; i < result->GetDim(); ++i) { |
| 112 | + std::cout << " Document " << result->GetIds()[i] |
| 113 | + << ": score = " << result->GetDistances()[i] << std::endl; |
| 114 | + } |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments