Skip to content

Commit 59b0afb

Browse files
OpenAI-DotNet 8.7.4 (#470)
- Updated proxy support for the OpenAI-DotNet-Proxy package - Renamed OpenAIAuthentication.LoadFromEnv -> OpenAIAuthentication.LoadFromEnvironment ## OpenAI-DotNet-Proxy 8.7.4 - Updated proxy support for the OpenAI-DotNet package - Ensure we're returning the full response message body and content length to the clients
1 parent c419cea commit 59b0afb

File tree

8 files changed

+36
-24
lines changed

8 files changed

+36
-24
lines changed

OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
<IncludeSymbols>true</IncludeSymbols>
2323
<SignAssembly>false</SignAssembly>
2424
<ImplicitUsings>false</ImplicitUsings>
25-
<Version>8.7.0</Version>
25+
<Version>8.7.4</Version>
2626
<PackageReleaseNotes>
27+
Version 8.7.4
28+
- Updated proxy support for the OpenAI-DotNet package
29+
- Ensure we're returning the full response message body and content length to the clients
2730
Version 8.7.0
2831
- Fix Azure OpenAI api-version query parameter not being forwarded correctly
2932
Version 8.4.0

OpenAI-DotNet-Proxy/Proxy/EndpointRouteBuilder.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public static class EndpointRouteBuilder
5252
/// Maps the <see cref="OpenAIClient"/> endpoints.
5353
/// </summary>
5454
/// <param name="endpoints"><see cref="IEndpointRouteBuilder"/>.</param>
55-
/// <param name="openAIClient"><see cref="OpenAIClient"/>.</param>
55+
/// <param name="client"><see cref="OpenAIClient"/>.</param>
5656
/// <param name="authenticationFilter"><see cref="IAuthenticationFilter"/>.</param>
5757
/// <param name="routePrefix">Optional, custom route prefix. i.e. '/openai'.</param>
58-
public static void MapOpenAIEndpoints(this IEndpointRouteBuilder endpoints, OpenAIClient openAIClient, IAuthenticationFilter authenticationFilter, string routePrefix = "")
58+
public static void MapOpenAIEndpoints(this IEndpointRouteBuilder endpoints, OpenAIClient client, IAuthenticationFilter authenticationFilter, string routePrefix = "")
5959
{
60-
endpoints.Map($"{routePrefix}{openAIClient.Settings.BaseRequest}{{**endpoint}}", HandleRequest);
60+
endpoints.Map($"{routePrefix}{client.Settings.BaseRequest}{{**endpoint}}", HandleRequest);
6161
return;
6262

6363
async Task HandleRequest(HttpContext httpContext, string endpoint)
@@ -80,13 +80,13 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
8080
modifiedQuery[pair.Key] = pair.Value.FirstOrDefault();
8181
}
8282

83-
if (openAIClient.Settings.IsAzureOpenAI)
83+
if (client.Settings.IsAzureOpenAI)
8484
{
85-
modifiedQuery["api-version"] = openAIClient.Settings.ApiVersion;
85+
modifiedQuery["api-version"] = client.Settings.ApiVersion;
8686
}
8787

8888
var uri = new Uri(string.Format(
89-
openAIClient.Settings.BaseRequestUrlFormat,
89+
client.Settings.BaseRequestUrlFormat,
9090
QueryHelpers.AddQueryString(endpoint, modifiedQuery)
9191
));
9292

@@ -98,8 +98,10 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
9898
request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(httpContext.Request.ContentType);
9999
}
100100

101-
var proxyResponse = await openAIClient.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, httpContext.RequestAborted).ConfigureAwait(false);
101+
var proxyResponse = await client.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, httpContext.RequestAborted).ConfigureAwait(false);
102102
httpContext.Response.StatusCode = (int)proxyResponse.StatusCode;
103+
httpContext.Response.ContentLength = proxyResponse.Content.Headers.ContentLength;
104+
httpContext.Response.ContentType = proxyResponse.Content.Headers.ContentType?.ToString();
103105

104106
foreach (var (key, value) in proxyResponse.Headers)
105107
{
@@ -112,11 +114,10 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
112114
if (excludedHeaders.Contains(key)) { continue; }
113115
httpContext.Response.Headers[key] = value.ToArray();
114116
}
115-
116-
httpContext.Response.ContentType = proxyResponse.Content.Headers.ContentType?.ToString() ?? string.Empty;
117117
const string streamingContent = "text/event-stream";
118118

119-
if (httpContext.Response.ContentType.Equals(streamingContent))
119+
if (httpContext.Response.ContentType != null &&
120+
httpContext.Response.ContentType.Equals(streamingContent, StringComparison.OrdinalIgnoreCase))
120121
{
121122
var stream = await proxyResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
122123
await WriteServerStreamEventsAsync(httpContext, stream).ConfigureAwait(false);
@@ -185,13 +186,13 @@ async Task ProcessWebSocketRequest(HttpContext httpContext, string endpoint)
185186

186187
using var hostWebsocket = new ClientWebSocket();
187188

188-
foreach (var header in openAIClient.WebsocketHeaders)
189+
foreach (var header in client.WebsocketHeaders)
189190
{
190191
hostWebsocket.Options.SetRequestHeader(header.Key, header.Value);
191192
}
192193

193194
var uri = new Uri(string.Format(
194-
openAIClient.Settings.BaseWebSocketUrlFormat,
195+
client.Settings.BaseWebSocketUrlFormat,
195196
$"{endpoint}{httpContext.Request.QueryString}"
196197
));
197198
await hostWebsocket.ConnectAsync(uri, httpContext.RequestAborted).ConfigureAwait(false);

OpenAI-DotNet-Proxy/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public partial class Program
7575

7676
public static void Main(string[] args)
7777
{
78-
var auth = OpenAIAuthentication.LoadFromEnv();
78+
var auth = OpenAIAuthentication.LoadFromEnvironment();
7979
var settings = new OpenAISettings(/* your custom settings if using Azure OpenAI */);
8080
using var openAIClient = new OpenAIClient(auth, settings);
8181
OpenAIProxy.CreateWebApplication<AuthenticationFilter>(args, openAIClient).Run();

OpenAI-DotNet-Tests-Proxy/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override async Task ValidateAuthenticationAsync(IHeaderDictionary request
3333

3434
public static void Main(string[] args)
3535
{
36-
var auth = OpenAIAuthentication.LoadFromEnv();
36+
var auth = OpenAIAuthentication.LoadFromEnvironment();
3737
var settings = new OpenAISettings(/* your custom settings if using Azure OpenAI */);
3838
using var openAIClient = new OpenAIClient(auth, settings);
3939
using var app = OpenAIProxy.CreateWebApplication<AuthenticationFilter>(args, openAIClient);

OpenAI-DotNet-Tests/TestFixture_00_01_Authentication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public void Setup()
2222
[Test]
2323
public void Test_01_GetAuthFromEnv()
2424
{
25-
var auth = OpenAIAuthentication.LoadFromEnv();
25+
var auth = OpenAIAuthentication.LoadFromEnvironment();
2626
Assert.IsNotNull(auth);
2727
Assert.IsNotNull(auth.ApiKey);
2828
Assert.IsNotEmpty(auth.ApiKey);
2929

30-
auth = OpenAIAuthentication.LoadFromEnv("org-testOrg");
30+
auth = OpenAIAuthentication.LoadFromEnvironment("org-testOrg");
3131
Assert.IsNotNull(auth);
3232
Assert.IsNotNull(auth.ApiKey);
3333
Assert.IsNotEmpty(auth.ApiKey);

OpenAI-DotNet/Authentication/OpenAIAuthentication.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public sealed class OpenAIAuthentication
7171
/// <summary>
7272
/// The default authentication to use when no other auth is specified.
7373
/// This can be set manually, or automatically loaded via environment variables or a config file.
74-
/// <seealso cref="LoadFromEnv"/><seealso cref="LoadFromDirectory"/>
74+
/// <seealso cref="LoadFromEnvironment"/><seealso cref="LoadFromDirectory"/>
7575
/// </summary>
7676
public static OpenAIAuthentication Default
7777
{
@@ -84,13 +84,17 @@ public static OpenAIAuthentication Default
8484

8585
var auth = LoadFromDirectory() ??
8686
LoadFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ??
87-
LoadFromEnv();
87+
LoadFromEnvironment();
8888
cachedDefault = auth ?? throw new UnauthorizedAccessException("Failed to load a valid API Key!");
8989
return auth;
9090
}
9191
internal set => cachedDefault = value;
9292
}
9393

94+
[Obsolete("use LoadFromEnvironment")]
95+
public static OpenAIAuthentication LoadFromEnv(string organizationId = null)
96+
=> LoadFromEnvironment(organizationId);
97+
9498
/// <summary>
9599
/// Attempts to load api keys from environment variables, as "OPENAI_KEY" (or "OPENAI_SECRET_KEY", for backwards compatibility)
96100
/// </summary>
@@ -102,7 +106,7 @@ public static OpenAIAuthentication Default
102106
/// Returns the loaded <see cref="OpenAIAuthentication"/> any api keys were found,
103107
/// or <see langword="null"/> if there were no matching environment vars.
104108
/// </returns>
105-
public static OpenAIAuthentication LoadFromEnv(string organizationId = null)
109+
public static OpenAIAuthentication LoadFromEnvironment(string organizationId = null)
106110
{
107111
var apiKey = Environment.GetEnvironmentVariable(OPENAI_KEY);
108112

OpenAI-DotNet/OpenAI-DotNet.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
2929
<AssemblyOriginatorKeyFile>OpenAI-DotNet.pfx</AssemblyOriginatorKeyFile>
3030
<IncludeSymbols>true</IncludeSymbols>
3131
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
32-
<Version>8.7.3</Version>
32+
<Version>8.7.4</Version>
3333
<PackageReleaseNotes>
34+
Version 8.7.4
35+
- Updated proxy support for the OpenAI-DotNet-Proxy package
36+
- Renamed OpenAIAuthentication.LoadFromEnv -&gt; OpenAIAuthentication.LoadFromEnvironment
3437
Version 8.7.3
3538
- Fixed Response.Instructions deserialization when using CreateResponseRequest.Prompt
3639
Version 8.7.2
@@ -481,7 +484,8 @@ Version 4.4.0
481484
</None>
482485
</ItemGroup>
483486
<ItemGroup>
484-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
487+
<PackageReference Include="Microsoft.SourceLink.GitHub"
488+
Version="8.0.0">
485489
<PrivateAssets>all</PrivateAssets>
486490
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
487491
</PackageReference>

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Use your system's environment variables specify an api key and organization to u
231231
- Use `OPENAI_PROJECT_ID` to specify a project.
232232

233233
```csharp
234-
using var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
234+
using var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnvironment());
235235
```
236236

237237
### Handling OpenAIClient and HttpClient Lifecycle
@@ -355,7 +355,7 @@ public partial class Program
355355

356356
public static void Main(string[] args)
357357
{
358-
var auth = OpenAIAuthentication.LoadFromEnv();
358+
var auth = OpenAIAuthentication.LoadFromEnvironment();
359359
var settings = new OpenAISettings(/* your custom settings if using Azure OpenAI */);
360360
using var openAIClient = new OpenAIClient(auth, settings);
361361
OpenAIProxy.CreateWebApplication<AuthenticationFilter>(args, openAIClient).Run();

0 commit comments

Comments
 (0)