Skip to content

Use SHA384 and make key more explicit #6237

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

Conversation

SteveSandersonMS
Copy link
Member

@SteveSandersonMS SteveSandersonMS commented Apr 3, 2025

This PR includes the changes from #6236 and also changes the GetCacheKey method signature so that it explicitly takes messages/options params:

-    protected abstract string GetCacheKey(params ReadOnlySpan<object?> values);
+    protected abstract string GetCacheKey(IEnumerable<ChatMessage> messages, ChatOptions? options, params ReadOnlySpan<object?> additionalValues);

We don't strictly have to do this. However it can be beneficial for people who want to customize the cache key computation to include other data (e.g., from AdditionalProperties from within messages or options). Previously they would have to just somehow know that one of the incoming values will be the data they need, and would need to do runtime type checking to find the relevant one, and would have to hope that we don't change the incoming data shape in the future (e.g., pre-JSON-serializing the values).

The more explicit signature in this PR would be a guarantee that we will in fact pass in these specific IEnumerable<ChatMessage>/ChatOptions? types. I don't think there's much drawback to us offering that guarantee, since this is really baked into the IChatClient contract anyway - those are exactly the data items that middleware has access to.

Microsoft Reviewers: Open in CodeFlow

Hash algorithm

I considered also making the hash algorithm configurable (defaulting to SHA384). However that complicates things because currently we use a thread-static IncrementalHashStream that holds the IncrementalHash instance. We'd have to create new instances when different hash algorithms are specified.

Since we've been told that in this case, SHA384 is sufficient without being configurable, I'd be happy to leave it at that.

The point of this is to make it easier and more reliable for people overriding the logic to obtain specific values from messages/options to include in cache key without having to search through an opaque list of `object` values.
@stephentoub
Copy link
Member

@shyamnamboodiripad, @peterwald, fyi

@SteveSandersonMS SteveSandersonMS merged commit 65d3d0e into main Apr 3, 2025
7 checks passed
@SteveSandersonMS SteveSandersonMS deleted the stevesa/use-sha384-and-make-key-more-explicit branch April 3, 2025 12:21
joperezr pushed a commit to joperezr/extensions that referenced this pull request Apr 8, 2025
* Minimal change in AIJsonUtilities from SHA256 to SHA384

* Update GetCacheKey API to have explicit messages/options params

The point of this is to make it easier and more reliable for people overriding the logic to obtain specific values from messages/options to include in cache key without having to search through an opaque list of `object` values.
@github-actions github-actions bot locked and limited conversation to collaborators May 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-ai Microsoft.Extensions.AI libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants