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.


1. Creating Cached Content (CreateCachedContentAsync)

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}");
}

2. Listing Cached Content (ListCachedContentsAsync)

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}");
}

3. Retrieving Cached Content (GetCachedContentAsync)

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}");
}

4. Updating Cached Content (UpdateCachedContentAsync)

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}");
}

5. Deleting Cached Content (DeleteCachedContentAsync)

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}");
}

Using Cached Content in GenerativeModel

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

  1. Resource Names: Use the correct resource names when interacting with cached content (e.g., "cachedContents/your-content-id").
  2. Pagination: Use pageSize and pageToken with ListCachedContentsAsync to handle large lists of cached content.
  3. Error Handling: Implement proper error handling to manage potential exceptions during API calls.
  4. Authentication: The CachingClient relies on the authentication configured in your GeminiAI instance. Ensure your GeminiConfiguration is correctly set up.
  5. Update Mask: When updating cached content, use the updateMask parameter to specify which fields should be modified. This prevents unintended changes to other fields.

API Reference