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

.Net: Add CachedContent Property to GeminiPromptExecutionSettings #10268

Merged
merged 7 commits into from
Jan 28, 2025

Conversation

davidpene
Copy link
Contributor

Motivation and Context

Why is this change required?
This change introduces a new CachedContent field to the GeminiPromptExecutionSettings class. The addition enables context caching, a feature that optimizes the handling of repeated static content in requests with high input token counts.

What problem does it solve?
Repeatedly processing substantial, static context data (e.g., lengthy documents, audio files, or video files) in requests can be resource-intensive and costly. Context caching addresses this by allowing the reuse of shared context across multiple requests, improving performance and cost-efficiency.

What scenario does it contribute to?
Context caching is particularly well-suited for scenarios where a substantial initial context is repeatedly referenced by shorter requests. Use cases include:

  • Chatbots with extensive system instructions: Allows reuse of complex system configurations across multiple user interactions.
  • Repetitive analysis of lengthy video files: Enables efficient analysis by caching video-related metadata or transcriptions.
  • Recurring queries against large document sets: Improves efficiency for workflows requiring repeated access to large document collections.
  • Frequent code repository analysis or bug fixing: Facilitates the reuse of large codebases as cached context for debugging or analysis tasks.

No open issues are linked to this change.


Description

This PR adds a CachedContent field to the GeminiPromptExecutionSettings class. The field allows users to reference cached context items such as text blocks, audio files, or video files in prompt requests.

Key features:

  • The minimum size of a context cache is 32,768 tokens.
  • Cached content is retained for a default duration of 60 minutes, with the option to configure expiration.
  • Cached content is billed efficiently: the initial creation call is charged at the standard rate, while subsequent references to the cache are billed at a reduced rate.

By enabling context caching, users can optimize cost and resource usage for workflows requiring substantial and repeat context data.


Contribution Checklist


Demo

Screenshot 2025-01-23 at 11 46 17 PM Screenshot 2025-01-23 at 11 50 29 PM

Notes

  1. The CachedContent is only available via v1beta1 endpoint
  2. Overview of context caching in Gemini
  3. cachedContent field in the body for the v1beta1 endpoint

@davidpene davidpene requested a review from a team as a code owner January 23, 2025 10:57
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Jan 23, 2025
@davidpene
Copy link
Contributor Author

@microsoft-github-policy-service agree company="Carepatron"

@davidpene
Copy link
Contributor Author

@RogerBarreto / @markwallace-microsoft / @dmytrostruk would love to hear your thoughts.

@RogerBarreto
Copy link
Member

@davidpene Thanks very much for the contribution, suggest adding missing UnitTests ensuring the new request property is behaving correctly when serializing, deserializing and going over the http, suggest adding an integration test ensuring it as well (keep the integration test Skipped for the project but ensure it works as expected when runningh locally)

For reference check:

https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Connectors/Connectors.Google.UnitTests/Core/Gemini/GeminiRequestTests.cs

@RogerBarreto RogerBarreto self-assigned this Jan 27, 2025
@davidpene
Copy link
Contributor Author

Appreciate the feedback @RogerBarreto - all done ✅
Let me know if there's anything else.

@RogerBarreto RogerBarreto added this pull request to the merge queue Jan 28, 2025
Merged via the queue into microsoft:main with commit 6fbbb44 Jan 28, 2025
17 checks passed
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 .NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants