-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrag_minimalistic.html
72 lines (63 loc) · 2.36 KB
/
rag_minimalistic.html
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
<!DOCTYPE html>
<html>
<head>
<title>Minimalistic RAG</title>
<script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
textarea {
width: 100%;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Minimalistic RAG</h1>
<p>Enter three sentences:</p>
<textarea id="sentences" rows="6"></textarea>
<p>Enter a query sentence:</p>
<textarea id="query" rows="2"></textarea>
<button onclick="findSimilarity()">Find Similarity</button>
<div id="result"></div>
<script>
let pipeline;
async function init() {
const { pipeline } = await import('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2');
window.pipeline = await pipeline('feature-extraction', 'Snowflake/snowflake-arctic-embed-xs');
}
function cosineSimilarity(a, b) {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
async function findSimilarity() {
const sentences = document.getElementById('sentences').value.split('\n');
const query = document.getElementById('query').value;
const resultDiv = document.getElementById('result');
const sentenceEmbeddings = await Promise.all(sentences.map(sentence =>
window.pipeline(sentence, { pooling: 'mean', normalize: true }).then(output => output.data)
));
const queryEmbedding = await window.pipeline(query, { pooling: 'mean', normalize: true }).then(output => output.data);
const similarities = sentenceEmbeddings.map(embedding => cosineSimilarity(queryEmbedding, embedding));
resultDiv.innerHTML = `
<p>Similarities:</p>
<ul>
${similarities.map((sim, i) => `<li>Sentence ${i+1}: ${sim.toFixed(4)}</li>`).join('')}
</ul>
`;
}
init();
</script>
</body>
</html>