Skip to content

Instantly share code, notes, and snippets.

@qaixerabbas
Created February 9, 2023 15:48
Show Gist options
  • Save qaixerabbas/84b1172ca25c7f987e669044647e442f to your computer and use it in GitHub Desktop.
Save qaixerabbas/84b1172ca25c7f987e669044647e442f to your computer and use it in GitHub Desktop.
from sentence_transformers import SentenceTransformer, util
import time
model = SentenceTransformer("all-MiniLM-L6-v2")
# Two lists of sentences
sentences1 = [
"I want to know about financial advice about car loan.",
]
sentences2 = [
"How to get car loan financial advice",
]
t1 = time.time()
# Compute embedding
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
embeddings2 = model.encode(sentences2, convert_to_tensor=True)
# cosine-similarities
cosine_scores = util.cos_sim(embeddings1, embeddings2)
pt_cosine = util.pytorch_cos_sim(embeddings1, embeddings2)
t2 = time.time()
print(f"contextual similarity: {cosine_scores}, with time {t2 - t1} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment