Skip to content

Commit

Permalink
OpenAI-DotNet 7.0.10 (#146)
Browse files Browse the repository at this point in the history
- fixed processing time string culture conversion when parsing double
  • Loading branch information
StephenHodgson authored Oct 7, 2023
1 parent 3b48832 commit 9efcacd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
12 changes: 4 additions & 8 deletions OpenAI-DotNet/Chat/ChatEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public async Task<ChatResponse> 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);
Expand Down Expand Up @@ -102,10 +100,8 @@ public async IAsyncEnumerable<ChatResponse> 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);
Expand Down
12 changes: 4 additions & 8 deletions OpenAI-DotNet/Completions/CompletionsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -309,10 +307,8 @@ public async IAsyncEnumerable<CompletionResult> 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);
Expand Down
24 changes: 17 additions & 7 deletions OpenAI-DotNet/Extensions/ResponseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -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);
}
}

Expand Down
3 changes: 1 addition & 2 deletions OpenAI-DotNet/Images/ImagesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,13 @@ public async Task<IReadOnlyList<string>> CreateImageVariationAsync(ImageVariatio
private async Task<IReadOnlyList<string>> DeserializeResponseAsync(HttpResponseMessage response, CancellationToken cancellationToken = default)
{
var resultAsString = await response.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
var imagesResponse = JsonSerializer.Deserialize<ImagesResponse>(resultAsString, Api.JsonSerializationOptions);
var imagesResponse = response.DeserializeResponse<ImagesResponse>(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();
}
}
Expand Down
6 changes: 4 additions & 2 deletions OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
<RepositoryUrl>https://github.com/RageAgainstThePixel/OpenAI-DotNet</RepositoryUrl>
<PackageTags>OpenAI, AI, ML, API, gpt-4, gpt-3.5-tubo, gpt-3, chatGPT, chat-gpt, gpt-2, gpt</PackageTags>
<Title>OpenAI API</Title>
<Version>7.0.9</Version>
<PackageReleaseNotes>Version 7.0.9
<Version>7.0.10</Version>
<PackageReleaseNotes>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?
Expand Down

0 comments on commit 9efcacd

Please sign in to comment.