Skip to content

Commit 7023ce5

Browse files
committed
Cleanup of Azure support
1 parent bc9043a commit 7023ce5

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

OpenAI_API/EndpointBase.cs

+3-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace OpenAI_API
1616
/// </summary>
1717
public abstract class EndpointBase
1818
{
19-
private const string Value = "okgodoit/dotnet_openai_api";
19+
private const string UserAgent = "okgodoit/dotnet_openai_api";
2020

2121
/// <summary>
2222
/// The internal reference to the API, mostly used for authentication
@@ -44,22 +44,7 @@ protected string Url
4444
{
4545
get
4646
{
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}";
47+
return string.Format(_Api.ApiUrlFormat, _Api.ApiVersion, Endpoint);
6348
}
6449
}
6550

@@ -87,7 +72,7 @@ protected HttpClient GetClient()
8772
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _Api.Auth.ApiKey);
8873
// Further authentication-header used for Azure openAI service
8974
client.DefaultRequestHeaders.Add("api-key", _Api.Auth.ApiKey);
90-
client.DefaultRequestHeaders.Add("User-Agent", Value);
75+
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
9176
if (!string.IsNullOrEmpty(_Api.Auth.OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", _Api.Auth.OpenAIOrganization);
9277

9378
return client;

OpenAI_API/OpenAIAPI.cs

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OpenAI_API.Embedding;
33
using OpenAI_API.Files;
44
using OpenAI_API.Models;
5+
using System.Xml.Linq;
56

67
namespace OpenAI_API
78
{
@@ -12,13 +13,15 @@ public class OpenAIAPI
1213
{
1314
/// <summary>
1415
/// Base url for OpenAI
16+
/// for OpenAI, should be "https://api.openai.com/{0}/{1}"
17+
/// for Azure, should be "https://(your-resource-name).openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}"
1518
/// </summary>
16-
public string ApiUrlBase = "https://api.openai.com/v1/";
19+
public string ApiUrlFormat { get; set; } = "https://api.openai.com/{0}/{1}";
1720

1821
/// <summary>
19-
/// Version of the Rest Api. Needed for e.g. for the Azure OpenAI service.
22+
/// Version of the Rest Api
2023
/// </summary>
21-
public string ApiVersion { get; set; }
24+
public string ApiVersion { get; set; } = "v1";
2225

2326
/// <summary>
2427
/// The API authentication information to use for API calls
@@ -38,6 +41,21 @@ public OpenAIAPI(APIAuthentication apiKeys = null)
3841
Embeddings = new EmbeddingEndpoint(this);
3942
}
4043

44+
/// <summary>
45+
/// Instantiates a version of the API for connecting to the Azure OpenAI endpoint instead of the main OpenAI endpoint.
46+
/// </summary>
47+
/// <param name="YourResourceName">The name of your Azure OpenAI Resource</param>
48+
/// <param name="deploymentId">The name of your model deployment. You're required to first deploy a model before you can make calls.</param>
49+
/// <param name="apiKey">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file. Currently this library only supports the api-key flow, not the AD-Flow.</param>
50+
/// <returns></returns>
51+
public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, APIAuthentication apiKey = null)
52+
{
53+
OpenAIAPI api = new OpenAIAPI(apiKey);
54+
api.ApiVersion = "2022-12-01";
55+
api.ApiUrlFormat = $"https://{YourResourceName}.openai.azure.com/openai/deployments/{deploymentId})/" + "{1}?api-version={0}";
56+
return api;
57+
}
58+
4159
/// <summary>
4260
/// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration).
4361
/// </summary>

README.md

+7-15
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ Console.WriteLine(result);
2727
## Status
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.
30+
Now also should work with the Azure OpenAI Service, although this is untested. See [Azure](#azure) section for further details.
3031

31-
Now also works with the Azure OpenAI Service. See [Azure](#azure) section for further details.
32-
33-
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!
32+
Thank you [@GotMike](https://github.com/gotmike), [@ncface](https://github.com/ncface), [@KeithHenry](https://github.com/KeithHenry), [@gmilano](https://github.com/gmilano), [@metjuperry](https://github.com/metjuperry), and [@Alexei000](https://github.com/Alexei000) for your contributions!
3433

3534
## Requirements
3635

@@ -158,26 +157,19 @@ The fine-tuning endpoint itself has not yet been implemented, but will be added
158157

159158
### Azure
160159

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`.
160+
For using the Azure OpenAI Service, you need to specify the name of your Azure OpenAI resource as well as your model deployment id. Additionally you may specify the Api version which defaults to `2022-12-01`.
162161

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)
162+
_I do not have access to the Microsoft Azure OpenAI service, so I am unable to test this functionality. If you have access and can test, please submit an issue describing your results. A PR with integration tests would also be greatly appreciated. Specifically, it is unclear to me that specifying models works the same way with Azure._
164163

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-
```
164+
Refer the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) for further information.
169165

170166
Configuration should look something like this for the Azure service:
171167

172168
```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"
169+
OpenAIAPI api = OpenAIAPI.ForAzure("YourResourceName", "deploymentId", "api-key");
178170
```
179171

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.
172+
You may then use the `api` object like normal. You may also specify the `APIAuthentication` is any of the other ways listed in the [Authentication](#authentication) section above. Currently this library only supports the api-key flow, not the AD-Flow.
181173

182174
## Documentation
183175

0 commit comments

Comments
 (0)