Skip to content

Commit 627edd1

Browse files
author
Nils Clauß
committed
Implement Azure OpenAI Service
1 parent dd49841 commit 627edd1

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

OpenAI_API/EndpointBase.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,31 @@ protected string Url
4444
{
4545
get
4646
{
47-
return $"{_Api.ApiUrlBase}{Endpoint}";
47+
return $"{_Api.ApiUrlBase}{Endpoint}?{ApiVersionParameter}";
4848
}
4949
}
5050

51-
/// <summary>
52-
/// Gets an HTTPClient with the appropriate authorization and other headers set
53-
/// </summary>
54-
/// <returns>The fully initialized HttpClient</returns>
55-
/// <exception cref="AuthenticationException">Thrown if there is no valid authentication. Please refer to <see href="https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication"/> for details.</exception>
56-
protected HttpClient GetClient()
51+
/// <summary>
52+
/// Gets the version of the endpoint as url parameter, based on the configuration in the api definition. For example "https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#rest-api-versioning"
53+
/// </summary>
54+
protected string ApiVersionParameter
55+
{
56+
get
57+
{
58+
if(string.IsNullOrEmpty(_Api.ApiVersion))
59+
{
60+
return "";
61+
}
62+
return $"api-version={_Api.ApiVersion}";
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Gets an HTTPClient with the appropriate authorization and other headers set
68+
/// </summary>
69+
/// <returns>The fully initialized HttpClient</returns>
70+
/// <exception cref="AuthenticationException">Thrown if there is no valid authentication. Please refer to <see href="https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication"/> for details.</exception>
71+
protected HttpClient GetClient()
5772
{
5873
if (_Api.Auth?.ApiKey is null)
5974
{
@@ -62,7 +77,9 @@ protected HttpClient GetClient()
6277

6378
HttpClient client = new HttpClient();
6479
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Api.Auth.ApiKey);
65-
client.DefaultRequestHeaders.Add("User-Agent", Value);
80+
// Further authentication-header used for Azure openAI service
81+
client.DefaultRequestHeaders.Add("api-key", _Api.Auth.ApiKey);
82+
client.DefaultRequestHeaders.Add("User-Agent", Value);
6683
if (!string.IsNullOrEmpty(_Api.Auth.OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", _Api.Auth.OpenAIOrganization);
6784

6885
return client;

OpenAI_API/OpenAIAPI.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ public class OpenAIAPI
1414
/// Base url for OpenAI
1515
/// </summary>
1616
public string ApiUrlBase = "https://api.openai.com/v1/";
17-
18-
/// <summary>
19-
/// The API authentication information to use for API calls
20-
/// </summary>
21-
public APIAuthentication Auth { get; set; }
17+
18+
/// <summary>
19+
/// Version of the Rest Api. Needed for e.g. for the Azure OpenAI service.
20+
/// </summary>
21+
public string ApiVersion { get; set; }
22+
23+
/// <summary>
24+
/// The API authentication information to use for API calls
25+
/// </summary>
26+
public APIAuthentication Auth { get; set; }
2227

2328
/// <summary>
2429
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A simple C# .NET wrapper library to use with OpenAI's GPT-3 API. More context [
55
## Status
66
Updated to work with the current API as of February 2, 2023. Added Files and Embedding endpoints. Removed the Search endpoint as OpenAI has removed that API.
77
Potentially breaking change with v1.4: The various endpoints (Completions, Models, etc) and related classes have each moved into their own namespaces, for example `OpenAI_API.Completions.CompletionRequest` and `OpenAI_API.Models.Model.DavinciText`. You may need to add `using`s or fully qualify names in exisitng code.
8+
Now also works with the Azure OpenAI Service. See Azure section for further details.
89

910
Thank you [@GotMike](https://github.com/gotmike), [@gmilano](https://github.com/gmilano), [@metjuperry](https://github.com/metjuperry), and [@Alexei000](https://github.com/Alexei000) for your contributions!
1011

@@ -143,6 +144,30 @@ There are also methods to get file contents, delete a file, etc.
143144

144145
The fine-tuning endpoint itself has not yet been implemented, but will be added soon.
145146

147+
### Azure
148+
149+
For using the Azure OpenAI Service, you need to define the Api-Version of the OpenAIAPI class. Currently only the following version is suported by azure: `2022-12-01`.
150+
151+
Refer the Azure OpenAI documentation for further informations: [REST API versioning](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#rest-api-versioning)
152+
153+
Additionally you need to specify the BaseUrl to your API. The Url should look something like:
154+
```http
155+
https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}
156+
```
157+
158+
159+
Configuration should look something like this for the Azure service:
160+
161+
```csharp
162+
// authentication methods as specified above in authentication section
163+
OpenAIAPI api = new OpenAIAPI(); // uses default, env, or config file
164+
// configure your specific azure settings
165+
api.ApiUrlBase = "https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}";
166+
api.ApiVersion = "2022-12-01"
167+
```
168+
169+
The API-Key for Azure Service should be provided like the api-key for the OpenAI native API. Currently this library only supports the api-key flow, not the AD-Flow.
170+
146171
## Documentation
147172

148173
Every single class, method, and property has extensive XML documentation, so it should show up automatically in IntelliSense. That combined with the official OpenAI documentation should be enough to get started. Feel free to open an issue here if you have any questions. Better documentation may come later.

0 commit comments

Comments
 (0)