Can one get an embeddings from an inference API that computes Sentence Similarity?

Hi there,

I’m new to using Huggingface’s inference API and wanted to check if a model whose task is to return Sentence Similarity can return sentence embeddings instead.

For example, in this sentence-transformers model, the model task is to return sentence similarity. Instead, I would like to just get the embeddings of a list of sentences.

Is there an API parameter I can tweak to get this? Help would be very much appreciated! :slight_smile:

2 Likes

Hi there!

Yes, you can compute sentence embeddings. Here is an example

import requests

API_URL = "https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-mpnet-base-v2"
headers = {"Authorization": "Bearer API_TOKEN"}

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

output = query({
    "inputs": ["this is a sentence", "this is another sentence"]
})
# Output is a list of 2 embeddings, each of 768 values.
2 Likes

Can you use this endpoint
to do image embedding?
IE: using the model sentence-transformers/clip-ViT-B-32-multilingual-v1

https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/**sentence-transformers/clip-ViT-B-32-multilingual-v1**

If so, how do you pass the images? via base64encode?

I’m doing it passing base64encode images in the “inputs” parameter and it doesn’t seem to get the image meaning content right…

body:

{
“inputs”: ,
“options”: {
“wait_for_model”: true
}
}

Thanks in advance!

1 Like

@osanseviero Does this still work?

It was working until recently, now I get the error {'error': ['SentenceSimilarityInputsCheck expected dict not list: rootinparameters']}.

Thanks in advance!

Hi there! Do you have an example of the request you’re doing? I just tried one as above and it is working well.

Nevermind! I missed the pipeline/feature-extraction/ part of the URL. Thanks a ton!

Since it looks like the main difference here is the addition of pipeline/feature-extraction in the url, how would you replicate this approach on a deployed inference endpoint?

– i.e.
“https:some-custom-key.region.aws.endpoints.huggingface.cloud”

@Jonp1

When set up the deployed inference endpoint you can choose the Task. To get the text embeddings use Sentence Embeddings

1 Like

Just Thank you dude, i was searching for this since last hour.
THANK YOU VERY MUCH