-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorStorage.h
More file actions
122 lines (97 loc) · 2.83 KB
/
VectorStorage.h
File metadata and controls
122 lines (97 loc) · 2.83 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
#include "ONNXEmbedder.h"
#include "PageItem.h"
#include <vector>
#include <mutex>
#include <httplib.h>
#include <unordered_set>
#include <string>
#include <memory>
#include <cstdint>
#include <pqxx/pqxx>
#include <pqxx/connection.hxx>
/*
This class manages vector storage, including embedding texts, searching, and indexing using HNSW.
*/
constexpr size_t DIM = 384; // Dimension of embeddings
constexpr size_t MAX_ELEMENTS = 2'000'000; // Maximum number of elements in HNSW index
// Holds search result
struct SearchResult {
int64_t id;
float score = 0.0f;
std::string title;
std::string description;
std::string link;
};
// Holds token hash and frequency for a document, used for token overlap scoring
struct TokenStat {
int64_t hash;
int16_t freq;
};
class VectorStorage {
public:
VectorStorage(
pqxx::connection& conn,
size_t threadCount
);
void ingestBatch(const std::vector<PageItem>& pages);
std::vector<SearchResult> search(
const std::string& query,
size_t topK
);
private:
pqxx::connection& conn;
size_t threadCount;
std::unique_ptr<ONNXEmbedder> embedder;
std::vector<int64_t> insertBatch(
const std::vector<PageItem>& pages,
const std::vector<std::vector<float>>& embeddings
);
std::vector<std::vector<float>> embedBatch(
const std::vector<std::string>& texts
);
std::vector<float> EmbedText(
const std::string& text
);
std::string VectorToPGVector(
const std::vector<float>& v
);
std::unordered_map<std::string, int> tokenizeWithFrequency(
const std::string& text
);
std::string buildTokenStatArray(
const std::unordered_map<std::string, int>& tokenFreq
);
std::unordered_set<std::string> tokenizeText(
const std::string& text
);
std::vector<int64_t> hashTokens(
const std::unordered_set<std::string>& tokens
);
std::string extractEntity(
const std::string& query
);
std::unordered_map<int64_t, int> parseTokenStats(
const pqxx::field& field
);
float keywordScore(
const std::unordered_set<int64_t>& queryHashes,
const std::unordered_map<int64_t, int>& docFreqs
);
float titleScore(
const std::string& cleanTitle,
const std::unordered_set<std::string>& queryTokens,
const std::string& cleanQuery
);
std::string cleanString(
const std::string& text
);
private:
const std::unordered_set<std::string> stopwords = {
"a", "an", "the", "is", "are", "was", "were",
"of", "to", "in", "on", "for", "with",
"what", "who", "when", "where", "why", "how",
"define", "definition", "explain"
};
httplib::Client client; // HTTP client for embedding server
};