Skip to content

Commit 1aa9b15

Browse files
committed
adding search applications app
1 parent d070296 commit 1aa9b15

File tree

3 files changed

+382
-0
lines changed

3 files changed

+382
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import ModelClient from "@azure-rest/ai-inference";
2+
import { isUnexpected } from "@azure-rest/ai-inference";
3+
import { AzureKeyCredential } from "@azure/core-auth";
4+
import { brotliDecompress } from "zlib";
5+
6+
const token = process.env["GITHUB_TOKEN"];
7+
const endpoint = "https://models.inference.ai.azure.com";
8+
9+
/* By using the Azure AI Inference SDK, you can easily experiment with different models
10+
by modifying the value of `modelName` in the code below. For this code sample
11+
you need an embedding model. The following embedding models are
12+
available in the GitHub Models service:
13+
14+
Cohere: Cohere-embed-v3-english, Cohere-embed-v3-multilingual
15+
Azure OpenAI: text-embedding-3-small, text-embedding-3-large */
16+
const modelName = "text-embedding-3-small";
17+
18+
function cosineSimilarity(vector1, vector2) {
19+
if (vector1.length !== vector2.length) {
20+
throw new Error("Vector dimensions must match for cosine similarity calculation.");
21+
}
22+
23+
const dotProduct = vector1.reduce((acc, val, index) => acc + val * vector2[index], 0);
24+
const magnitude1 = Math.sqrt(vector1.reduce((acc, val) => acc + val ** 2, 0));
25+
const magnitude2 = Math.sqrt(vector2.reduce((acc, val) => acc + val ** 2, 0));
26+
27+
if (magnitude1 === 0 || magnitude2 === 0) {
28+
throw new Error("Magnitude of a vector must be non-zero for cosine similarity calculation.");
29+
}
30+
31+
return dotProduct / (magnitude1 * magnitude2);
32+
}
33+
34+
35+
36+
export async function main() {
37+
let carEmbedding, vehicleEmbedding, birdEmbedding
38+
39+
const client = new ModelClient(endpoint, new AzureKeyCredential(token));
40+
41+
const response = await client.path("/embeddings").post({
42+
body: {
43+
input: ["Car", "Vehicle", "Bird"],
44+
model: modelName
45+
}
46+
});
47+
48+
if (isUnexpected(response)) {
49+
throw response.body.error;
50+
}
51+
52+
for (const item of response.body.data) {
53+
const { embedding, index } = item; // Destructure item for cleaner code
54+
const length = embedding.length;
55+
56+
switch (index) {
57+
case 0:
58+
carEmbedding = embedding;
59+
break;
60+
case 1:
61+
vehicleEmbedding = embedding;
62+
break;
63+
case 2:
64+
birdEmbedding = embedding;
65+
break;
66+
}
67+
68+
console.log(
69+
`data[${item.index}]: length=${length}, ` +
70+
`[${item.embedding[0]}, ${item.embedding[1]}, ` +
71+
`..., ${item.embedding[length - 2]}, ${item.embedding[length - 1]}]`);
72+
73+
74+
}
75+
76+
console.log(response.body.usage);
77+
console.log(carEmbedding)
78+
const scoreCarWithVehicle = cosineSimilarity(carEmbedding, vehicleEmbedding);
79+
console.log("Comparing - Car vs Vehicle...: ", scoreCarWithVehicle.toFixed(7));
80+
81+
82+
const scoreCarWithBird = cosineSimilarity(carEmbedding, birdEmbedding);
83+
console.log("Comparing - Car vs Bird...: ", scoreCarWithBird.toFixed(7));
84+
85+
}
86+
87+
main().catch((err) => {
88+
console.error("The sample encountered an error:", err);
89+
});

08-building-search-applications/js-githubmodels/package-lock.json

+285
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"@azure-rest/ai-inference": "latest",
5+
"@azure/core-auth": "latest",
6+
"@azure/core-sse": "latest"
7+
}
8+
}

0 commit comments

Comments
 (0)