-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvecdb.cpp
More file actions
executable file
·40 lines (34 loc) · 1.21 KB
/
vecdb.cpp
File metadata and controls
executable file
·40 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "vecdb.hpp"
vecdb::vecdb(const std::string &collectionName)
{
std::string path = collectionName;
this->hnswSpace = std::make_unique<hnswlib::L2Space>(L2SZ);
this->hnswIndex = std::make_unique<hnswlib::HierarchicalNSW<float>>(this->hnswSpace.get(), path);
}
vecdb::vecdb(const std::string &collectionName, const std::vector<std::vector<float>>& vecDocs)
{
std::string path = collectionName;
this->hnswSpace = std::make_unique<hnswlib::L2Space>(L2SZ);
int max_elements = 10000;
int M = 16;
int ef_construction = 200;
this->hnswIndex = std::make_unique<hnswlib::HierarchicalNSW<float>>(this->hnswSpace.get(), max_elements, M, ef_construction);
for (size_t i = 0; i < vecDocs.size(); ++i) {
this->hnswIndex->addPoint(vecDocs[i].data(), i);
}
this->hnswIndex->saveIndex(path);
}
vecdb::~vecdb()
{
}
std::vector<int> vecdb::findNearestIds(const std::vector<float> vectoredQuestion)
{
std::vector<int> result = {};
auto searchResult = this->hnswIndex->searchKnnCloserFirst(vectoredQuestion.data(), 3);
for(auto& x : searchResult)
{
hnswlib::labeltype label = x.second;
result.push_back(label);
}
return result;
}