OER·harvester

← Back to the library
GitHub NOTEBOOK resource

Aoai Assignment

Repository 21 Lessons, Get Started Building with Generative AI

Licence
OPEN MIT
Authors
Microsoft (microsoft)
Updated
2026-07-16 · GitHub
Language
en
Length
123 words
Open original ↗
{ }
Jupyter notebook Converted to a read-only view · code is not executed

Source: Aoai Assignment · GitHub · microsoft/generative-ai-for-beginners Authors: Microsoft (microsoft) Licence: MIT — https://spdx.org/licenses/MIT.html

In [1]
%pip install openai python-dotenv
In [2]
import os
from openai import AzureOpenAI
import numpy as np
from dotenv import load_dotenv
load_dotenv()

client = AzureOpenAI(
  api_key = os.getenv("AZURE_OPENAI_API_KEY"),  
  api_version = "2024-10-21",
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
)
In [3]
# Dependencies for embeddings_utils
%pip install matplotlib plotly scikit-learn pandas
In [4]
def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
In [5]
text = 'the quick brown fox jumped over the lazy dog'
model = os.getenv("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT", "").strip()
assert model, "AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT environment variable must be set and non-empty"
client.embeddings.create(input = [text], model=model).data[0].embedding
In [6]
# compare several words
automobile_embedding    = client.embeddings.create(input = 'automobile', model=model).data[0].embedding
vehicle_embedding       = client.embeddings.create(input = 'vehicle', model=model).data[0].embedding
dinosaur_embedding      = client.embeddings.create(input = 'dinosaur', model=model).data[0].embedding
stick_embedding         = client.embeddings.create(input = 'stick', model=model).data[0].embedding

# comparing cosine similarity, automobiles vs automobiles should be 1.0, i.e exactly the same, while automobiles vs dinosaurs should be between 0 and 1, i.e. not the same
print(cosine_similarity(automobile_embedding, automobile_embedding))
print(cosine_similarity(automobile_embedding, vehicle_embedding))
print(cosine_similarity(automobile_embedding, dinosaur_embedding))
print(cosine_similarity(automobile_embedding, stick_embedding))

Disclaimer: This document has been translated using AI translation service Co-op Translator. While we strive for accuracy, please be aware that automated translations may contain errors or inaccuracies. The original document in its native language should be considered the authoritative source. For critical information, professional human translation is recommended. We are not liable for any misunderstandings or misinterpretations arising from the use of this translation.