-
Notifications
You must be signed in to change notification settings - Fork 35
Cached Content Client
The CachingClient provides functionality to manage cached content for Gemini models. This page explains how to use the CachingClient to interact with the Gemini Caching API. You can initialize the CachingClient with a GoogleAi instance.
The CachingClient offers methods for creating, retrieving, listing, updating, and deleting cached content.
This method creates a new cached content resource.
// Initialize GoogleAi (replace with your actual configuration)
var googleAi = new GoogleAi(...);
// Initialize GenerativeAI model
var model = googleAi.CreateGenerativeModel("gemini-1.5-flash);
// Create CachingClient instance
var cachingClient = model.CachingClient;
var cachedContent = new CachedContent { /*... set properties of the cached content... */ };
try
{
var createdContent = await cachingClient.CreateCachedContentAsync(cachedContent);
Console.WriteLine($"Created Cached Content: {createdContent.Name}");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating cached content: {ex.Message}");
}This method retrieves a list of cached content resources. You can use pageSize and pageToken for pagination.
int pageSize = 20; // Optional page size
string? pageToken = null; // Optional page token
try
{
var response = await cachingClient.ListCachedContentsAsync(pageSize, pageToken);
foreach (var content in response.CachedContents)
{
Console.WriteLine($"Cached Content: {content.Name}");
}
if (!string.IsNullOrEmpty(response.NextPageToken))
{
Console.WriteLine($"Next Page Token: {response.NextPageToken}");
// To get the next page:
// pageToken = response.NextPageToken;
// response = await cachingClient.ListCachedContentsAsync(pageSize, pageToken);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error listing cached content: {ex.Message}");
}This method retrieves a specific cached content resource.
string contentName = "cachedContents/your-content-id"; // Replace with the actual content name
try
{
var cachedContent = await cachingClient.GetCachedContentAsync(contentName);
if (cachedContent != null)
{
Console.WriteLine($"Cached Content: {cachedContent.Name}");
}
else
{
Console.WriteLine("Cached content not found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error retrieving cached content: {ex.Message}");
}This method updates an existing cached content resource.
string contentName = "cachedContents/your-content-id"; // Replace with the actual content name
var cachedContent = new CachedContent { /*... set updated properties... */ };
string? updateMask = "expirationTime"; // Optional: specify fields to update
try
{
var updatedContent = await cachingClient.UpdateCachedContentAsync(contentName, cachedContent, updateMask);
if (updatedContent != null)
{
Console.WriteLine($"Updated Cached Content: {updatedContent.Name}");
}
else
{
Console.WriteLine("Cached content not found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error updating cached content: {ex.Message}");
}This method deletes a cached content resource.
string contentName = "cachedContents/your-content-id"; // Replace with the actual content name
try
{
await cachingClient.DeleteCachedContentAsync(contentName);
Console.WriteLine("Cached content deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting cached content: {ex.Message}");
}After you have created or retrieved your cached content, you can assign it to the CachedContent property on a GenerativeModel. This allows the model to utilize cached data when generating responses or content.
Example usage:
// Suppose you have retrieved a CachedContent object named retrievedContent
generativeModel.CachedContent = retrievedContent;
// The GenerativeModel will use the cached content in subsequent operations.-
Resource Names: Use the correct resource names when interacting with cached content (e.g.,
"cachedContents/your-content-id"). -
Pagination: Use
pageSizeandpageTokenwithListCachedContentsAsyncto handle large lists of cached content. - Error Handling: Implement proper error handling to manage potential exceptions during API calls.
-
Authentication: The
CachingClientrelies on the authentication configured in yourGeminiAIinstance. Ensure yourGeminiConfigurationis correctly set up. -
Update Mask: When updating cached content, use the
updateMaskparameter to specify which fields should be modified. This prevents unintended changes to other fields.