From 9efcacdb728d57c37d88022e05c9027b142a3e39 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sat, 7 Oct 2023 12:06:48 -0400 Subject: [PATCH] OpenAI-DotNet 7.0.10 (#146) - fixed processing time string culture conversion when parsing double --- OpenAI-DotNet/Chat/ChatEndpoint.cs | 12 ++++------ .../Completions/CompletionsEndpoint.cs | 12 ++++------ .../Extensions/ResponseExtensions.cs | 24 +++++++++++++------ OpenAI-DotNet/Images/ImagesEndpoint.cs | 3 +-- OpenAI-DotNet/OpenAI-DotNet.csproj | 6 +++-- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/OpenAI-DotNet/Chat/ChatEndpoint.cs b/OpenAI-DotNet/Chat/ChatEndpoint.cs index f1c79829..4826979a 100644 --- a/OpenAI-DotNet/Chat/ChatEndpoint.cs +++ b/OpenAI-DotNet/Chat/ChatEndpoint.cs @@ -49,10 +49,8 @@ public async Task StreamCompletionAsync(ChatRequest chatRequest, A { chatRequest.Stream = true; var jsonContent = JsonSerializer.Serialize(chatRequest, Api.JsonSerializationOptions).ToJsonStringContent(); - using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl("/completions")) - { - Content = jsonContent - }; + using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl("/completions")); + request.Content = jsonContent; var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false); await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); @@ -102,10 +100,8 @@ public async IAsyncEnumerable StreamCompletionEnumerableAsync(Chat { chatRequest.Stream = true; var jsonContent = JsonSerializer.Serialize(chatRequest, Api.JsonSerializationOptions).ToJsonStringContent(); - using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl("/completions")) - { - Content = jsonContent - }; + using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl("/completions")); + request.Content = jsonContent; var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false); await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); diff --git a/OpenAI-DotNet/Completions/CompletionsEndpoint.cs b/OpenAI-DotNet/Completions/CompletionsEndpoint.cs index 2f9fdaa0..a98e8eeb 100644 --- a/OpenAI-DotNet/Completions/CompletionsEndpoint.cs +++ b/OpenAI-DotNet/Completions/CompletionsEndpoint.cs @@ -199,10 +199,8 @@ public async Task StreamCompletionAsync(CompletionRequest completionRequest, Act { completionRequest.Stream = true; var jsonContent = JsonSerializer.Serialize(completionRequest, Api.JsonSerializationOptions).ToJsonStringContent(); - using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl()) - { - Content = jsonContent - }; + using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl()); + request.Content = jsonContent; var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false); await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); @@ -309,10 +307,8 @@ public async IAsyncEnumerable StreamCompletionEnumerableAsync( { completionRequest.Stream = true; var jsonContent = JsonSerializer.Serialize(completionRequest, Api.JsonSerializationOptions).ToJsonStringContent(); - using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl()) - { - Content = jsonContent - }; + using var request = new HttpRequestMessage(HttpMethod.Post, GetUrl()); + request.Content = jsonContent; var response = await Api.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); await response.CheckResponseAsync(cancellationToken).ConfigureAwait(false); await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); diff --git a/OpenAI-DotNet/Extensions/ResponseExtensions.cs b/OpenAI-DotNet/Extensions/ResponseExtensions.cs index 3705458a..178c439b 100644 --- a/OpenAI-DotNet/Extensions/ResponseExtensions.cs +++ b/OpenAI-DotNet/Extensions/ResponseExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; @@ -11,25 +12,34 @@ namespace OpenAI.Extensions { internal static class ResponseExtensions { - private const string Organization = "Openai-Organization"; private const string RequestId = "X-Request-ID"; + private const string Organization = "Openai-Organization"; private const string ProcessingTime = "Openai-Processing-Ms"; + private static readonly NumberFormatInfo numberFormatInfo = new NumberFormatInfo + { + NumberGroupSeparator = ",", + NumberDecimalSeparator = "." + }; + internal static void SetResponseData(this BaseResponse response, HttpResponseHeaders headers) { - if (headers.Contains(Organization)) + if (headers == null) { return; } + + if (headers.TryGetValues(RequestId, out var requestId)) { - response.Organization = headers.GetValues(Organization).FirstOrDefault(); + response.RequestId = requestId.First(); } - if (headers.Contains(ProcessingTime)) + if (headers.TryGetValues(Organization, out var organization)) { - response.ProcessingTime = TimeSpan.FromMilliseconds(double.Parse(headers.GetValues(ProcessingTime).First())); + response.Organization = organization.First(); } - if (headers.Contains(RequestId)) + if (headers.TryGetValues(ProcessingTime, out var processingTimeString) && + double.TryParse(processingTimeString.First(), NumberStyles.AllowDecimalPoint, numberFormatInfo, out var processingTime)) { - response.RequestId = headers.GetValues(RequestId).FirstOrDefault(); + response.ProcessingTime = TimeSpan.FromMilliseconds(processingTime); } } diff --git a/OpenAI-DotNet/Images/ImagesEndpoint.cs b/OpenAI-DotNet/Images/ImagesEndpoint.cs index 3736626e..586fa70b 100644 --- a/OpenAI-DotNet/Images/ImagesEndpoint.cs +++ b/OpenAI-DotNet/Images/ImagesEndpoint.cs @@ -206,14 +206,13 @@ public async Task> CreateImageVariationAsync(ImageVariatio private async Task> DeserializeResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken = default) { var resultAsString = await response.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); - var imagesResponse = JsonSerializer.Deserialize(resultAsString, Api.JsonSerializationOptions); + var imagesResponse = response.DeserializeResponse(resultAsString, Api.JsonSerializationOptions); if (imagesResponse?.Data == null || imagesResponse.Data.Count == 0) { throw new HttpRequestException($"{nameof(DeserializeResponseAsync)} returned no results! HTTP status code: {response.StatusCode}. Response body: {resultAsString}"); } - imagesResponse.SetResponseData(response.Headers); return imagesResponse.Data.Select(imageResult => string.IsNullOrWhiteSpace(imageResult.Url) ? imageResult.B64_Json : imageResult.Url).ToList(); } } diff --git a/OpenAI-DotNet/OpenAI-DotNet.csproj b/OpenAI-DotNet/OpenAI-DotNet.csproj index ddc3bc6f..361facc0 100644 --- a/OpenAI-DotNet/OpenAI-DotNet.csproj +++ b/OpenAI-DotNet/OpenAI-DotNet.csproj @@ -17,8 +17,10 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet- https://github.com/RageAgainstThePixel/OpenAI-DotNet OpenAI, AI, ML, API, gpt-4, gpt-3.5-tubo, gpt-3, chatGPT, chat-gpt, gpt-2, gpt OpenAI API - 7.0.9 - Version 7.0.9 + 7.0.10 + Version 7.0.10 +- Fixed processing time string culture conversion when parsing double +Version 7.0.9 - Fixed Model delete permission Unauthorized Access check Version 7.0.8 - Fixed AudioTranscriptionRequest.Temperature type. int? -> float?