A Non-Official OpenAI RESTful API Client for DotNet
A Non-Official OpenAI RESTful API Client for DotNet
A simple C# .NET client library for OpenAI to use though their RESTful API. Independently developed, this is not an official library and I am not affiliated with OpenAI. An OpenAI API account is required.
Originally Forked from OpenAI-API-dotnet. More context on Roger Pincombe's blog.
Install package OpenAI-DotNet from Nuget. Here's how via command line:
powershell:
Install-Package OpenAI-DotNetdotnet:
dotnet add package OpenAI-DotNetLooking to use OpenAI-DotNet in the Unity Game Engine? Check out our unity package on OpenUPM:
Check out our new api docs!
There are 3 ways to provide your API keys, in order of precedence:
[!WARNING] We recommended using the environment variables to load the API key instead of having it hard coded in your source. It is not recommended use this method in production, but only for accepting user credentials, local testing and quick start scenarios.
[!WARNING] We recommended using the environment variables to load the API key instead of having it hard coded in your source. It is not recommended use this method in production, but only for accepting user credentials, local testing and quick start scenarios.
using var api = new OpenAIClient("sk-apiKey");Or create a OpenAIAuthentication object manually
using var api = new OpenAIClient(new OpenAIAuthentication("sk-apiKey", "org-yourOrganizationId", "proj_yourProjectId"));Attempts to load api keys from a configuration file, by default .openai in the current directory, optionally traversing up the directory tree or in the user's home directory.
To create a configuration file, create a new text file named .openai and containing the line:
[!NOTE] Organization and project id entries are optional.
{
"apiKey": "sk-aaaabbbbbccccddddd",
"organizationId": "org-yourOrganizationId",
"projectId": "proj_yourProjectId"
}OPENAI_API_KEY=sk-aaaabbbbbccccddddd
OPENAI_ORGANIZATION_ID=org-yourOrganizationId
OPENAI_PROJECT_ID=proj_yourProjectIdYou can also load the configuration file directly with known path by calling static methods in OpenAIAuthentication:
.openai config in the specified directory:using var api = new OpenAIClient(OpenAIAuthentication.LoadFromDirectory("path/to/your/directory"));.openai as long as it conforms to the json format:using var api = new OpenAIClient(OpenAIAuthentication.LoadFromPath("path/to/your/file.json"));Use your system's environment variables specify an api key and organization to use.
OPENAI_API_KEY for your api key.OPENAI_ORGANIZATION_ID to specify an organization.OPENAI_PROJECT_ID to specify a project.using var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnvironment());OpenAIClient implements IDisposable to manage the lifecycle of the resources it uses, including HttpClient. When you initialize OpenAIClient, it will create an internal HttpClient instance if one is not provided. This internal HttpClient is disposed of when OpenAIClient is disposed of. If you provide an external HttpClient instance to OpenAIClient, you are responsible for managing its disposal.
OpenAIClient creates its own HttpClient, it will also take care of disposing it when you dispose OpenAIClient.HttpClient is passed to OpenAIClient, it will not be disposed of by OpenAIClient. You must manage the disposal of the HttpClient yourself.Please ensure to appropriately dispose of OpenAIClient to release resources timely and to prevent any potential memory or resource leaks in your application.
Typical usage with an internal HttpClient:
using var api = new OpenAIClient();Custom HttpClient (which you must dispose of yourself):
using var customHttpClient = new HttpClient();
// set custom http client properties here
var api = new OpenAIClient(client: customHttpClient);You can also choose to use Microsoft's Azure OpenAI deployments as well.
You can find the required information in the Azure Playground by clicking the View Code button and view a URL like this:
https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}your-resource-name The name of your Azure OpenAI Resource.deployment-id The deployment name you chose when you deployed the model.api-version The API version to use for this operation. This follows the YYYY-MM-DD format.To setup the client to use your deployment, you'll need to pass in OpenAISettings into the client constructor.
var auth = new OpenAIAuthentication("sk-apiKey");
var settings = new OpenAISettings(resourceName: "your-resource-name", deploymentId: "deployment-id", apiVersion: "api-version");
using var api = new OpenAIClient(auth, settings);Authenticate with MSAL as usual and get access token, then use the access token when creating your OpenAIAuthentication. Then be sure to set useAzureActiveDirectory to true when creating your OpenAISettings.
Tutorial: Desktop app that calls web APIs: Acquire a token
// getNo open issues yet, or sync has not completed.