Skip to content

Commit 5961ba1

Browse files
committed
draft 1 of tutorial - Getting Started with Vector Search Using Redis in NodeJS
1 parent 6a8b8d8 commit 5961ba1

14 files changed

+569
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Sample data
2+
const products = [
3+
{
4+
name: 'Puma Men Race Black Watch',
5+
price: 150,
6+
quality: 5,
7+
popularity: 8,
8+
},
9+
{
10+
name: 'Puma Men Top Fluctuation Red Black Watch',
11+
price: 180,
12+
quality: 7,
13+
popularity: 6,
14+
},
15+
{
16+
name: 'Inkfruit Women Behind Cream Tshirt',
17+
price: 5,
18+
quality: 9,
19+
popularity: 7,
20+
},
21+
];
22+
23+
const dataWithAttributes = products.map((product) => ({
24+
x: product.price,
25+
y: product.quality,
26+
label: product.name,
27+
}));
28+
29+
const product1Point = { x: products[0].price, y: products[0].quality };
30+
const product2Point = { x: products[1].price, y: products[1].quality };
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-return */
2+
/* eslint-disable @typescript-eslint/no-unsafe-call */
3+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
4+
5+
const ctxCosine = document
6+
.getElementById('productChartCosine')
7+
.getContext('2d');
8+
9+
function cosineSimilarity(point1, point2) {
10+
let dotProduct = point1.x * point2.x + point1.y * point2.y;
11+
let magnitudePoint1 = Math.sqrt(
12+
Math.pow(point1.x, 2) + Math.pow(point1.y, 2),
13+
);
14+
let magnitudePoint2 = Math.sqrt(
15+
Math.pow(point2.x, 2) + Math.pow(point2.y, 2),
16+
);
17+
return dotProduct / (magnitudePoint1 * magnitudePoint2);
18+
}
19+
20+
const cosineSim = cosineSimilarity(product1Point, product2Point);
21+
22+
const scatterChartCosine = new Chart(ctxCosine, {
23+
type: 'scatter',
24+
data: {
25+
datasets: [
26+
{
27+
label: 'Products',
28+
data: dataWithAttributes,
29+
pointBackgroundColor: ['black', 'red', 'bisque'],
30+
pointRadius: 5,
31+
},
32+
{
33+
label: 'Vector for Product-1',
34+
data: [{ x: 0, y: 0 }, product1Point],
35+
showLine: true,
36+
fill: false,
37+
borderColor: 'black',
38+
pointRadius: [0, 5],
39+
lineTension: 0,
40+
},
41+
{
42+
label: 'Vector for Product-2',
43+
data: [{ x: 0, y: 0 }, product2Point],
44+
showLine: true,
45+
fill: false,
46+
borderColor: 'red',
47+
pointRadius: [0, 5],
48+
lineTension: 0,
49+
},
50+
],
51+
},
52+
options: {
53+
responsive: true,
54+
plugins: {
55+
legend: {
56+
position: 'top',
57+
},
58+
title: {
59+
display: true,
60+
text: `Cosine Similarity between Product-1 and Product-2 is ${cosineSim}`,
61+
},
62+
},
63+
scales: {
64+
x: {
65+
type: 'linear',
66+
position: 'bottom',
67+
title: {
68+
display: true,
69+
text: 'Price ($)',
70+
},
71+
ticks: {
72+
beginAtZero: true,
73+
},
74+
min: 0, // Ensure it starts from 0
75+
},
76+
y: {
77+
title: {
78+
display: true,
79+
text: 'Quality (1-10)',
80+
},
81+
ticks: {
82+
beginAtZero: true,
83+
},
84+
min: 0, // Ensure it starts from 0
85+
},
86+
},
87+
tooltips: {
88+
callbacks: {
89+
title: function (tooltipItem, data) {
90+
return data.datasets[0].data[tooltipItem[0].index].label;
91+
},
92+
},
93+
},
94+
},
95+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-call */
2+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
3+
const ctx = document.getElementById('productChartEuclidean').getContext('2d');
4+
5+
function euclideanDistance(point1, point2) {
6+
return Math.sqrt(
7+
Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2),
8+
);
9+
}
10+
11+
const distance = euclideanDistance(product1Point, product2Point);
12+
13+
const scatterChart = new Chart(ctx, {
14+
type: 'scatter',
15+
data: {
16+
datasets: [
17+
{
18+
label: 'Products',
19+
data: dataWithAttributes,
20+
pointBackgroundColor: ['black', 'red', 'bisque'],
21+
pointRadius: 5,
22+
},
23+
{
24+
label: `Euclidean Distance: ${distance.toFixed(2)}`,
25+
data: [product1Point, product2Point],
26+
showLine: true,
27+
fill: false,
28+
borderColor: 'green',
29+
pointRadius: 0,
30+
lineTension: 0,
31+
},
32+
],
33+
},
34+
options: {
35+
responsive: true,
36+
plugins: {
37+
legend: {
38+
position: 'top',
39+
},
40+
title: {
41+
display: true,
42+
text: `Euclidean Distance between Product-1 and Product-2`,
43+
},
44+
},
45+
scales: {
46+
x: {
47+
type: 'linear',
48+
position: 'bottom',
49+
title: {
50+
display: true,
51+
text: 'Price ($)',
52+
},
53+
ticks: {
54+
beginAtZero: true,
55+
},
56+
min: 0, // Ensure it starts from 0
57+
},
58+
y: {
59+
title: {
60+
display: true,
61+
text: 'Quality (1-10)',
62+
},
63+
ticks: {
64+
beginAtZero: true,
65+
},
66+
min: 0, // Ensure it starts from 0
67+
},
68+
},
69+
tooltips: {
70+
callbacks: {
71+
title: function (tooltipItem, data) {
72+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
73+
return data.datasets[0].data[tooltipItem[0].index].label;
74+
},
75+
},
76+
},
77+
},
78+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Product Attributes Visualization</title>
6+
<style>
7+
.test {
8+
background-color: bisque;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.3.3"></script>
14+
15+
<div style="height: 300px">
16+
<canvas id="productChartEuclidean"></canvas>
17+
</div>
18+
<br />
19+
<br />
20+
<div style="height: 300px">
21+
<canvas id="productChartCosine"></canvas>
22+
</div>
23+
24+
<script src="./common.js"></script>
25+
<script src="./euclidean.js"></script>
26+
<script src="./cosine.js"></script>
27+
</body>
28+
</html>
55.1 KB
Loading
76.6 KB
Loading
50.6 KB
Loading
62.9 KB
Loading
67.9 KB
Loading
16.8 KB
Loading

0 commit comments

Comments
 (0)