Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Nov 16, 2023
1 parent 8f7c6b2 commit c8717f2
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 119 deletions.
28 changes: 12 additions & 16 deletions OpenAI-DotNet-Tests/TestFixture_09_FineTuning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public async Task Test_02_ListFineTuneJobs()
Assert.IsNotNull(OpenAIClient.FineTuningEndpoint);
var list = await OpenAIClient.FineTuningEndpoint.ListJobsAsync();
Assert.IsNotNull(list);
Assert.IsNotEmpty(list.Jobs);
Assert.IsNotEmpty(list.Items);

foreach (var job in list.Jobs.OrderByDescending(job => job.CreatedAt))
foreach (var job in list.Items.OrderByDescending(job => job.CreatedAt))
{
Console.WriteLine($"{job.Id} -> {job.CreatedAt} | {job.Status}");
}
Expand All @@ -120,9 +120,9 @@ public async Task Test_03_RetrieveFineTuneJobInfo()
Assert.IsNotNull(OpenAIClient.FineTuningEndpoint);
var list = await OpenAIClient.FineTuningEndpoint.ListJobsAsync();
Assert.IsNotNull(list);
Assert.IsNotEmpty(list.Jobs);
Assert.IsNotEmpty(list.Items);

foreach (var job in list.Jobs.OrderByDescending(job => job.CreatedAt))
foreach (var job in list.Items.OrderByDescending(job => job.CreatedAt))
{
var request = await OpenAIClient.FineTuningEndpoint.GetJobInfoAsync(job);
Assert.IsNotNull(request);
Expand All @@ -136,22 +136,18 @@ public async Task Test_04_ListFineTuneEvents()
Assert.IsNotNull(OpenAIClient.FineTuningEndpoint);
var list = await OpenAIClient.FineTuningEndpoint.ListJobsAsync();
Assert.IsNotNull(list);
Assert.IsNotEmpty(list.Jobs);
Assert.IsNotEmpty(list.Items);

foreach (var job in list.Jobs)
foreach (var job in list.Items)
{
if (job.Status == JobStatus.Cancelled)
{
continue;
}
if (job.Status == JobStatus.Cancelled) { continue; }

var eventList = await OpenAIClient.FineTuningEndpoint.ListJobEventsAsync(job);
Assert.IsNotNull(eventList);
Assert.IsNotEmpty(eventList.Events);

Console.WriteLine($"{job.Id} -> status: {job.Status} | event count: {eventList.Events.Count}");
Assert.IsNotEmpty(eventList.Items);
Console.WriteLine($"{job.Id} -> status: {job.Status} | event count: {eventList.Items.Count}");

foreach (var @event in eventList.Events.OrderByDescending(@event => @event.CreatedAt))
foreach (var @event in eventList.Items.OrderByDescending(@event => @event.CreatedAt))
{
Console.WriteLine($" {@event.CreatedAt} [{@event.Level}] {@event.Message}");
}
Expand All @@ -166,9 +162,9 @@ public async Task Test_05_CancelFineTuneJob()
Assert.IsNotNull(OpenAIClient.FineTuningEndpoint);
var list = await OpenAIClient.FineTuningEndpoint.ListJobsAsync();
Assert.IsNotNull(list);
Assert.IsNotEmpty(list.Jobs);
Assert.IsNotEmpty(list.Items);

foreach (var job in list.Jobs)
foreach (var job in list.Items)
{
if (job.Status is > JobStatus.NotStarted and < JobStatus.Succeeded)
{
Expand Down
4 changes: 1 addition & 3 deletions OpenAI-DotNet-Tests/TestFixture_14_TheadMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Message = OpenAI.Threads.Message;

namespace OpenAI.Tests
{
Expand All @@ -20,8 +19,7 @@ internal class TestFixture_14_TheadMessages : AbstractTestFixture
new Dictionary<string, string>
{
["text"] = "test"
}
);
});

[Test]
public async Task Test_01_CreateThreadMessage()
Expand Down
14 changes: 7 additions & 7 deletions OpenAI-DotNet/Assistants/AssistantsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<Assistant> CreateAssistantAsync(AssistantRequest request, Canc
var jsonContent = JsonSerializer.Serialize(request, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl(), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -37,7 +37,7 @@ public async Task<Assistant> RetrieveAssistantAsync(string assistantId, Cancella
{
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}"), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ public async Task<Assistant> ModifyAssistantAsync(string assistantId, AssistantR
var jsonContent = JsonSerializer.Serialize(request, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl($"/{assistantId}"), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<Assistant>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -78,7 +78,7 @@ public async Task<ListResponse<Assistant>> ListAssistantsAsync(ListRequest reque
{
var response = await Api.Client.GetAsync(GetUrl(queryParameters: request), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<ListResponse<Assistant>>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<ListResponse<Assistant>>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -100,7 +100,7 @@ public async Task<AssistantFile> CreateAssistantFileAsync(string assistantId, Fi
var jsonContent = JsonSerializer.Serialize(new { file_id = file.Id }, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl($"/{assistantId}/files"), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<AssistantFile>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<AssistantFile>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -114,7 +114,7 @@ public async Task<AssistantFile> RetrieveAssistantFileAsync(string assistantId,
{
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}/files/{fileId}"), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<AssistantFile>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<AssistantFile>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task<ListResponse<AssistantFile>> ListAssistantFilesAsync(string as
{
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}/files", request), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<ListResponse<AssistantFile>>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<ListResponse<AssistantFile>>(responseAsString, OpenAIClient.JsonSerializationOptions);
}
}
}
8 changes: 3 additions & 5 deletions OpenAI-DotNet/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ChatResponse> GetCompletionAsync(ChatRequest chatRequest, Canc
var jsonContent = JsonSerializer.Serialize(chatRequest, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl("/completions"), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<ChatResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<ChatResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -43,7 +43,6 @@ public async Task<ChatResponse> GetCompletionAsync(ChatRequest chatRequest, Canc
/// <param name="resultHandler">An action to be called as each new result arrives.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ChatResponse"/>.</returns>
/// <exception cref="HttpRequestException">Raised when the HTTP request fails</exception>
public async Task<ChatResponse> StreamCompletionAsync(ChatRequest chatRequest, Action<ChatResponse> resultHandler, CancellationToken cancellationToken = default)
{
chatRequest.Stream = true;
Expand All @@ -68,7 +67,7 @@ public async Task<ChatResponse> StreamCompletionAsync(ChatRequest chatRequest, A
Console.WriteLine(eventData);
}

var partialResponse = response.DeserializeResponse<ChatResponse>(eventData, OpenAIClient.JsonSerializationOptions);
var partialResponse = response.Deserialize<ChatResponse>(eventData, OpenAIClient.JsonSerializationOptions);

if (chatResponse == null)
{
Expand Down Expand Up @@ -99,7 +98,6 @@ public async Task<ChatResponse> StreamCompletionAsync(ChatRequest chatRequest, A
/// <param name="chatRequest">The chat request which contains the message content.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ChatResponse"/>.</returns>
/// <exception cref="HttpRequestException">Raised when the HTTP request fails</exception>
public async IAsyncEnumerable<ChatResponse> StreamCompletionEnumerableAsync(ChatRequest chatRequest, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
chatRequest.Stream = true;
Expand All @@ -124,7 +122,7 @@ public async IAsyncEnumerable<ChatResponse> StreamCompletionEnumerableAsync(Chat
Console.WriteLine(eventData);
}

var partialResponse = response.DeserializeResponse<ChatResponse>(eventData, OpenAIClient.JsonSerializationOptions);
var partialResponse = response.Deserialize<ChatResponse>(eventData, OpenAIClient.JsonSerializationOptions);

if (chatResponse == null)
{
Expand Down
33 changes: 19 additions & 14 deletions OpenAI-DotNet/Completions/CompletionsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ internal CompletionsEndpoint(OpenAIClient api) : base(api) { }
/// <param name="model">Optional, <see cref="Model"/> to use when calling the API.
/// Defaults to <see cref="Model.Davinci"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Asynchronously returns the completion result.
/// Look in its <see cref="CompletionResult.Completions"/> property for the completions.</returns>
/// <returns>
/// Asynchronously returns the completion result.
/// Look in its <see cref="CompletionResult.Completions"/> property for the completions.
/// </returns>
public async Task<CompletionResult> CreateCompletionAsync(
string prompt = null,
IEnumerable<string> prompts = null,
Expand Down Expand Up @@ -101,16 +103,17 @@ public async Task<CompletionResult> CreateCompletionAsync(
/// </summary>
/// <param name="completionRequest">The request to send to the API.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Asynchronously returns the completion result.
/// Look in its <see cref="CompletionResult.Completions"/> property for the completions.</returns>
/// <exception cref="HttpRequestException">Raised when the HTTP request fails</exception>
/// <returns>
/// Asynchronously returns the completion result.
/// Look in its <see cref="CompletionResult.Completions"/> property for the completions.
/// </returns>
public async Task<CompletionResult> CreateCompletionAsync(CompletionRequest completionRequest, CancellationToken cancellationToken = default)
{
completionRequest.Stream = false;
var jsonContent = JsonSerializer.Serialize(completionRequest, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl(), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<CompletionResult>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<CompletionResult>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

#endregion Non-Streaming
Expand Down Expand Up @@ -149,9 +152,11 @@ public async Task<CompletionResult> CreateCompletionAsync(CompletionRequest comp
/// <param name="model">Optional, <see cref="Model"/> to use when calling the API.
/// Defaults to <see cref="Model.Davinci"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>An async enumerable with each of the results as they come in.
/// <returns>
/// An async enumerable with each of the results as they come in.
/// See <see href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#asynchronous-streams">the C# docs</see>
/// for more details on how to consume an async enumerable.</returns>
/// for more details on how to consume an async enumerable.
/// </returns>
public async Task StreamCompletionAsync(
Action<CompletionResult> resultHandler,
string prompt = null,
Expand Down Expand Up @@ -194,7 +199,6 @@ public async Task StreamCompletionAsync(
/// <param name="completionRequest">The request to send to the API.</param>
/// <param name="resultHandler">An action to be called as each new result arrives.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <exception cref="HttpRequestException">Raised when the HTTP request fails</exception>
public async Task StreamCompletionAsync(CompletionRequest completionRequest, Action<CompletionResult> resultHandler, CancellationToken cancellationToken = default)
{
completionRequest.Stream = true;
Expand All @@ -214,7 +218,7 @@ public async Task StreamCompletionAsync(CompletionRequest completionRequest, Act
{
if (string.IsNullOrWhiteSpace(eventData)) { continue; }

resultHandler(response.DeserializeResponse<CompletionResult>(eventData, OpenAIClient.JsonSerializationOptions));
resultHandler(response.Deserialize<CompletionResult>(eventData, OpenAIClient.JsonSerializationOptions));
}
else
{
Expand Down Expand Up @@ -299,10 +303,11 @@ public IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(
/// </summary>
/// <param name="completionRequest">The request to send to the API.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>An async enumerable with each of the results as they come in.
/// <returns>
/// An async enumerable with each of the results as they come in.
/// See <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#asynchronous-streams"/>
/// for more details on how to consume an async enumerable.</returns>
/// <exception cref="HttpRequestException">Raised when the HTTP request fails</exception>
/// for more details on how to consume an async enumerable.
/// </returns>
public async IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(CompletionRequest completionRequest, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
completionRequest.Stream = true;
Expand All @@ -321,7 +326,7 @@ public async IAsyncEnumerable<CompletionResult> StreamCompletionEnumerableAsync(
if (streamData.TryGetEventStreamData(out var eventData))
{
if (string.IsNullOrWhiteSpace(eventData)) { continue; }
yield return response.DeserializeResponse<CompletionResult>(eventData, OpenAIClient.JsonSerializationOptions);
yield return response.Deserialize<CompletionResult>(eventData, OpenAIClient.JsonSerializationOptions);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Edits/EditsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task<EditResponse> CreateEditAsync(EditRequest request, Cancellatio
var jsonContent = JsonSerializer.Serialize(request, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl(), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<EditResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<EditResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Embeddings/EmbeddingsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<EmbeddingsResponse> CreateEmbeddingAsync(EmbeddingsRequest req
var jsonContent = JsonSerializer.Serialize(request, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(EnableDebug);
var response = await Api.Client.PostAsync(GetUrl(), jsonContent, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.DeserializeResponse<EmbeddingsResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
return response.Deserialize<EmbeddingsResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Extensions/ResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal static async Task CheckResponseAsync(this HttpResponseMessage response,
}
}

internal static T DeserializeResponse<T>(this HttpResponseMessage response, string json, JsonSerializerOptions settings) where T : BaseResponse
internal static T Deserialize<T>(this HttpResponseMessage response, string json, JsonSerializerOptions settings) where T : BaseResponse
{
var result = JsonSerializer.Deserialize<T>(json, settings);
result.SetResponseData(response.Headers);
Expand Down
Loading

0 comments on commit c8717f2

Please sign in to comment.