-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaisearch-docs.js
More file actions
228 lines (206 loc) · 8.18 KB
/
aisearch-docs.js
File metadata and controls
228 lines (206 loc) · 8.18 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
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
/**
* Edge Function template for performing AI-powered search on databases created by the GitHub Action.
*
* This template provides a ready-to-use Edge Function that users can copy and paste
* into their SQLite Cloud project to enable semantic search capabilities on the
* documentation database generated by the sqlite-aisearch-action GitHub Action.
*
* The function combines vector embeddings with full-text search using Reciprocal
* Rank Fusion (RRF) to deliver accurate and contextually relevant search results.
*
* See README.md for deployment instructions.
*/
//---- CONFIGURATION ----
const sqliteAIBaseUrl = "https://aiserver.vital-rhino.eks.euc1.ryujaz.sqlite.cloud";
const sqliteAIAPI = "/v1/ai/embeddings";
const topKSentences = 3; // Number of top sentences to include in preview
const maxChars = 400; // Maximum total characters for preview
const gap = "[...]"; // Gap indicator string
//-----------------------
const query = request.params.query;
const limit = parseInt(request.params.limit) || 5; // Number of top results to return
// Get embedding from sqlite-ai-server
const data = {"text": query };
const response = await fetch(sqliteAIBaseUrl + sqliteAIAPI, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error("Failed to generate embedding. " + response.statusText);
}
const result = await response.json();
const query_embedding = result.data.embedding;
// Clean query for full-text search
const query_fts = (query.toLowerCase().match(/\b\w+\b/g) || []).join(" ") + "*";
// Vector configuration must match the embedding parameters used during database generation
await connection.sql("SELECT vector_init('chunks', 'embedding', 'type=INT8,dimension=768,distance=cosine')");
await connection.sql("SELECT vector_init('sentences', 'embedding', 'type=INT8,dimension=768,distance=cosine')");
const res = await connection.sql(
`
-- sqlite-vector KNN vector search results
WITH vec_matches AS (
SELECT
v.rowid AS chunk_id,
row_number() OVER (ORDER BY v.distance) AS rank_number,
v.distance
FROM vector_quantize_scan('chunks', 'embedding', ?, ?) AS v
),
-- Full-text search results
fts_matches AS (
SELECT
chunks_fts.rowid AS chunk_id,
row_number() OVER (ORDER BY rank) AS rank_number,
rank AS score
FROM chunks_fts
WHERE chunks_fts MATCH ?
LIMIT ?
),
-- combine FTS5 + vector search results with RRF
matches AS (
SELECT
COALESCE(vec_matches.chunk_id, fts_matches.chunk_id) AS chunk_id,
vec_matches.rank_number AS vec_rank,
fts_matches.rank_number AS fts_rank,
-- Reciprocal Rank Fusion score
(
COALESCE(1.0 / (60 + vec_matches.rank_number), 0.0) * 1.0 +
COALESCE(1.0 / (60 + fts_matches.rank_number), 0.0) * 1.0
) AS combined_rank,
vec_matches.distance AS vec_distance,
fts_matches.score AS fts_score
FROM vec_matches
FULL OUTER JOIN fts_matches
ON vec_matches.chunk_id = fts_matches.chunk_id
)
SELECT
documents.id,
documents.uri,
documents.metadata,
chunks.id AS chunk_id,
chunks.content AS chunk_content,
vec_rank,
fts_rank,
combined_rank,
vec_distance,
fts_score
FROM matches
JOIN chunks ON chunks.id = matches.chunk_id
JOIN documents ON documents.id = chunks.document_id
ORDER BY combined_rank DESC
;
`, query_embedding, limit, query_fts, limit
);
// The results from the query may contain multiple resulting chunks per document.
// We want to return one result per document, so we will group by document id and take
// the top-ranked chunk as a snippet.
const seenDocuments = new Set();
const topResults = res
.filter(item => !seenDocuments.has(item.id) && seenDocuments.add(item.id))
.slice(0, limit);
// ----- Fetch top sentences for each top result -----
for (const result of topResults) {
result.sentences = await connection.sql(
`WITH vec_matches AS (
SELECT
v.rowid AS sentence_id,
row_number() OVER (ORDER BY v.distance) AS rank_number,
v.distance
FROM vector_quantize_scan_stream('sentences', 'embedding', ?) AS v
JOIN sentences ON sentences.rowid = v.rowid
WHERE sentences.chunk_id = ?
LIMIT ?
)
SELECT
sentence_id,
-- Extract sentence directly from document content
COALESCE(
substr(chunks.content, sentences.start_offset + 1, sentences.end_offset - sentences.start_offset),
""
) AS content,
sentences.start_offset AS sentence_start_offset,
sentences.end_offset AS sentence_end_offset,
rank_number,
distance
FROM vec_matches
JOIN sentences ON sentences.rowid = vec_matches.sentence_id
JOIN chunks ON chunks.id = sentences.chunk_id
ORDER BY rank_number ASC
;
`, query_embedding, result.chunk_id, topKSentences
);
}
// ----- Build snippets from sentences -----
for (const item of topResults) {
const topSentences = item.sentences ? item.sentences.slice(0, topKSentences) : [];
let snippet = "";
if (topSentences.length === 0) {
// Fallback: no sentences, return truncated chunk content
const chunkContent = item.chunk_content || "";
snippet = chunkContent.substring(0, maxChars);
} else {
// Sort by start_offset to maintain document order
topSentences.sort((a, b) => {
const offsetA = a.sentence_start_offset !== null ? a.sentence_start_offset : -1;
const offsetB = b.sentence_start_offset !== null ? b.sentence_start_offset : -1;
return offsetA - offsetB;
});
const previewParts = [];
let totalChars = 0;
let prevEndOffset = null;
for (const sentence of topSentences) {
const sentenceText = sentence.content;
// Check for gap between sentences
if (prevEndOffset !== null && sentence.sentence_start_offset !== null) {
const gapSize = sentence.sentence_start_offset - prevEndOffset;
if (gapSize > 10) {
previewParts.push(gap);
totalChars += gap.length;
}
}
previewParts.push(sentenceText);
totalChars += sentenceText.length;
prevEndOffset = sentence.sentence_end_offset;
}
const preview = previewParts.join(" ");
snippet = preview.length > maxChars ? preview.substring(0, maxChars - 3) + "..." : preview;
}
item.snippet = snippet;
}
// ----- URLs for results -----
// Customize this section based on how URLs should be constructed for your documents.
// This example uses 'base_url' from metadata and 'slug' if available, otherwise derives from URI.
// ----------------------------
const finalResults = topResults
.map(item => {
const metadata = JSON.parse(item.metadata);
const baseUrl = metadata.base_url;
const slug = metadata.extracted?.slug;
const uri = item.uri;
let fullUrl;
if (slug) {
fullUrl = `${baseUrl}${slug}`;
} else {
const uriWithoutExtension = uri
.toLowerCase()
.replace(/\.(mdx?|md)$/i, '');
fullUrl = `${baseUrl}${uriWithoutExtension}`;
}
return {
id: item.id,
url: fullUrl,
title: metadata.extracted?.title || metadata.generated?.title,
snippet: item.snippet
};
});
return {
data: {
/**
* @type {Array<{id: number, url: string, title: string, snippet: string}>}
* The search results with constructed URLs, titles, and snippets.
*/
search: finalResults
}
}