From a4d05573d28bcf68c9ca58f9a36038fcb7f343c9 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 21 Mar 2025 12:23:26 +0000 Subject: [PATCH] Set default similarity for Cohere model to cosine (#125370) Cohere embeddings are expected to be normalized to unit vectors, but due to floating point precision issues, our check ({@link DenseVectorFieldMapper#isNotUnitVector(float)}) often fails. This change fixes this bug by setting the default similarity for newly created Cohere inference endpoint to cosine. Closes #122878 --- docs/changelog/125370.yaml | 6 ++++++ .../inference/services/cohere/CohereService.java | 13 +++++++------ .../services/cohere/CohereServiceTests.java | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 docs/changelog/125370.yaml diff --git a/docs/changelog/125370.yaml b/docs/changelog/125370.yaml new file mode 100644 index 0000000000000..113988089776c --- /dev/null +++ b/docs/changelog/125370.yaml @@ -0,0 +1,6 @@ +pr: 125370 +summary: Set default similarity for Cohere model to cosine +area: Machine Learning +type: bug +issues: + - 122878 diff --git a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/cohere/CohereService.java b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/cohere/CohereService.java index 6c2d3bb96d74d..b32cec54df414 100644 --- a/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/cohere/CohereService.java +++ b/x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/cohere/CohereService.java @@ -14,6 +14,7 @@ import org.elasticsearch.common.util.LazyInitializable; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper; import org.elasticsearch.inference.ChunkedInference; import org.elasticsearch.inference.ChunkingSettings; import org.elasticsearch.inference.InferenceServiceConfiguration; @@ -335,15 +336,15 @@ public Model updateModelWithEmbeddingDetails(Model model, int embeddingSize) { } /** - * Return the default similarity measure for the embedding type. - * Cohere embeddings are normalized to unit vectors therefor Dot - * Product similarity can be used and is the default for all Cohere - * models. + * Returns the default similarity measure for the embedding type. + * Cohere embeddings are expected to be normalized to unit vectors, but due to floating point precision issues, + * our check ({@link DenseVectorFieldMapper#isNotUnitVector(float)}) often fails. + * Therefore, we use cosine similarity to ensure compatibility. * - * @return The default similarity. + * @return The default similarity measure. */ static SimilarityMeasure defaultSimilarity() { - return SimilarityMeasure.DOT_PRODUCT; + return SimilarityMeasure.COSINE; } @Override diff --git a/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/cohere/CohereServiceTests.java b/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/cohere/CohereServiceTests.java index 20785f5bd3742..7916df2536dc7 100644 --- a/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/cohere/CohereServiceTests.java +++ b/x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/cohere/CohereServiceTests.java @@ -1577,7 +1577,7 @@ public void testChunkedInfer_BatchesCalls_Bytes() throws IOException { } public void testDefaultSimilarity() { - assertEquals(SimilarityMeasure.DOT_PRODUCT, CohereService.defaultSimilarity()); + assertEquals(SimilarityMeasure.COSINE, CohereService.defaultSimilarity()); } public void testInfer_StreamRequest() throws Exception {