can I use api to get the embeddings?
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.
@nicolasec : This documentation on feature extraction may help: https://huggingface.co/tasks/feature-extraction
Also this form explains a similar question: https://discuss.huggingface.co/t/can-one-get-an-embeddings-from-an-inference-api-that-computes-sentence-similarity/9433/2