Skip to content

Commit

Permalink
Broken: getting to azureFineTuningJob
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail committed Feb 27, 2025
1 parent bef5bc6 commit c1cff5b
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text.Json;
using System.Linq;
using Azure.AI.OpenAI.Utility;

namespace Azure.AI.OpenAI.FineTuning;

Expand Down Expand Up @@ -47,6 +49,21 @@ public override FineTuningJob FineTune(
return operation.WaitUntil(waitUntilCompleted, options);
}

internal override AsyncCollectionResult GetJobsAsync(string afterJobId, int? pageSize, RequestOptions options)
{
//var result = new AsyncFineTuningJobCollectionResult(this, Pipeline, options, pageSize, afterJobId);
return (AsyncCollectionResult<AzureFineTuningJob>)new AzureAsyncCollectionResult<AzureFineTuningJob, FineTuningCollectionPageToken>(
Pipeline,
options,
continuation => GetJobsPipelineMessage(continuation?.After, pageSize, options),
page => FineTuningCollectionPageToken.FromResponse(page, pageSize),
page => (IEnumerable<AzureFineTuningJob>)ModelReaderWriter.Read<InternalListPaginatedFineTuningJobsResponse>(page.GetRawResponse().Content).Data,
options?.CancellationToken ?? default
);


}

internal override PipelineMessage PostJobPipelineMessage(BinaryContent content, RequestOptions options)
=> new AzureOpenAIPipelineMessageBuilder(Pipeline, _endpoint, _apiVersion)
.WithMethod("POST")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace Azure.AI.OpenAI.FineTuning;

Expand All @@ -23,6 +24,7 @@ internal partial class AzureFineTuningClient : FineTuningClient

[Experimental("OPENAI001")]
internal AzureFineTuningClient(ClientPipeline pipeline, Uri endpoint, AzureOpenAIClientOptions options)
// : base(pipeline, new OpenAIClientOptions() { Endpoint = endpoint })
: base(pipeline, new OpenAIClientOptions() { Endpoint = endpoint })
{
Argument.AssertNotNull(pipeline, nameof(pipeline));
Expand All @@ -37,6 +39,37 @@ internal AzureFineTuningClient(ClientPipeline pipeline, Uri endpoint, AzureOpenA
protected AzureFineTuningClient()
{ }

/// <summary>
/// Get FineTuningJob for a previously started fine-tuning job.
///
/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
/// </summary>
/// <param name="JobId"> The ID of the fine-tuning job. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
public override FineTuningJob GetJob(string JobId, CancellationToken cancellationToken = default)
{
return AzureFineTuningJob.Rehydrate(this, JobId, cancellationToken.ToRequestOptions());
}

/// <summary>
/// Get FineTuningJob for a previously started fine-tuning job.
///
/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
/// </summary>
/// <param name="JobId"> The ID of the fine-tuning job. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
public override async Task<FineTuningJob> GetJobAsync(string JobId, CancellationToken cancellationToken = default)
{
return await AzureFineTuningJob.RehydrateAsync(this, JobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
}

//public override AsyncCollectionResult<AzureFineTuningJob> GetJobsAsync(FineTuningJobCollectionOptions options = default, CancellationToken cancellationToken = default)
//{
// options ??= new FineTuningJobCollectionOptions();
// AsyncCollectionResult<AzureFineTuningJob> jobs = (AsyncCollectionResult<AzureFineTuningJob>)GetJobsAsync(options.AfterJobId, options.PageSize, cancellationToken.ToRequestOptions());
// return (AsyncCollectionResult<AzureFineTuningJob>)jobs;
//}

[Experimental("OPENAI001")]
internal override FineTuningJob CreateJobFromResponse(PipelineResponse response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Azure.AI.OpenAI.FineTuning;
/// A long-running operation for creating a new model from a given dataset.
/// </summary>
[Experimental("OPENAI001")]
internal class AzureFineTuningJob : FineTuningJob
internal partial class AzureFineTuningJob : FineTuningJob
{
private readonly PipelineMessageClassifier _deleteJobClassifier;
private readonly ClientPipeline _pipeline;
Expand Down Expand Up @@ -60,17 +60,57 @@ private PipelineMessage CreateDeleteJobRequestMessage(string fineTuningJobId, Re
.WithOptions(options)
.Build();



internal override PipelineMessage CancelPipelineMessage(string fineTuningJobId, RequestOptions? options)
// TODO use this
internal PipelineMessage CancelPipelineMessage(string fineTuningJobId, RequestOptions? options)
=> new AzureOpenAIPipelineMessageBuilder(_pipeline, _endpoint, _apiVersion)
.WithMethod("POST")
.WithPath("fine_tuning", "jobs", fineTuningJobId, "cancel")
.WithAccept("application/json")
.WithOptions(options)
.Build();

internal override PipelineMessage GetCheckpointsPipelineMessage(string fineTuningJobId, string? after, int? limit, RequestOptions? options)
/// <summary>
/// [Protocol Method] List the checkpoints for a fine-tuning job.
/// </summary>
/// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
/// <param name="limit"> Number of checkpoints to retrieve. </param>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
public override AsyncCollectionResult GetCheckpointsAsync(string? after, int? limit, RequestOptions? options)
{
return new AsyncFineTuningCheckpointCollectionResult(this, options, limit, after);
}

/// <summary>
/// [Protocol Method] List the checkpoints for a fine-tuning job.
/// </summary>
/// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
/// <param name="limit"> Number of checkpoints to retrieve. </param>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
internal override async Task<ClientResult> GetCheckpointsPageAsync(string? after, int? limit, RequestOptions? options)
{
using PipelineMessage message = GetCheckpointsPipelineMessage(JobId, after, limit, options);
return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
}

/// <summary>
/// [Protocol Method] List the checkpoints for a fine-tuning job.
/// </summary>
/// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
/// <param name="limit"> Number of checkpoints to retrieve. </param>
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
internal override ClientResult GetCheckpointsPage(string? after, int? limit, RequestOptions? options)
{
using PipelineMessage message = GetCheckpointsPipelineMessage(JobId, after, limit, options);
return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
}

internal PipelineMessage GetCheckpointsPipelineMessage(string fineTuningJobId, string? after, int? limit, RequestOptions? options)
=> new AzureOpenAIPipelineMessageBuilder(_pipeline, _endpoint, _apiVersion)
.WithMethod("GET")
.WithPath("fine_tuning", "jobs", fineTuningJobId, "checkpoints")
Expand All @@ -80,7 +120,19 @@ internal override PipelineMessage GetCheckpointsPipelineMessage(string fineTunin
.WithOptions(options)
.Build();

internal override PipelineMessage GetEventsPipelineMessage(string fineTuningJobId, string? after, int? limit, RequestOptions? options)
internal override async Task<ClientResult> GetEventsPageAsync(string? after, int? limit, RequestOptions? options)
{
using PipelineMessage message = GetEventsPipelineMessage(JobId, after, limit, options);
return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
}

internal override ClientResult GetEventsPage(string? after, int? limit, RequestOptions? options)
{
using PipelineMessage message = GetEventsPipelineMessage(JobId, after, limit, options);
return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
}

internal PipelineMessage GetEventsPipelineMessage(string fineTuningJobId, string? after, int? limit, RequestOptions? options)
=> new AzureOpenAIPipelineMessageBuilder(_pipeline, _endpoint, _apiVersion)
.WithMethod("GET")
.WithPath("fine_tuning", "jobs", fineTuningJobId, "events")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.ClientModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#nullable enable

namespace Azure.AI.OpenAI.FineTuning;

internal partial class AzureFineTuningJob : FineTuningJob
{

public override AsyncCollectionResult<FineTuningCheckpoint> GetCheckpointsAsync(GetCheckpointsOptions? options = null, CancellationToken cancellationToken = default)
{
options ??= new GetCheckpointsOptions();
return (AsyncCollectionResult<FineTuningCheckpoint>)GetCheckpointsAsync(options.AfterCheckpointId, options.PageSize, cancellationToken.ToRequestOptions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,20 @@ public async Task JobsFineTuning(AzureOpenAIClientOptions.ServiceVersion? versio
[TestCase(null)]
public async Task CheckpointsFineTuning(AzureOpenAIClientOptions.ServiceVersion? version)
{
FineTuningClient client = GetTestClient(GetTestClientOptions(version));
AzureFineTuningClient client = (AzureFineTuningClient)UnWrap(GetTestClient(GetTestClientOptions(version)));

FineTuningJob job = await client.GetJobsAsync(options: new() { PageSize = 10 })
.FirstOrDefaultAsync(j => j.Status == "succeeded");
FineTuningJob job = await client.GetJobsAsync(options: new FineTuningJobCollectionOptions() { PageSize = 10 })
.FirstOrDefaultAsync(j => j.Status == "succeeded");

//FineTuningJob job = client.GetJob("ftjob-5ad97dff8fd246eeb0934f4fb37e8a76");
Assert.That(job, Is.Not.Null);
Assert.That(job.Status, Is.EqualTo("succeeded"));

Console.WriteLine("Job id: " + job.JobId);

if (job is AzureFineTuningJob azureJob)
{
azureJob.GetCheckpointsAsync();
}
var checkpoints = job.GetCheckpointsAsync();

await checkpoints.ToListAsync(); // Fails
Expand Down
4 changes: 2 additions & 2 deletions .dotnet/src/Custom/FineTuning/FineTuningClient.Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public virtual FineTuningJob FineTune(
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
private AsyncCollectionResult GetJobsAsync(string afterJobId, int? pageSize, RequestOptions options)
internal virtual AsyncCollectionResult GetJobsAsync(string afterJobId, int? pageSize, RequestOptions options)
{
return new AsyncFineTuningJobCollectionResult(this, Pipeline, options, pageSize, afterJobId);
}
Expand All @@ -98,7 +98,7 @@ private AsyncCollectionResult GetJobsAsync(string afterJobId, int? pageSize, Req
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
/// <returns> The response returned from the service. </returns>
private CollectionResult GetJobs(string after, int? pageSize, RequestOptions options)
internal virtual CollectionResult GetJobs(string after, int? pageSize, RequestOptions options)
{
return new FineTuningJobCollectionResult(this, Pipeline, options, pageSize, after);
}
Expand Down
13 changes: 11 additions & 2 deletions .dotnet/src/Custom/FineTuning/FineTuningClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ protected internal FineTuningClient(ClientPipeline pipeline, OpenAIClientOptions
_endpoint = OpenAIClient.GetEndpoint(options);
}

protected internal FineTuningClient(ClientPipeline pipeline, Uri endpoint)
{
Argument.AssertNotNull(pipeline, nameof(pipeline));
Argument.AssertNotNull(endpoint, nameof(endpoint));

Pipeline = pipeline;
_endpoint = endpoint;
}

/// <summary> Creates a job with a training file and base model. </summary>
/// <param name="baseModel"> The original model to use as a starting base to fine-tune. String such as "gpt-3.5-turbo" </param>
/// <param name="trainingFileId"> The training file Id that is already uploaded. String should match pattern '^file-[a-zA-Z0-9]{24}$'. </param>
Expand Down Expand Up @@ -124,7 +133,7 @@ public virtual async Task<FineTuningJob> FineTuneAsync(
/// </summary>
/// <param name="JobId"> The ID of the fine-tuning job. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
public FineTuningJob GetJob(string JobId, CancellationToken cancellationToken = default)
public virtual FineTuningJob GetJob(string JobId, CancellationToken cancellationToken = default)
{
return FineTuningJob.Rehydrate(this, JobId, cancellationToken.ToRequestOptions());
}
Expand All @@ -136,7 +145,7 @@ public FineTuningJob GetJob(string JobId, CancellationToken cancellationToken =
/// </summary>
/// <param name="JobId"> The ID of the fine-tuning job. </param>
/// <param name="cancellationToken"> The cancellation token. </param>
public async Task<FineTuningJob> GetJobAsync(string JobId, CancellationToken cancellationToken = default)
public async virtual Task<FineTuningJob> GetJobAsync(string JobId, CancellationToken cancellationToken = default)
{
return await FineTuningJob.RehydrateAsync(this, JobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
}
Expand Down
Loading

0 comments on commit c1cff5b

Please sign in to comment.