Skip to content

Commit bc9043a

Browse files
authored
Merge pull request #40 from ncface/develop
Implement Azure OpenAI Service
2 parents 7755f4c + fc8667d commit bc9043a

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

OpenAI_API/EndpointBase.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ protected string Url
4444
{
4545
get
4646
{
47-
return $"{_Api.ApiUrlBase}{Endpoint}";
47+
return $"{_Api.ApiUrlBase}{Endpoint}?{ApiVersionParameter}";
48+
}
49+
}
50+
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}";
4863
}
4964
}
5065

@@ -70,6 +85,8 @@ protected HttpClient GetClient()
7085

7186
HttpClient client = new HttpClient();
7287
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Api.Auth.ApiKey);
88+
// Further authentication-header used for Azure openAI service
89+
client.DefaultRequestHeaders.Add("api-key", _Api.Auth.ApiKey);
7390
client.DefaultRequestHeaders.Add("User-Agent", Value);
7491
if (!string.IsNullOrEmpty(_Api.Auth.OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", _Api.Auth.OpenAIOrganization);
7592

OpenAI_API/OpenAIAPI.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public class OpenAIAPI
1414
/// Base url for OpenAI
1515
/// </summary>
1616
public string ApiUrlBase = "https://api.openai.com/v1/";
17-
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+
1823
/// <summary>
1924
/// The API authentication information to use for API calls
2025
/// </summary>

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Console.WriteLine(result);
2828
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.
2929
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 existing code.
3030

31+
Now also works with the Azure OpenAI Service. See [Azure](#azure) section for further details.
32+
3133
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!
3234

3335
## Requirements
@@ -154,6 +156,29 @@ There are also methods to get file contents, delete a file, etc.
154156

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

159+
### Azure
160+
161+
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`.
162+
163+
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)
164+
165+
Additionally you need to specify the BaseUrl to your API. The Url should look something like:
166+
```http
167+
https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}
168+
```
169+
170+
Configuration should look something like this for the Azure service:
171+
172+
```csharp
173+
// authentication methods as specified above in authentication section
174+
OpenAIAPI api = new OpenAIAPI(); // uses default, env, or config file
175+
// configure your specific azure settings
176+
api.ApiUrlBase = "https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}";
177+
api.ApiVersion = "2022-12-01"
178+
```
179+
180+
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.
181+
157182
## Documentation
158183

159184
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)