Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI-DotNet 7.2.3 #162

Merged
merged 11 commits into from
Nov 12, 2023
8 changes: 8 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_03_Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public async Task Test_01_GetChatCompletion()
}

result.GetUsage();

Console.WriteLine(result.LimitRequests);
Console.WriteLine(result.RemainingRequests);
Console.WriteLine(result.ResetRequests);
Console.WriteLine(result.LimitTokens);
Console.WriteLine(result.RemainingTokens);
Console.WriteLine(result.ResetTokens);

chsword marked this conversation as resolved.
Show resolved Hide resolved
}

[Test]
Expand Down
44 changes: 44 additions & 0 deletions OpenAI-DotNet/BaseResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,49 @@ public abstract class BaseResponse
/// </summary>
[JsonIgnore]
public string RequestId { get; internal set; }

/// <summary>
/// The version of the API used to generate this response, as reported in the response headers.
/// </summary>
[JsonIgnore]
public string OpenAIVersion { get; internal set; }

/// <summary>
/// The maximum number of requests that are permitted before exhausting the rate limit.
/// </summary>

[JsonIgnore]
public int LimitRequests { get; internal set; }
chsword marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The maximum number of tokens that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int LimitTokens { get; internal set; }
chsword marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The remaining number of requests that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int RemainingRequests { get; internal set; }
chsword marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The remaining number of tokens that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int RemainingTokens { get; internal set; }
chsword marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The time until the rate limit (based on requests) resets to its initial state.
/// </summary>
[JsonIgnore]
public string ResetRequests { get; internal set; }

/// <summary>
/// The time until the rate limit (based on tokens) resets to its initial state.
/// </summary>
[JsonIgnore]
public string ResetTokens { get; internal set; }

chsword marked this conversation as resolved.
Show resolved Hide resolved
}
}
47 changes: 47 additions & 0 deletions OpenAI-DotNet/Extensions/ResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ internal static class ResponseExtensions
private const string RequestId = "X-Request-ID";
private const string Organization = "Openai-Organization";
private const string ProcessingTime = "Openai-Processing-Ms";
private const string OpenAIVersion = "openai-version";
private const string XRateLimitLimitRequests = "x-ratelimit-limit-requests";
private const string XRateLimitLimitTokens = "x-ratelimit-limit-tokens";
private const string XRateLimitRemainingRequests = "x-ratelimit-remaining-requests";
private const string XRateLimitRemainingTokens = "x-ratelimit-remaining-tokens";
private const string XRateLimitResetRequests = "x-ratelimit-reset-requests";
private const string XRateLimitResetTokens = "x-ratelimit-reset-tokens";

private static readonly NumberFormatInfo numberFormatInfo = new NumberFormatInfo
{
Expand All @@ -41,6 +48,46 @@ internal static void SetResponseData(this BaseResponse response, HttpResponseHea
{
response.ProcessingTime = TimeSpan.FromMilliseconds(processingTime);
}

if (headers.TryGetValues(OpenAIVersion, out var version))
{
response.OpenAIVersion = version.First();
}

if (headers.TryGetValues(XRateLimitLimitRequests, out var limitRequests) &&
int.TryParse(limitRequests.FirstOrDefault(), out var limitRequestsValue)
)
{
response.LimitRequests = limitRequestsValue;
}

if (headers.TryGetValues(XRateLimitLimitTokens, out var limitTokens) &&
int.TryParse(limitTokens.FirstOrDefault(), out var limitTokensValue))
{
response.LimitTokens = limitTokensValue;
}

if (headers.TryGetValues(XRateLimitRemainingRequests, out var remainingRequests) &&
int.TryParse(remainingRequests.FirstOrDefault(), out var remainingRequestsValue))
{
response.RemainingRequests = remainingRequestsValue;
}

if (headers.TryGetValues(XRateLimitRemainingTokens, out var remainingTokens) &&
int.TryParse(remainingTokens.FirstOrDefault(), out var remainingTokensValue))
{
response.RemainingTokens = remainingTokensValue;
}

if (headers.TryGetValues(XRateLimitResetRequests, out var resetRequests))
{
response.ResetRequests = resetRequests.FirstOrDefault();
}

if (headers.TryGetValues(XRateLimitResetTokens, out var resetTokens))
{
response.ResetTokens = resetTokens.FirstOrDefault();
}
}

internal static async Task<string> ReadAsStringAsync(this HttpResponseMessage response, bool debugResponse = false, CancellationToken cancellationToken = default, [CallerMemberName] string methodName = null)
Expand Down
7 changes: 5 additions & 2 deletions OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ 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, dall-e-2, dall-e-3</PackageTags>
<Title>OpenAI API</Title>
<Version>7.2.2</Version>
<PackageReleaseNotes>Version 7.2.2
<Version>7.2.3</Version>
<PackageReleaseNotes>Version 7.2.3
- Added support for reading RateLimit information from the Headers in Completions and Chat endpoints.
- Updated chatEndpoint.GetCompletionAsync to handle Headers processing.
chsword marked this conversation as resolved.
Show resolved Hide resolved
Version 7.2.2
- Fixed Image Generation for Azure
Version 7.2.1
- Fixed a NRE with chat Message.ToString call when dynamic content is json element
Expand Down