can I use api to get the embeddings?

#72
by starmaq - opened

rn it seems like the api can just return the similarity score, not the embeddings

Hi there, this example in the Sentence-transformers official documentation may be helpful for you.

from sentence_transformers import SentenceTransformer

# 1. Load a pretrained Sentence Transformer model
model = SentenceTransformer("all-MiniLM-L6-v2")

# The sentences to encode
sentences = [
    "The weather is lovely today.",
    "It's so sunny outside!",
    "He drove to the stadium.",
]

# 2. Calculate embeddings by calling model.encode()
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 384]

# 3. Calculate the embedding similarities
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[1.0000, 0.6660, 0.1046],
#         [0.6660, 1.0000, 0.1411],
#         [0.1046, 0.1411, 1.0000]])

I have the same request: use the API to encode the embeddings. Here, the solution computes similarities.
To be more specific, i would like that, in the provided example https://huggingface.co./sentence-transformers/all-MiniLM-L6-v2/discussions/72?inference_api=true, we call request.post(API_URL, headers=headers, json=payload) where payload is a simple text. The expected result would be a list of vectors like model.encode(sentences) would do.

@starmaq , @nicolasec :

Just started using this API and had the same question and found this to work. The api link needs to contain "/pipeline/feature-extraction"


import requests

hf_token = "< you hf token>"

API_URL = "https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2"
headers = {"Authorization": f"Bearer {hf_token}"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

output = query({
"inputs": "The big brown dog"})

print(output)

Looks like it is working! Could you explain where you have find the documentation explaining we have to use this API_URL? It would be useful if we have similar needs in the future.
Thank you.

Sign up or log in to comment