Is there a way to get Cosine Similarity score returned?

Hi! I was wondering if there is a way to get the actual cosine similarity score/value returned by the api? So I have a code snippet where I fetch all embeddings and do a sort & filter on the cosineSimilarity. But I noticed I can do greaterThan at the filter, which is already nice. But when for example returning 3 items, we know they are all above 0.7, But I would like to determine things based on what similarity score they got. So for that I would need the score they got returned.
const embeddings = await api.embeddings.findMany({
sort: {
embedding: {
cosineSimilarityTo: embedding,
},
},
filter: {
shopId: { equals: shopId },
embedding: {
cosineSimilarity: {
greaterThan: 0.7,
to: embedding,
}
}
},
first: 3,
select: {
answer: true,
}
});
const embeddings = await api.embeddings.findMany({
sort: {
embedding: {
cosineSimilarityTo: embedding,
},
},
filter: {
shopId: { equals: shopId },
embedding: {
cosineSimilarity: {
greaterThan: 0.7,
to: embedding,
}
}
},
first: 3,
select: {
answer: true,
}
});
I thought it was in the select and than embeddingCosineSimilarityTo: true, while this is an option in the SDK, it throws. Kind regards, Dane
3 Replies
Chocci_Milk
Chocci_Milk2w ago
Hello, There's currently no way for you to be returned the value of the similarity. I'm not sure what kind of priority we would put to this but it definitely could be useful. Could you please make a #feature-requests ticket for this?
[Gadget] Kyle
[Gadget] Kyle2w ago
we actually do expose this on the graphql api (however it does not work on the js api client (going to file a bug to fix)) if I have a model called myModel with a vector field called vec
{
myModel(id: "1") {
id
vec
vecCosineSimilarityTo(vector: [1,2,3])
}
}
{
myModel(id: "1") {
id
vec
vecCosineSimilarityTo(vector: [1,2,3])
}
}
you will get the cosine similarity if you don't want to drop down to the graphql api you could also implement this yourself using i.e. https://www.npmjs.com/package/compute-cosine-similarity (or just rolling your own, it's a pretty easy calculation)
Käse
KäseOP5d ago
For the meantime I’ve rolled my own, thanks for the clarification guys!

Did you find this page helpful?