Class TuningClient
- Namespace
- GenerativeAI.Clients
- Assembly
- GenerativeAI.dll
A client for model tuning, mirroring client.tunings of the google-genai SDK.
public class TuningClient : BaseClient
- Inheritance
-
TuningClient
- Inherited Members
- Extension Methods
Remarks
On Vertex AI it uses the projects/{project}/locations/{location}/tuningJobs resource and supports
supervised fine tuning, preference tuning, distillation and reinforcement tuning, listing, cancellation
and reward validation.
On the Gemini Developer API it uses the tunedModels resource: TuneAsync(string, TuningDataset, CreateTuningJobConfig?, CancellationToken),
GetAsync(string, CancellationToken), CancelAsync(string, CancellationToken) and
WaitForCompletionAsync(string, TimeSpan?, TimeSpan?, CancellationToken) are supported, and tuned models are reported as TuningJobs.
Constructors
TuningClient(IPlatformAdapter, HttpClient?, ILogger?)
Initializes a new instance of the TuningClient class.
public TuningClient(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)
Parameters
platformIPlatformAdapterThe platform adapter used to manage API endpoints and authentication.
httpClientHttpClientThe HTTP client instance used for making API requests.
loggerILoggerAn optional logger instance for logging operations.
Methods
CancelAsync(string, CancellationToken)
Cancels a tuning job.
public Task CancelAsync(string name, CancellationToken cancellationToken = default)
Parameters
namestringThe tuning job (Vertex AI) or tuned model (Gemini API) name.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task
A task that completes when the cancellation request was accepted.
GetAsync(string, CancellationToken)
Gets a tuning job (Vertex AI) or a tuned model reported as a tuning job (Gemini API).
public Task<TuningJob> GetAsync(string name, CancellationToken cancellationToken = default)
Parameters
namestringThe tuning job resource name (
projects/.../tuningJobs/...,tuningJobs/...or the job id) on Vertex AI, or the tuned model name (tunedModels/...or the id) on the Gemini API.cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
ListAllAsync(int?, string?, CancellationToken)
Lists all tuning jobs, fetching further pages as needed (Vertex AI only).
public IAsyncEnumerable<TuningJob> ListAllAsync(int? pageSize = null, string? filter = null, CancellationToken cancellationToken = default)
Parameters
pageSizeint?The page size used for each request.
filterstringAn optional list filter.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- IAsyncEnumerable<TuningJob>
An async sequence of tuning jobs.
ListAsync(int?, string?, string?, CancellationToken)
Lists tuning jobs (Vertex AI only).
public Task<ListTuningJobsResponse> ListAsync(int? pageSize = null, string? pageToken = null, string? filter = null, CancellationToken cancellationToken = default)
Parameters
pageSizeint?The maximum number of tuning jobs to return.
pageTokenstringA page token from a previous call.
filterstringAn optional list filter.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<ListTuningJobsResponse>
A page of tuning jobs.
Exceptions
- NotSupportedException
Called on the Gemini Developer API.
TuneAsync(string, TuningDataset, CreateTuningJobConfig?, CancellationToken)
Creates a tuning job (Vertex AI) or a tuned model (Gemini API) and returns it as a TuningJob.
public Task<TuningJob> TuneAsync(string baseModel, TuningDataset trainingDataset, CreateTuningJobConfig? config = null, CancellationToken cancellationToken = default)
Parameters
baseModelstringThe model to tune. On Vertex AI, a tuned model resource name (
projects/.../models/...) continues tuning from that model (with PreTunedModelCheckpointId as checkpoint).trainingDatasetTuningDatasetThe training dataset.
configCreateTuningJobConfigOptional tuning configuration.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<TuningJob>
The created TuningJob. On the Gemini API only Name (the tuned model name) and State (JOB_STATE_QUEUED) are set.
Exceptions
- NotSupportedException
An option is not supported on the current platform.
ValidateRewardAsync(Content, ReinforcementTuningExample, SingleReinforcementTuningRewardConfig?, CompositeReinforcementTuningRewardConfig?, string?, CancellationToken)
Validates a reinforcement tuning reward configuration against a sample response and example, without creating a tuning job (Vertex AI only).
public Task<ValidateRewardResponse> ValidateRewardAsync(Content sampleResponse, ReinforcementTuningExample example, SingleReinforcementTuningRewardConfig? singleRewardConfig = null, CompositeReinforcementTuningRewardConfig? compositeRewardConfig = null, string? parent = null, CancellationToken cancellationToken = default)
Parameters
sampleResponseContentThe sample model response to score.
exampleReinforcementTuningExampleThe example (prompt and references) the response belongs to.
singleRewardConfigSingleReinforcementTuningRewardConfigA single reward configuration. Mutually exclusive with
compositeRewardConfig.compositeRewardConfigCompositeReinforcementTuningRewardConfigA composite reward configuration. Mutually exclusive with
singleRewardConfig.parentstringThe location to validate in,
projects/{project}/locations/{location}. Defaults to the platform's project and location.cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
- Task<ValidateRewardResponse>
The computed reward(s).
Exceptions
- NotSupportedException
Called on the Gemini Developer API.
WaitForCompletionAsync(string, TimeSpan?, TimeSpan?, CancellationToken)
Polls a tuning job until it reaches a terminal state (succeeded, partially succeeded, failed, cancelled or expired).
public Task<TuningJob> WaitForCompletionAsync(string name, TimeSpan? pollInterval = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
namestringThe tuning job (Vertex AI) or tuned model (Gemini API) name.
pollIntervalTimeSpan?The delay between polls. Defaults to 30 seconds.
timeoutTimeSpan?The maximum time to wait. Defaults to no limit.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
Exceptions
- TimeoutException
The job did not complete within
timeout.