Created
February 22, 2024 07:47
-
-
Save robert-mcdermott/5957ef1ddcfc7c3ba898d800531b2aa7 to your computer and use it in GitHub Desktop.
ollama embeddings cosine similarity and dot product example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install scikit-learn numpy ollama | |
import ollama | |
import numpy as np | |
from sklearn.metrics.pairwise import cosine_similarity | |
text1 = ollama.embeddings(model='nomic-embed-text', prompt='The sky is blue because of rayleigh scattering') | |
text2 = ollama.embeddings(model='nomic-embed-text', prompt='The sky is cloudy and grey today') | |
vec1 = np.array(text1['embedding']).reshape(1, -1) | |
vec2 = np.array(text2['embedding']).reshape(1, -1) | |
similarity = cosine_similarity(vec1, vec2) | |
print(similarity) | |
# same thing but with out needing scikit-learn | |
vec1 = np.array(text1['embedding']) | |
vec2 = np.array(text2['embedding']) | |
dot_product = np.dot(vec1, vec2) | |
norm_vec1 = np.linalg.norm(vec1) | |
norm_vec2 = np.linalg.norm(vec2) | |
cosine_similarity = dot_product / (norm_vec1 * norm_vec2) | |
print(cosine_similarity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment