Skip to content

Commit

Permalink
adding search applications app
Browse files Browse the repository at this point in the history
  • Loading branch information
koreyspace committed Sep 17, 2024
1 parent d070296 commit 1aa9b15
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 0 deletions.
89 changes: 89 additions & 0 deletions 08-building-search-applications/js-githubmodels/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
import { brotliDecompress } from "zlib";

const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.inference.ai.azure.com";

/* By using the Azure AI Inference SDK, you can easily experiment with different models
by modifying the value of `modelName` in the code below. For this code sample
you need an embedding model. The following embedding models are
available in the GitHub Models service:
Cohere: Cohere-embed-v3-english, Cohere-embed-v3-multilingual
Azure OpenAI: text-embedding-3-small, text-embedding-3-large */
const modelName = "text-embedding-3-small";

function cosineSimilarity(vector1, vector2) {
if (vector1.length !== vector2.length) {
throw new Error("Vector dimensions must match for cosine similarity calculation.");
}

const dotProduct = vector1.reduce((acc, val, index) => acc + val * vector2[index], 0);
const magnitude1 = Math.sqrt(vector1.reduce((acc, val) => acc + val ** 2, 0));
const magnitude2 = Math.sqrt(vector2.reduce((acc, val) => acc + val ** 2, 0));

if (magnitude1 === 0 || magnitude2 === 0) {
throw new Error("Magnitude of a vector must be non-zero for cosine similarity calculation.");
}

return dotProduct / (magnitude1 * magnitude2);
}



export async function main() {
let carEmbedding, vehicleEmbedding, birdEmbedding

const client = new ModelClient(endpoint, new AzureKeyCredential(token));

const response = await client.path("/embeddings").post({
body: {
input: ["Car", "Vehicle", "Bird"],
model: modelName
}
});

if (isUnexpected(response)) {
throw response.body.error;
}

for (const item of response.body.data) {
const { embedding, index } = item; // Destructure item for cleaner code
const length = embedding.length;

switch (index) {
case 0:
carEmbedding = embedding;
break;
case 1:
vehicleEmbedding = embedding;
break;
case 2:
birdEmbedding = embedding;
break;
}

console.log(
`data[${item.index}]: length=${length}, ` +
`[${item.embedding[0]}, ${item.embedding[1]}, ` +
`..., ${item.embedding[length - 2]}, ${item.embedding[length - 1]}]`);


}

console.log(response.body.usage);
console.log(carEmbedding)
const scoreCarWithVehicle = cosineSimilarity(carEmbedding, vehicleEmbedding);
console.log("Comparing - Car vs Vehicle...: ", scoreCarWithVehicle.toFixed(7));


const scoreCarWithBird = cosineSimilarity(carEmbedding, birdEmbedding);
console.log("Comparing - Car vs Bird...: ", scoreCarWithBird.toFixed(7));

}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
285 changes: 285 additions & 0 deletions 08-building-search-applications/js-githubmodels/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 08-building-search-applications/js-githubmodels/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"@azure-rest/ai-inference": "latest",
"@azure/core-auth": "latest",
"@azure/core-sse": "latest"
}
}

0 comments on commit 1aa9b15

Please sign in to comment.