Similarity API
The POST https://llmfoundry.straive.com/similarity
API computes pairwise similarity between documents or topics using the dot-product of embeddings. It accepts a JSON body with:
docs
: List of strings or objects containing{type, value}
.type
can betext
orimage
. Value can be the text or a base-64 encoded image. Examples:["King", "Queen"]
[{"type": "text", "value": "King"}, {"type": "text", "value": "Queen"]
[{"type": "image", "value": "data:image/jpeg;base64,..."}, {"type": "image", "value": "data:image/png;base64,..."}, ]
model
: Optional string. Model used for embeddings. Default istext-embedding-3-small
. Can betext-embedding-3-small
(medium accuracy, low cost)text-embedding-3-large
(high accuracy higher cost)text-embedding-ada-002
(low accuracy, medium cost -- avoid)multimodalembedding@001
(for image: JPG, PNG, BMP, GIF, video: AVI, FLV, MKV, MOV, MP4, MPEG, MPG, WEBM, WMV, and text)
topics
: Optional list of strings. Defaults to the same asdocs
, i.e. pairwise similarity between documents.precision
: Optional integer. Defaults to 5. Number of decimal places in the similarity score.
It automatically chunks the input to avoid token limits.
Example:
{
"model": "text-embedding-3-small",
"docs": ["King", "Queen"],
"topics": ["male", "female"],
"precision": 5
}
It returns a JSON object with:
model
: The model usedsimilarity
: A list of lists of pairwise similarity scores between 0-1.- The outer list loops through
docs
. The inner list loops throughtopics
. - Values are between 0-1. Higher values indicate higher similarity.
- For
text-embedding-3-small
andtext-embedding-3-large
, a similarity of 0.45+ means "quite similar". Fortext-embedding-ada-002
, it's 0.85+.
- The outer list loops through
tokens
: The number of tokens used
{
"model": "text-embedding-3-small",
"similarity": [
[0.44537, 0.4027],
[0.39711, 0.4803]
],
"tokens": 4
}
Curl
curl -X POST https://llmfoundry.straive.com/similarity \
-H "Authorization: Bearer $LLMFOUNDRY_TOKEN:my-test-project" \
-H "Content-Type: application/json" \
-d '{"docs": ["King", "Queen"], "topics": ["male", "female"]}'
Python requests
import os
import requests # Or replace requests with httpx
response = requests.post(
"https://llmfoundry.straive.com/similarity",
headers={"Authorization": f"Bearer {os.environ['LLMFOUNDRY_TOKEN']}:my-test-project"},
json={"docs": ["King", "Queen"], "topics": ["male", "female"]}
)
print(response.json())