Skip to content

.Net: Support records with no key, fix issue with Relevance #6393

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

Merged
merged 9 commits into from
Jun 12, 2024

Conversation

Pilchie
Copy link
Member

@Pilchie Pilchie commented May 24, 2024

Motivation and Context

When using SemanticTextMemory.SaveReferenceAsync, no value is passed for the Key to use. In the Cosmos DB Memory Connector, we treat the "Key" as the Id and PartitionKey, and so it has to have a value. Address this by generating a GUID to use if none are provided.

Description

  • Assign a GUID key when the passed in MemoryRecord.Key is null or empty so that CosmosDB always has a valid PartitionKey.
  • While adding test coverage for this I noticed two things:
  1. withEmbeddings is ignored in AzureCosmosDBNoSQLMemoryStore.GetAsync. It's not clear whether it's worth changing this, as point reads like the current one tend to be less expensive than queries in Cosmos DB.
  2. The SimilarityScore returned from GetNearestAsync wasn't actually a similarity, it was a cosine distance. Normalize that.

Fixes #6379.

Contribution Checklist

@Pilchie Pilchie requested a review from a team as a code owner May 24, 2024 21:43
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel memory labels May 24, 2024
@github-actions github-actions bot changed the title Support records with no key, fix issue with Relevance .Net: Support records with no key, fix issue with Relevance May 24, 2024
@Pilchie Pilchie force-pushed the 6379-SaveReferenceAsync branch from 629bf78 to af4248e Compare June 10, 2024 22:15
@Pilchie
Copy link
Member Author

Pilchie commented Jun 10, 2024

@dmytrostruk - can you take another look here? I restricted the provider to only use Cosine distance in order to be able to calculate the 0..1 relevance score.

Pilchie and others added 3 commits June 10, 2024 16:46
Copy link
Member

@dmytrostruk dmytrostruk left a comment

Choose a reason for hiding this comment

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

@dmytrostruk dmytrostruk enabled auto-merge June 12, 2024 20:15
@dmytrostruk dmytrostruk added this pull request to the merge queue Jun 12, 2024
Merged via the queue into microsoft:main with commit 4adf3fe Jun 12, 2024
15 checks passed
LudoCorporateShark pushed a commit to LudoCorporateShark/semantic-kernel that referenced this pull request Aug 25, 2024
…t#6393)

### Motivation and Context

When using SemanticTextMemory.SaveReferenceAsync, no value is passed for
the `Key` to use. In the Cosmos DB Memory Connector, we treat the "Key"
as the Id and PartitionKey, and so it has to have a value. Address this
by generating a `GUID` to use if none are provided.

### Description

* Assign a GUID key when the passed in `MemoryRecord.Key` is null or
empty so that CosmosDB always has a valid PartitionKey.
* While adding test coverage for this I noticed two things:
1. `withEmbeddings` is ignored in
`AzureCosmosDBNoSQLMemoryStore.GetAsync`. It's not clear whether it's
worth changing this, as point reads like the current one tend to be less
expensive than queries in Cosmos DB.
2. The SimilarityScore returned from `GetNearestAsync` wasn't actually a
similarity, it was a cosine distance. Normalize that.

Fixes microsoft#6379.

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel Issues or pull requests impacting the core kernel memory .NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error while saving to Semantic Memory using NoSQL
4 participants