Model Client - gunpal5/Google_GenerativeAI GitHub Wiki

Introduction

The ModelClient provides functionality to retrieve information about available Gemini models. This page explains how to use the ModelClient to interact with the Gemini Models API.

Details

The ModelClient offers methods for retrieving metadata about specific models and listing all available models. You can initialize the ModelClient with either a GeminiAI or VertexAI platform object.


1. Retrieving Model Information (GetModelAsync)

This method retrieves information about a specific model, such as its version, token limits, parameters, and other metadata.

// Assuming you have an instance of ModelClient called 'modelClient'
string modelName = "models/your-model-name"; // Replace with the actual model name

try
{
    var model = await modelClient.GetModelAsync(modelName);
    Console.WriteLine($"Model Name: {model.Name}, Version: {model.Version}"); // Access other properties as needed
}
catch (Exception ex)
{
    Console.WriteLine($"Error retrieving model information: {ex.Message}");
}



2. Listing Available Models (ListModelsAsync)

This method lists the models available through the Gemini API. You can use pageSize and pageToken for pagination.

// Assuming you have an instance of ModelClient called 'modelClient'
try
{
    var response = await modelClient.ListModelsAsync(pageSize: 20); // Optional page size
    foreach (var model in response.Models)
    {
        Console.WriteLine($"Model Name: {model.Name}, Display Name: {model.DisplayName}"); 
        // Access other properties as needed
    }

    if (!string.IsNullOrEmpty(response.NextPageToken))
    {
        // Use response.NextPageToken to retrieve the next page of results
        Console.WriteLine($"Next Page Token: {response.NextPageToken}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error listing models: {ex.Message}");
}

// Example using GeminiAI platform:
// var modelClient = new ModelClient(new GeminiAI(...));

// Example using VertexAI platform:
// var modelClient = new ModelClient(new VertexAI(...));

Example with GoogleAi


var googleAI = new GoogleAi(...);

try
{
    var response = await googleAI.ListModelAsync(pageSize: 20); // Potential usage snippet
    // Or:
    // var response = await modelClient.ListModelsAsync(pageSize: 20);

    foreach (var model in response.Models)
    {
        Console.WriteLine($"Model Name: {model.Name}, Display Name: {model.DisplayName}");
    }

    if (!string.IsNullOrEmpty(response.NextPageToken))
    {
        Console.WriteLine($"Next Page Token: {response.NextPageToken}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error listing models: {ex.Message}");
}

Important Considerations

  1. Model Names: Use the correct model names when calling GetModelAsync. These names have a specific format (e.g., "models/your-model-name").
  2. Pagination: Use pageSize and pageToken with ListModelsAsync to handle large lists of models efficiently.
  3. Error Handling: Implement proper error handling to manage potential exceptions during API calls.
  4. Authentication: The ModelClient relies on the same authentication mechanism as other Gemini API clients. Make sure you have configured authentication correctly using either GeminiAI or VertexAI platform.

API Reference