Cached Content Client - gunpal5/Google_GenerativeAI GitHub Wiki
Introduction
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.
Details
The CachingClient
offers methods for creating, retrieving, listing, updating, and deleting cached content.
CreateCachedContentAsync
)
1. Creating 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}");
}
ListCachedContentsAsync
)
2. Listing Cached Content (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}");
}
GetCachedContentAsync
)
3. Retrieving Cached Content (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}");
}
UpdateCachedContentAsync
)
4. Updating Cached Content (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}");
}
DeleteCachedContentAsync
)
5. Deleting Cached Content (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}");
}
GenerativeModel
Using Cached Content in 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.
Important Considerations
- Resource Names: Use the correct resource names when interacting with cached content (e.g.,
"cachedContents/your-content-id"
). - Pagination: Use
pageSize
andpageToken
withListCachedContentsAsync
to handle large lists of cached content. - Error Handling: Implement proper error handling to manage potential exceptions during API calls.
- Authentication: The
CachingClient
relies on the authentication configured in yourGeminiAI
instance. Ensure yourGeminiConfiguration
is correctly set up. - Update Mask: When updating cached content, use the
updateMask
parameter to specify which fields should be modified. This prevents unintended changes to other fields.