Repository navigation
Expand file tree
/
Copy pathrag-query.js
More file actions
167 lines (148 loc) · 5.57 KB
/
Copy pathrag-query.js
File metadata and controls
167 lines (148 loc) · 5.57 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
/**
* api/rag-query.js
*
* Vercel serverless function (Node runtime). POST { question: string }
* -> { answer: string, sources: [{ moduleTitle, sectionTitle }] }
*
* Flow:
* 1. Embed the incoming question with Voyage AI.
* 2. Brute-force cosine similarity against the pre-computed embeddings
* in content/embeddings.json (fine at this scale — a few hundred
* chunks, no vector DB needed).
* 3. Take the top K chunks, stuff them into a Claude API call as
* grounding context, and return the answer plus which modules it
* drew from.
*
* Env vars needed in Vercel project settings:
* VOYAGE_API_KEY
* ANTHROPIC_API_KEY
*/
const fs = require("fs");
const path = require("path");
const EMBEDDINGS_PATH = path.join(process.cwd(), "content", "embeddings.json");
const TOP_K = 5;
const VOYAGE_MODEL = "voyage-4-lite";
const CLAUDE_MODEL = "claude-sonnet-4-6";
let cachedEmbeddings = null;
function loadEmbeddings() {
if (!cachedEmbeddings) {
cachedEmbeddings = JSON.parse(fs.readFileSync(EMBEDDINGS_PATH, "utf8"));
}
return cachedEmbeddings;
}
function cosineSimilarity(a, b) {
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
async function embedQuery(question) {
const response = await fetch("https://api.voyageai.com/v1/embeddings", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.VOYAGE_API_KEY}`,
},
body: JSON.stringify({
input: [question],
model: VOYAGE_MODEL,
input_type: "query",
}),
});
if (!response.ok) throw new Error(`Voyage embed error: ${await response.text()}`);
const data = await response.json();
return data.data[0].embedding;
}
async function askClaude(question, contextChunks, apiKey) {
const contextBlock = contextChunks
.map((c, i) => `[${i + 1}] (${c.moduleTitle} — ${c.sectionTitle})\n${c.text}`)
.join("\n\n");
const systemPrompt = `You answer questions about the "Learn Claude" module series using ONLY the excerpts provided below. If the excerpts don't contain the answer, say so plainly rather than guessing. Cite which module each part of your answer comes from.
EXCERPTS:
${contextBlock}`;
const response = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: CLAUDE_MODEL,
max_tokens: 800,
system: systemPrompt,
messages: [{ role: "user", content: question }],
}),
});
if (!response.ok) throw new Error(`Claude API error: ${await response.text()}`);
const data = await response.json();
return data.content.map((block) => (block.type === "text" ? block.text : "")).join("\n");
}
// Fallback used when no visitor-supplied key is present: no Claude call,
// just the raw retrieved excerpts, lightly formatted. Costs nothing.
function buildExcerptFallback(contextChunks) {
const body = contextChunks
.map((c, i) => `${i + 1}. From "${c.moduleTitle}" (${c.sectionTitle}):\n${c.text}`)
.join("\n\n");
return `Here are the closest matching passages (add your own Anthropic API key above for a synthesized answer instead):\n\n${body}`;
}
module.exports = async (req, res) => {
if (req.method !== "POST") {
res.status(405).json({ error: "Use POST" });
return;
}
const { question, apiKey } = req.body || {};
if (!question || typeof question !== "string") {
res.status(400).json({ error: "Missing 'question' string in request body" });
return;
}
// A visitor-supplied key is used only for this single request and is
// never logged, stored, or written anywhere.
const usingVisitorKey = typeof apiKey === "string" && apiKey.trim().length > 0;
try {
const embeddings = loadEmbeddings();
const queryVector = await embedQuery(question);
const scored = embeddings
.map((chunk) => ({ chunk, score: cosineSimilarity(queryVector, chunk.embedding) }))
.sort((a, b) => b.score - a.score)
.slice(0, TOP_K);
const topChunks = scored.map((s) => s.chunk);
let answer;
if (usingVisitorKey) {
answer = await askClaude(question, topChunks, apiKey.trim());
} else {
answer = buildExcerptFallback(topChunks);
}
const sources = [...new Map(
topChunks.map((c) => [c.moduleTitle, { moduleTitle: c.moduleTitle, sectionTitle: c.sectionTitle }])
).values()];
res.status(200).json({ answer, sources, mode: usingVisitorKey ? "generated" : "excerpts" });
} catch (err) {
console.error(err);
// If a bad visitor key caused the failure, fall back to excerpts rather
// than a bare error.
if (usingVisitorKey) {
try {
const embeddings = loadEmbeddings();
const queryVector = await embedQuery(question);
const topChunks = embeddings
.map((chunk) => ({ chunk, score: cosineSimilarity(queryVector, chunk.embedding) }))
.sort((a, b) => b.score - a.score)
.slice(0, TOP_K)
.map((s) => s.chunk);
res.status(200).json({
answer: `Your API key didn't work, so here are the closest matching excerpts instead:\n\n${buildExcerptFallback(topChunks)}`,
sources: [],
mode: "excerpts",
});
return;
} catch (_) {
// fall through to generic error below
}
}
res.status(500).json({ error: "Something went wrong answering that question." });
}
};