Skip to content

Commit

Permalink
Cleanup of Azure support
Browse files Browse the repository at this point in the history
  • Loading branch information
OkGoDoIt committed Feb 16, 2023
1 parent bc9043a commit 7023ce5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
21 changes: 3 additions & 18 deletions OpenAI_API/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace OpenAI_API
/// </summary>
public abstract class EndpointBase
{
private const string Value = "okgodoit/dotnet_openai_api";
private const string UserAgent = "okgodoit/dotnet_openai_api";

/// <summary>
/// The internal reference to the API, mostly used for authentication
Expand Down Expand Up @@ -44,22 +44,7 @@ protected string Url
{
get
{
return $"{_Api.ApiUrlBase}{Endpoint}?{ApiVersionParameter}";
}
}

/// <summary>
/// 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"
/// </summary>
protected string ApiVersionParameter
{
get
{
if(string.IsNullOrEmpty(_Api.ApiVersion))
{
return "";
}
return $"api-version={_Api.ApiVersion}";
return string.Format(_Api.ApiUrlFormat, _Api.ApiVersion, Endpoint);
}
}

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

return client;
Expand Down
24 changes: 21 additions & 3 deletions OpenAI_API/OpenAIAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenAI_API.Embedding;
using OpenAI_API.Files;
using OpenAI_API.Models;
using System.Xml.Linq;

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

/// <summary>
/// Version of the Rest Api. Needed for e.g. for the Azure OpenAI service.
/// Version of the Rest Api
/// </summary>
public string ApiVersion { get; set; }
public string ApiVersion { get; set; } = "v1";

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

/// <summary>
/// Instantiates a version of the API for connecting to the Azure OpenAI endpoint instead of the main OpenAI endpoint.
/// </summary>
/// <param name="YourResourceName">The name of your Azure OpenAI Resource</param>
/// <param name="deploymentId">The name of your model deployment. You're required to first deploy a model before you can make calls.</param>
/// <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>
/// <returns></returns>
public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, APIAuthentication apiKey = null)
{
OpenAIAPI api = new OpenAIAPI(apiKey);
api.ApiVersion = "2022-12-01";
api.ApiUrlFormat = $"https://{YourResourceName}.openai.azure.com/openai/deployments/{deploymentId})/" + "{1}?api-version={0}";
return api;
}

/// <summary>
/// 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).
/// </summary>
Expand Down
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ Console.WriteLine(result);
## Status
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.
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.
Now also should work with the Azure OpenAI Service, although this is untested. See [Azure](#azure) section for further details.

Now also works with the Azure OpenAI Service. See [Azure](#azure) section for further details.

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!
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!

## Requirements

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

### Azure

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`.
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`.

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)
_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._

Additionally you need to specify the BaseUrl to your API. The Url should look something like:
```http
https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}
```
Refer the [Azure OpenAI documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) for further information.

Configuration should look something like this for the Azure service:

```csharp
// authentication methods as specified above in authentication section
OpenAIAPI api = new OpenAIAPI(); // uses default, env, or config file
// configure your specific azure settings
api.ApiUrlBase = "https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}";
api.ApiVersion = "2022-12-01"
OpenAIAPI api = OpenAIAPI.ForAzure("YourResourceName", "deploymentId", "api-key");
```

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.
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.

## Documentation

Expand Down

0 comments on commit 7023ce5

Please sign in to comment.