Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with elasticsearch extension to respect limit and minReleva… #935

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

basyonic
Copy link

…nce parameters when calling GetSimilarListAsync API

Motivation and Context (Why the change? What's the scenario?)

Fixing issue #934

High level description (Approach, Design)

using ElasticSearch Client .Size(limit) clause during query.
using .Similarity(min score) to fix minRelevance (after adjusting the score value based on cosine similarity as per https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-similarity-search)

…nce parameters when calling GetSimilarListAsync API
@basyonic
Copy link
Author

@microsoft-github-policy-service agree

@@ -218,15 +218,20 @@ public async Task<string> UpsertAsync(
Embedding embedding = await this._embeddingGenerator.GenerateEmbeddingAsync(text, cancellationToken).ConfigureAwait(false);
var coll = embedding.Data.ToArray();

//adjust min score for cosine similarity
//https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#knn-similarity-search
float adjustedMinSimilarityScore = (float)(2 * minRelevance - 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formula transforms from score to cosine similarity, however internally ES works with score.

I believe this should be the inverse, e.g. float adjustedMinSimilarityScore = (float)((minRelevance + 1) / 2)

By the way, the formula (2 * minRelevance - 1) should be used in line 253, replacing hit.Score with the actual cosine similarity. The value returned is incorrect and doesn't match the similarity returned by other memory connectors.

var resp = await this._client.SearchAsync<ElasticsearchMemoryRecord>(s =>
s.Index(index)
s.Index(index).Size(limit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qd.k(limit) already sets a limit, isn't this redundant? should we remove the limit param in qd.k(limit)?

Comment on lines +66 to +67
maxRelevance = partitions.Max(p => p.Relevance);
minRelevance = partitions.Min(p => p.Relevance);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 vars are not used. Missing some assertions?

//relevance 0.82:
results = await this.KernelMemory.SearchAsync("What is Silicon?", index: indexName, null, null, minRelevance: 0.82, limit: 10);
partitions = results.Results[0].Partitions;
maxRelevance = partitions.Max(p => p.Relevance);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The var is not used. Missing an assertion?

@dluc dluc added the waiting for author Waiting for author to reply or address comments label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author Waiting for author to reply or address comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants