-
Notifications
You must be signed in to change notification settings - Fork 3
/
postgres.js
315 lines (285 loc) · 7.81 KB
/
postgres.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
postgres , pgvector , embeddings utils
*/
import { PGlite } from "./modules/pglite/index.js";
import { vector } from "./modules/pglite/vector/index.js";
import { getEncoding } from "./modules/js-tiktoken/index.js";
import CONFIG from "./config.js";
console.log("debug:postgres : script start");
const MAX_TOKENS_PER_DOC = 8192;
const OPENAI_API_KEY = CONFIG.setup.OPENAI_API_KEY;
const LOCAL_DB_NAME = CONFIG.setup.DB_NAME;
const enc = getEncoding("cl100k_base");
const _chunkify = (array, size) => {
const chunks = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
};
async function sha256(source) {
const sourceBytes = new TextEncoder().encode(source);
const digest = await window.crypto.subtle.digest("SHA-256", sourceBytes);
const resultBytes = [...new Uint8Array(digest)];
return resultBytes.map((x) => x.toString(16).padStart(2, "0")).join("");
}
function _tokens_chop({
text,
limit = MAX_TOKENS_PER_DOC,
chop_from_bottom = false,
chop_from_center = false,
}) {
const tokens = enc.encode(text);
if (chop_from_center) {
const halfLimit = Math.floor(limit / 2);
const start = Math.max(0, Math.floor((tokens.length - limit) / 2));
return enc.decode(tokens.slice(start, start + limit));
}
return !chop_from_bottom
? enc.decode(tokens.slice(0, limit))
: enc.decode(tokens.slice(Math.max(0, tokens.length - limit)));
}
let db;
async function getDb() {
console.log("debug:postgres : getDb : start");
if (db) {
return db;
}
db = new PGlite(`idb://${LOCAL_DB_NAME}`, {
extensions: {
vector,
},
});
await db.waitReady;
console.log("debug:postgres : getDb : done");
return db;
}
async function init(force = false) {
console.log("debug:postgres : init : start");
await db.exec(`
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS embeddings (
uid TEXT NOT NULL UNIQUE,
content TEXT,
timestamp BIGINT NOT NULL,
embedding VECTOR(1536)
);
CREATE INDEX IF NOT EXISTS embeddings_embedding_idx ON embeddings USING hnsw (embedding vector_ip_ops);
`);
console.log("debug:postgres : init : done");
}
getDb().then(() => init());
async function clear({ table = "embeddings" }) {
await db.query(`DELETE FROM ${table};`);
await init((force = true));
}
async function count({ table = "embeddings" }) {
const res = await db.query(`SELECT COUNT(*) FROM ${table};`);
return res.rows[0].count;
}
async function latest({ table = "embeddings", amount = 1 }) {
const res = await db.query(
`SELECT * FROM ${table} ORDER BY timestamp DESC LIMIT $1;`,
[amount],
);
return res.rows;
}
async function insert({ texts }) {
const chunks = _chunkify(texts, 15);
await Promise.all(
chunks.map(async (_chunk) => {
const chunkWithUids = await Promise.all(
_chunk.map(async (text) => {
const uid = await sha256(text);
return { uid, text };
}),
);
const existingUids = await db.query(`
SELECT uid FROM embeddings WHERE uid IN (${chunkWithUids.map((entry) => `'${entry.uid}'`).join(", ")});
`);
const existingUidSet = new Set(existingUids.rows.map((row) => row.uid));
const newEntries = chunkWithUids.filter(
(entry) => !existingUidSet.has(entry.uid),
);
if (newEntries.length) {
const newTexts = newEntries.map((entry) => entry.text);
const embeddings = await vectorize({ texts: newTexts });
if (!embeddings) return;
const entriesWithEmbeddings = newEntries.map((entry, idx) => ({
uid: entry.uid,
text: entry.text,
timestamp: Date.now(),
vector: embeddings[idx],
}));
const pg_entries = entriesWithEmbeddings
.map((entry) => {
const content = _tokens_chop({
text: entry.text,
chop_from_center: true,
}).replaceAll(`'`, `''`);
return `\t('${entry.uid}', '${content}', '${entry.timestamp}', '${JSON.stringify(entry.vector)}')`;
})
.join(",\n");
await db.exec(`
insert into embeddings (uid, content, timestamp, embedding) values
${pg_entries};
`);
}
}),
);
return {
count: await db.query(`SELECT COUNT(*) FROM embeddings;`),
};
}
async function vectorize({ texts }) {
const sliceTexts = (texts) => {
return texts.map((text) => {
return _tokens_chop({
text,
limit: MAX_TOKENS_PER_DOC,
chop_from_center: true,
});
});
};
texts = sliceTexts(texts);
try {
const response = await fetch("https://api.openai.com/v1/embeddings", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "text-embedding-3-small",
input: texts,
encoding_format: "float",
}),
});
if (!response.ok) {
const errorData = await response.json();
console.error("> openAI API error:", errorData);
return null;
}
const data = await response.json();
console.log({ debug_vectorize: { texts, data } });
return data.data.sort((a, b) => a.index - b.index).map((e) => e.embedding);
} catch (error) {
console.error("Fetch Error:", error);
alert(`Network error while fetching embedding for "${text}".`);
return null;
}
}
async function search({ query, embedding, match_threshold = 0.0, amount = 3 }) {
if (!embedding) {
embedding = (await vectorize({ texts: [query] }))[0];
}
if (typeof embedding === "string") {
embedding = JSON.parse(embedding);
}
console.log({ "debug:postgres:search:embedding": embedding });
try {
const res = await db.query(
`
SELECT uid, content, timestamp, embedding <#> $1 AS score FROM embeddings
WHERE embeddings.embedding <#> $1 < $2
ORDER BY embeddings.embedding <#> $1
LIMIT $3;
`,
[JSON.stringify(embedding), -Number(match_threshold), Number(amount)],
);
// console.log({ debug_search_res: { embedding, res } });
return res.rows;
} catch (error) {
console.error("Search Error:", error);
// alert("Error during search operation.");
return [];
}
}
function _format_timestamp(timestamp) {
return new Date(timestamp).toLocaleString("en-US", {
year: "numeric",
month: "short",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3,
hour12: false,
});
}
async function context({ recent = 2, branches = 5 }) {
/*
--- make context for conversation
gets latest document , gets relevant n docs ,
*/
const docs = await latest({ amount: recent });
console.log("debug:postgres.js:context:docs", { docs });
const context =
"<CONTEXT_MEMORIES>\n\n" +
_tokens_chop({
limit: CONFIG.setup.calls.tokens.limits.context,
chop_from_bottom: true,
text: (
await Promise.all(
docs.map(async (item) => {
return [
item,
await search({ embedding: item.embedding, amount: branches }),
];
}),
)
)
.flat()
.flat()
.filter(
(value, index, self) =>
index === self.findIndex((t) => t.uid === value.uid),
)
.sort((a, b) => b.timestamp - a.timestamp)
.map((item) => {
return (
`<memory>\n$ seen on : ${_format_timestamp(item.timestamp)}` +
`\n${item.content.trim()}` +
`\n</memory>`
);
})
.join("\n\n"),
}) +
"\n\n</CONTEXT_MEMORIES>";
return { context };
}
async function rag({ query, amount = 2 }) {
const docs = (await search({ query, amount })).reverse();
if (docs.length) {
return {
rag:
"<RELATED_MEMORIES>\n\n" +
_tokens_chop({
limit: CONFIG.setup.calls.tokens.limits.additional,
chop_from_bottom: true,
text: docs
.map((item) => {
return (
`<memory>\n$ seen on : ${_format_timestamp(item.timestamp)}` +
`\n${item.content.trim()}` +
`\n</memory>`
);
})
.join(``),
}) +
"\n\n</RELATED_MEMORIES>",
};
}
return { rag: "" };
}
export default {
count,
insert,
search,
latest,
vectorize,
clear,
context,
rag,
chop: _tokens_chop,
};