Skip to content

Commit

Permalink
Merge pull request #40 from ncface/develop
Browse files Browse the repository at this point in the history
Implement Azure OpenAI Service
  • Loading branch information
OkGoDoIt authored Feb 16, 2023
2 parents 7755f4c + fc8667d commit bc9043a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
19 changes: 18 additions & 1 deletion OpenAI_API/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ protected string Url
{
get
{
return $"{_Api.ApiUrlBase}{Endpoint}";
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}";
}
}

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

HttpClient client = new HttpClient();
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);
if (!string.IsNullOrEmpty(_Api.Auth.OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", _Api.Auth.OpenAIOrganization);

Expand Down
7 changes: 6 additions & 1 deletion OpenAI_API/OpenAIAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public class OpenAIAPI
/// Base url for OpenAI
/// </summary>
public string ApiUrlBase = "https://api.openai.com/v1/";


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

/// <summary>
/// The API authentication information to use for API calls
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Console.WriteLine(result);
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 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!

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

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

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

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)

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

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

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.

## Documentation

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

0 comments on commit bc9043a

Please sign in to comment.