use embedding model in python
·
export HF_ENDPOINT=https://hf-mirror.com
import os
# Set the HF endpoint for this Python process
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com" # this is useless, still need to export HF_ENDPOINT
from sentence_transformers import SentenceTransformer, util
# 1. Load a model (compact and fast)
#model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
#model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
#model = SentenceTransformer('paraphrase-MiniLM-L6-v2', trust_remote_code=True)
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
# 2. Two sentences
s1 = "I love going to the beach."
s2 = "I enjoy walking along the seaside."
# 3. Compute embeddings
emb1 = model.encode(s1, convert_to_tensor=True)
emb2 = model.encode(s2, convert_to_tensor=True)
# 4. Compute cosine similarity
similarity = util.cos_sim(emb1, emb2)
print("Similarity:", similarity.item())
python a.py
更多推荐
所有评论(0)