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>"
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: /static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcan-one-get-an-embeddings-from-an-inference-api-that-computes-sentence-similarity%2F9433%2F2%3C%2Fa%3E%3C%2Fp%3E
@incognitoluke I'm trying to add the API on Ragflow but keep getting the error : Fail to access embedding model(sentence-transformers/all-MiniLM-L6-v2___HuggingFace).Error: 429 - {"error":"Please log in or use a HF access token"}