Skip to content

Commit

Permalink
OpenAI-DotNet 7.2.3 (#162)
Browse files Browse the repository at this point in the history
- Added support for reading RateLimit information from the Headers

---------

Co-authored-by: Stephen Hodgson <rage.against.the.pixel@gmail.com>
  • Loading branch information
chsword and StephenHodgson authored Nov 12, 2023
1 parent 201f5fb commit 5de096c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
7 changes: 7 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,13 @@ 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);
}

[Test]
Expand Down
43 changes: 43 additions & 0 deletions OpenAI-DotNet/BaseResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,48 @@ 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; }

/// <summary>
/// The maximum number of tokens that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int? LimitTokens { get; internal set; }

/// <summary>
/// The remaining number of requests that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int? RemainingRequests { get; internal set; }

/// <summary>
/// The remaining number of tokens that are permitted before exhausting the rate limit.
/// </summary>
[JsonIgnore]
public int? RemainingTokens { get; internal set; }

/// <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; }
}
}
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
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, 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
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ You can also load the configuration file directly with known path by calling sta
- Loads the default `.openai` config in the specified directory:

```csharp
var api = new OpenAIClient(OpenAIAuthentication.Default.LoadFromDirectory("path/to/your/directory"));
var api = new OpenAIClient(OpenAIAuthentication.LoadFromDirectory("path/to/your/directory"));
```

- Loads the configuration file from a specific path. File does not need to be named `.openai` as long as it conforms to the json format:

```csharp
var api = new OpenAIClient(OpenAIAuthentication.Default.LoadFromPath("path/to/your/file.json"));
var api = new OpenAIClient(OpenAIAuthentication.LoadFromPath("path/to/your/file.json"));
```

#### Use System Environment Variables
Expand All @@ -149,7 +149,7 @@ Use your system's environment variables specify an api key and organization to u
- Use `OPENAI_ORGANIZATION_ID` to specify an organization.

```csharp
var api = new OpenAIClient(OpenAIAuthentication.Default.LoadFromEnv());
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
```

### [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/)
Expand Down Expand Up @@ -821,4 +821,4 @@ Assert.IsTrue(response);

![CC-0 Public Domain](https://licensebuttons.net/p/zero/1.0/88x31.png)

This library is licensed CC-0, in the public domain. You can use it for whatever you want, publicly or privately, without worrying about permission or licensing or whatever. It's just a wrapper around the OpenAI API, so you still need to get access to OpenAI from them directly. I am not affiliated with OpenAI and this library is not endorsed by them, I just have beta access and wanted to make a C# library to access it more easily. Hopefully others find this useful as well. Feel free to open a PR if there's anything you want to contribute.
This library is licensed CC-0, in the public domain. You can use it for whatever you want, publicly or privately, without worrying about permission or licensing or whatever. It's just a wrapper around the OpenAI API, so you still need to get access to OpenAI from them directly. I am not affiliated with OpenAI and this library is not endorsed by them, I just have beta access and wanted to make a C# library to access it more easily. Hopefully others find this useful as well. Feel free to open a PR if there's anything you want to contribute.

0 comments on commit 5de096c

Please sign in to comment.