OpenAI-DotNet
7.6.1
See the version list below for details.
dotnet add package OpenAI-DotNet --version 7.6.1
NuGet\Install-Package OpenAI-DotNet -Version 7.6.1
<PackageReference Include="OpenAI-DotNet" Version="7.6.1" />
paket add OpenAI-DotNet --version 7.6.1
#r "nuget: OpenAI-DotNet, 7.6.1"
// Install OpenAI-DotNet as a Cake Addin #addin nuget:?package=OpenAI-DotNet&version=7.6.1 // Install OpenAI-DotNet as a Cake Tool #tool nuget:?package=OpenAI-DotNet&version=7.6.1
OpenAI-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.
Forked from OpenAI-API-dotnet. More context on Roger Pincombe's blog.
This repository is available to transfer to the OpenAI organization if they so choose to accept it.
Requirements
- This library targets .NET 6.0 and above.
- It should work across console apps, winforms, wpf, asp.net, etc.
- It should also work across Windows, Linux, and Mac.
Getting started
Install from NuGet
Install package OpenAI-DotNet
from Nuget. Here's how via command line:
Install-Package OpenAI-DotNet
Looking to use OpenAI-DotNet in the Unity Game Engine? Check out our unity package on OpenUPM:
Documentation
Table of Contents
- Authentication
- OpenAIClient
- Azure OpenAI
- OpenAI API Proxy
- Models
- Assistants 🆕
- Threads 🆕
- Chat
- Audio
- Images
- Files
- Fine Tuning
- Embeddings
- Moderations
Completions⚠️ DeprecatedStreaming⚠️ Deprecated
Edits⚠️ DeprecatedCreate Edit⚠️ Deprecated
Authentication
There are 3 ways to provide your API keys, in order of precedence:
- Pass keys directly with constructor
- Load key from configuration file
- Use System Environment Variables
You use the OpenAIAuthentication
when you initialize the API as shown:
Pass keys directly with constructor
⚠️ 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"));
Load key from configuration file
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:
Organization entry is optional.
Json format
{
"apiKey": "sk-aaaabbbbbccccddddd",
"organization": "org-yourOrganizationId"
}
Deprecated format
OPENAI_KEY=sk-aaaabbbbbccccddddd
ORGANIZATION=org-yourOrganizationId
You can also load the configuration file directly with known path by calling static methods in OpenAIAuthentication
:
- Loads the default
.openai
config in the specified directory:
using var api = new OpenAIClient(OpenAIAuthentication.LoadFromDirectory("path/to/your/directory"));
- Loads the configuration file from a specific path. File does not need to be named
.openai
as long as it conforms to the json format:
using var api = new OpenAIClient(OpenAIAuthentication.LoadFromPath("path/to/your/file.json"));
Use System Environment Variables
Use your system's environment variables specify an api key and organization to use.
- Use
OPENAI_API_KEY
for your api key. - Use
OPENAI_ORGANIZATION_ID
to specify an organization.
using var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv());
Handling OpenAIClient and HttpClient Lifecycle
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.
- If
OpenAIClient
creates its ownHttpClient
, it will also take care of disposing it when you disposeOpenAIClient
. - If an external
HttpClient
is passed toOpenAIClient
, it will not be disposed of byOpenAIClient
. You must manage the disposal of theHttpClient
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);
Azure OpenAI
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 OpenAIClientSettings
into the client constructor.
var auth = new OpenAIAuthentication("sk-apiKey");
var settings = new OpenAIClientSettings(resourceName: "your-resource-name", deploymentId: "deployment-id", apiVersion: "api-version");
using var api = new OpenAIClient(auth, settings);
Azure Active Directory Authentication
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 OpenAIClientSettings
.
Tutorial: Desktop app that calls web APIs: Acquire a token
// get your access token using any of the MSAL methods
var accessToken = result.AccessToken;
var auth = new OpenAIAuthentication(accessToken);
var settings = new OpenAIClientSettings(resourceName: "your-resource", deploymentId: "deployment-id", apiVersion: "api-version", useActiveDirectoryAuthentication: true);
using var api = new OpenAIClient(auth, settings);
OpenAI API Proxy
Using either the OpenAI-DotNet or com.openai.unity packages directly in your front-end app may expose your API keys and other sensitive information. To mitigate this risk, it is recommended to set up an intermediate API that makes requests to OpenAI on behalf of your front-end app. This library can be utilized for both front-end and intermediary host configurations, ensuring secure communication with the OpenAI API.
Front End Example
In the front end example, you will need to securely authenticate your users using your preferred OAuth provider. Once the user is authenticated, exchange your custom auth token with your API key on the backend.
Follow these steps:
- Setup a new project using either the OpenAI-DotNet or com.openai.unity packages.
- Authenticate users with your OAuth provider.
- After successful authentication, create a new
OpenAIAuthentication
object and pass in the custom token with the prefixsess-
. - Create a new
OpenAIClientSettings
object and specify the domain where your intermediate API is located. - Pass your new
auth
andsettings
objects to theOpenAIClient
constructor when you create the client instance.
Here's an example of how to set up the front end:
var authToken = await LoginAsync();
var auth = new OpenAIAuthentication($"sess-{authToken}");
var settings = new OpenAIClientSettings(domain: "api.your-custom-domain.com");
using var api = new OpenAIClient(auth, settings);
This setup allows your front end application to securely communicate with your backend that will be using the OpenAI-DotNet-Proxy, which then forwards requests to the OpenAI API. This ensures that your OpenAI API keys and other sensitive information remain secure throughout the process.
Back End Example
In this example, we demonstrate how to set up and use OpenAIProxyStartup
in a new ASP.NET Core web app. The proxy server will handle authentication and forward requests to the OpenAI API, ensuring that your API keys and other sensitive information remain secure.
- Create a new ASP.NET Core minimal web API project.
- Add the OpenAI-DotNet nuget package to your project.
- Powershell install:
Install-Package OpenAI-DotNet-Proxy
- Manually editing .csproj:
<PackageReference Include="OpenAI-DotNet-Proxy" />
- Powershell install:
- Create a new class that inherits from
AbstractAuthenticationFilter
and override theValidateAuthentication
method. This will implement theIAuthenticationFilter
that you will use to check user session token against your internal server. - In
Program.cs
, create a new proxy web application by callingOpenAIProxyStartup.CreateDefaultHost
method, passing your customAuthenticationFilter
as a type argument. - Create
OpenAIAuthentication
andOpenAIClientSettings
as you would normally with your API keys, org id, or Azure settings.
public partial class Program
{
private class AuthenticationFilter : AbstractAuthenticationFilter
{
public override void ValidateAuthentication(IHeaderDictionary request)
{
// You will need to implement your own class to properly test
// custom issued tokens you've setup for your end users.
if (!request.Authorization.ToString().Contains(userToken))
{
throw new AuthenticationException("User is not authorized");
}
}
}
public static void Main(string[] args)
{
var auth = OpenAIAuthentication.LoadFromEnv();
var settings = new OpenAIClientSettings(/* your custom settings if using Azure OpenAI */);
using var openAIClient = new OpenAIClient(auth, settings);
var proxy = OpenAIProxyStartup.CreateDefaultHost<AuthenticationFilter>(args, openAIClient);
proxy.Run();
}
}
Once you have set up your proxy server, your end users can now make authenticated requests to your proxy api instead of directly to the OpenAI API. The proxy server will handle authentication and forward requests to the OpenAI API, ensuring that your API keys and other sensitive information remain secure.
Models
List and describe the various models available in the API. You can refer to the Models documentation to understand what models are available and the differences between them.
Also checkout model endpoint compatibility to understand which models work with which endpoints.
To specify a custom model not pre-defined in this library:
var model = new Model("model-id");
The Models API is accessed via OpenAIClient.ModelsEndpoint
List models
Lists the currently available models, and provides basic information about each one such as the owner and availability.
using var api = new OpenAIClient();
var models = await api.ModelsEndpoint.GetModelsAsync();
foreach (var model in models)
{
Console.WriteLine(model.ToString());
}
Retrieve model
Retrieves a model instance, providing basic information about the model such as the owner and permissions.
using var api = new OpenAIClient();
var model = await api.ModelsEndpoint.GetModelDetailsAsync("text-davinci-003");
Console.WriteLine(model.ToString());
Delete Fine Tuned Model
Delete a fine-tuned model. You must have the Owner role in your organization.
using var api = new OpenAIClient();
var isDeleted = await api.ModelsEndpoint.DeleteFineTuneModelAsync("your-fine-tuned-model");
Assert.IsTrue(isDeleted);
Assistants
⚠️ Beta Feature
Build assistants that can call models and use tools to perform tasks.
The Assistants API is accessed via OpenAIClient.AssistantsEndpoint
List Assistants
Returns a list of assistants.
using var api = new OpenAIClient();
var assistantsList = await api.AssistantsEndpoint.ListAssistantsAsync();
foreach (var assistant in assistantsList.Items)
{
Console.WriteLine($"{assistant} -> {assistant.CreatedAt}");
}
Create Assistant
Create an assistant with a model and instructions.
using var api = new OpenAIClient();
var request = new CreateAssistantRequest("gpt-3.5-turbo-1106");
var assistant = await api.AssistantsEndpoint.CreateAssistantAsync(request);
Retrieve Assistant
Retrieves an assistant.
using var api = new OpenAIClient();
var assistant = await api.AssistantsEndpoint.RetrieveAssistantAsync("assistant-id");
Console.WriteLine($"{assistant} -> {assistant.CreatedAt}");
Modify Assistant
Modifies an assistant.
using var api = new OpenAIClient();
var createRequest = new CreateAssistantRequest("gpt-3.5-turbo-1106");
var assistant = await api.AssistantsEndpoint.CreateAssistantAsync(createRequest);
var modifyRequest = new CreateAssistantRequest("gpt-4-1106-preview");
var modifiedAssistant = await api.AssistantsEndpoint.ModifyAsync(assistant.Id, modifyRequest);
// OR AssistantExtension for easier use!
var modifiedAssistantEx = await assistant.ModifyAsync(modifyRequest);
Delete Assistant
Delete an assistant.
using var api = new OpenAIClient();
var isDeleted = await api.AssistantsEndpoint.DeleteAssistantAsync("assistant-id");
// OR AssistantExtension for easier use!
var isDeleted = await assistant.DeleteAsync();
Assert.IsTrue(isDeleted);
List Assistant Files
Returns a list of assistant files.
using var api = new OpenAIClient();
var filesList = await api.AssistantsEndpoint.ListFilesAsync("assistant-id");
// OR AssistantExtension for easier use!
var filesList = await assistant.ListFilesAsync();
foreach (var file in filesList.Items)
{
Console.WriteLine($"{file.AssistantId}'s file -> {file.Id}");
}
Attach File to Assistant
Create an assistant file by attaching a File to an assistant.
using var api = new OpenAIClient();
var filePath = "assistant_test_2.txt";
await File.WriteAllTextAsync(filePath, "Knowledge is power!");
var fileUploadRequest = new FileUploadRequest(filePath, "assistant");
var file = await api.FilesEndpoint.UploadFileAsync(fileUploadRequest);
var assistantFile = await api.AssistantsEndpoint.AttachFileAsync("assistant-id", file.Id);
// OR use extension method for convenience!
var assistantFIle = await assistant.AttachFileAsync(file);
Upload File to Assistant
Uploads and attaches a file to an assistant.
Assistant extension method, for extra convenience!
using var api = new OpenAIClient();
var filePath = "assistant_test_2.txt";
await File.WriteAllTextAsync(filePath, "Knowledge is power!");
var assistantFile = await assistant.UploadFileAsync(filePath);
Retrieve File from Assistant
Retrieves an AssistantFile.
using var api = new OpenAIClient();
var assistantFile = await api.AssistantsEndpoint.RetrieveFileAsync("assistant-id", "file-id");
// OR AssistantExtension for easier use!
var assistantFile = await assistant.RetrieveFileAsync(fileId);
Console.WriteLine($"{assistantFile.AssistantId}'s file -> {assistantFile.Id}");
Remove File from Assistant
Remove a file from an assistant.
Note: The file will remain in your organization until deleted with FileEndpoint.
using var api = new OpenAIClient();
var isRemoved = await api.AssistantsEndpoint.RemoveFileAsync("assistant-id", "file-id");
// OR use extension method for convenience!
var isRemoved = await assistant.RemoveFileAsync("file-id");
Assert.IsTrue(isRemoved);
Delete File from Assistant
Removes a file from the assistant and then deletes the file from the organization.
Assistant extension method, for extra convenience!
using var api = new OpenAIClient();
var isDeleted = await assistant.DeleteFileAsync("file-id");
Assert.IsTrue(isDeleted);
Threads
⚠️ Beta Feature
Create Threads that Assistants can interact with.
The Threads API is accessed via OpenAIClient.ThreadsEndpoint
Create Thread
Create a thread.
using var api = new OpenAIClient();
var thread = await api.ThreadsEndpoint.CreateThreadAsync();
Console.WriteLine($"Create thread {thread.Id} -> {thread.CreatedAt}");
Create Thread and Run
Create a thread and run it in one request.
See also: Thread Runs
using var api = new OpenAIClient();
var assistant = await api.AssistantsEndpoint.CreateAssistantAsync(
new CreateAssistantRequest(
name: "Math Tutor",
instructions: "You are a personal math tutor. Answer questions briefly, in a sentence or less.",
model: "gpt-4-1106-preview"));
var messages = new List<Message> { "I need to solve the equation `3x + 11 = 14`. Can you help me?" };
var threadRequest = new CreateThreadRequest(messages);
var run = await assistant.CreateThreadAndRunAsync(threadRequest);
Console.WriteLine($"Created thread and run: {run.ThreadId} -> {run.Id} -> {run.CreatedAt}");
Retrieve Thread
Retrieves a thread.
using var api = new OpenAIClient();
var thread = await api.ThreadsEndpoint.RetrieveThreadAsync("thread-id");
// OR if you simply wish to get the latest state of a thread
thread = await thread.UpdateAsync();
Console.WriteLine($"Retrieve thread {thread.Id} -> {thread.CreatedAt}");
Modify Thread
Modifies a thread.
Note: Only the metadata can be modified.
using var api = new OpenAIClient();
var thread = await api.ThreadsEndpoint.CreateThreadAsync();
var metadata = new Dictionary<string, string>
{
{ "key", "custom thread metadata" }
}
thread = await api.ThreadsEndpoint.ModifyThreadAsync(thread.Id, metadata);
// OR use extension method for convenience!
thread = await thread.ModifyAsync(metadata);
Console.WriteLine($"Modify thread {thread.Id} -> {thread.Metadata["key"]}");
Delete Thread
Delete a thread.
using var api = new OpenAIClient();
var isDeleted = await api.ThreadsEndpoint.DeleteThreadAsync("thread-id");
// OR use extension method for convenience!
var isDeleted = await thread.DeleteAsync();
Assert.IsTrue(isDeleted);
Thread Messages
Create messages within threads.
List Thread Messages
Returns a list of messages for a given thread.
using var api = new OpenAIClient();
var messageList = await api.ThreadsEndpoint.ListMessagesAsync("thread-id");
// OR use extension method for convenience!
var messageList = await thread.ListMessagesAsync();
foreach (var message in messageList.Items)
{
Console.WriteLine($"{message.Id}: {message.Role}: {message.PrintContent()}");
}
Create Thread Message
Create a message.
using var api = new OpenAIClient();
var thread = await api.ThreadsEndpoint.CreateThreadAsync();
var request = new CreateMessageRequest("Hello world!");
var message = await api.ThreadsEndpoint.CreateMessageAsync(thread.Id, request);
// OR use extension method for convenience!
var message = await thread.CreateMessageAsync("Hello World!");
Console.WriteLine($"{message.Id}: {message.Role}: {message.PrintContent()}");
Retrieve Thread Message
Retrieve a message.
using var api = new OpenAIClient();
var message = await api.ThreadsEndpoint.RetrieveMessageAsync("thread-id", "message-id");
// OR use extension methods for convenience!
var message = await thread.RetrieveMessageAsync("message-id");
var message = await message.UpdateAsync();
Console.WriteLine($"{message.Id}: {message.Role}: {message.PrintContent()}");
Modify Thread Message
Modify a message.
Note: Only the message metadata can be modified.
using var api = new OpenAIClient();
var metadata = new Dictionary<string, string>
{
{ "key", "custom message metadata" }
};
var message = await api.ThreadsEndpoint.ModifyMessageAsync("thread-id", "message-id", metadata);
// OR use extension method for convenience!
var message = await message.ModifyAsync(metadata);
Console.WriteLine($"Modify message metadata: {message.Id} -> {message.Metadata["key"]}");
Thread Message Files
List Thread Message Files
Returns a list of message files.
using var api = new OpenAIClient();
var fileList = await api.ThreadsEndpoint.ListFilesAsync("thread-id", "message-Id");
// OR use extension method for convenience!
var fileList = await thread.ListFilesAsync("message-id");
var fileList = await message.ListFilesAsync();
foreach (var file in fileList.Items)
{
Console.WriteLine(file.Id);
}
Retrieve Thread Message File
Retrieves a message file.
using var api = new OpenAIClient();
var file = await api.ThreadsEndpoint.RetrieveFileAsync("thread-id", "message-id", "file-id");
// OR use extension method for convenience!
var file = await message.RetrieveFileAsync();
Console.WriteLine(file.Id);
Thread Runs
Represents an execution run on a thread.
List Thread Runs
Returns a list of runs belonging to a thread.
using var api = new OpenAIClient();
var runList = await api.ThreadsEndpoint.ListRunsAsync("thread-id");
// OR use extension method for convenience!
var runList = await thread.ListRunsAsync();
foreach (var run in runList.Items)
{
Console.WriteLine($"[{run.Id}] {run.Status} | {run.CreatedAt}");
}
Create Thread Run
Create a run.
using var api = new OpenAIClient();
var assistant = await api.AssistantsEndpoint.CreateAssistantAsync(
new CreateAssistantRequest(
name: "Math Tutor",
instructions: "You are a personal math tutor. Answer questions briefly, in a sentence or less.",
model: "gpt-4-1106-preview"));
var thread = await api.ThreadsEndpoint.CreateThreadAsync();
var message = await thread.CreateMessageAsync("I need to solve the equation `3x + 11 = 14`. Can you help me?");
var run = await thread.CreateRunAsync(assistant);
Console.WriteLine($"[{run.Id}] {run.Status} | {run.CreatedAt}");
Retrieve Thread Run
Retrieves a run.
using var api = new OpenAIClient();
var run = await api.ThreadsEndpoint.RetrieveRunAsync("thread-id", "run-id");
// OR use extension method for convenience!
var run = await thread.RetrieveRunAsync("run-id");
var run = await run.UpdateAsync();
Console.WriteLine($"[{run.Id}] {run.Status} | {run.CreatedAt}");
Modify Thread Run
Modifies a run.
Note: Only the metadata can be modified.
using var api = new OpenAIClient();
var metadata = new Dictionary<string, string>
{
{ "key", "custom run metadata" }
};
var run = await api.ThreadsEndpoint.ModifyRunAsync("thread-id", "run-id", metadata);
// OR use extension method for convenience!
var run = await run.ModifyAsync(metadata);
Console.WriteLine($"Modify run {run.Id} -> {run.Metadata["key"]}");
Thread Submit Tool Outputs to Run
When a run has the status: requires_action
and required_action.type
is submit_tool_outputs
, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.
using var api = new OpenAIClient();
var function = new Function(
nameof(WeatherService.GetCurrentWeather),
"Get the current weather in a given location",
new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["location"] = new JsonObject
{
["type"] = "string",
["description"] = "The city and state, e.g. San Francisco, CA"
},
["unit"] = new JsonObject
{
["type"] = "string",
["enum"] = new JsonArray { "celsius", "fahrenheit" }
}
},
["required"] = new JsonArray { "location", "unit" }
});
testAssistant = await api.AssistantsEndpoint.CreateAssistantAsync(new CreateAssistantRequest(tools: new Tool[] { function }));
var run = await testAssistant.CreateThreadAndRunAsync("I'm in Kuala-Lumpur, please tell me what's the temperature in celsius now?");
// waiting while run is Queued and InProgress
run = await run.WaitForStatusChangeAsync();
var toolCall = run.RequiredAction.SubmitToolOutputs.ToolCalls[0];
Console.WriteLine($"tool call arguments: {toolCall.FunctionCall.Arguments}");
var functionArgs = JsonSerializer.Deserialize<WeatherArgs>(toolCall.FunctionCall.Arguments);
var functionResult = WeatherService.GetCurrentWeather(functionArgs);
var toolOutput = new ToolOutput(toolCall.Id, functionResult);
run = await run.SubmitToolOutputsAsync(toolOutput);
// waiting while run in Queued and InProgress
run = await run.WaitForStatusChangeAsync();
var messages = await run.ListMessagesAsync();
foreach (var message in messages.Items.OrderBy(response => response.CreatedAt))
{
Console.WriteLine($"{message.Role}: {message.PrintContent()}");
}
List Thread Run Steps
Returns a list of run steps belonging to a run.
using var api = new OpenAIClient();
var runStepList = await api.ThreadsEndpoint.ListRunStepsAsync("thread-id", "run-id");
// OR use extension method for convenience!
var runStepList = await run.ListRunStepsAsync();
foreach (var runStep in runStepList.Items)
{
Console.WriteLine($"[{runStep.Id}] {runStep.Status} {runStep.CreatedAt} -> {runStep.ExpiresAt}");
}
Retrieve Thread Run Step
Retrieves a run step.
using var api = new OpenAIClient();
var runStep = await api.ThreadsEndpoint.RetrieveRunStepAsync("thread-id", "run-id", "step-id");
// OR use extension method for convenience!
var runStep = await run.RetrieveRunStepAsync("step-id");
var runStep = await runStep.UpdateAsync();
Console.WriteLine($"[{runStep.Id}] {runStep.Status} {runStep.CreatedAt} -> {runStep.ExpiresAt}");
Cancel Thread Run
Cancels a run that is in_progress
.
using var api = new OpenAIClient();
var isCancelled = await api.ThreadsEndpoint.CancelRunAsync("thread-id", "run-id");
// OR use extension method for convenience!
var isCancelled = await run.CancelAsync();
Assert.IsTrue(isCancelled);
Chat
Given a chat conversation, the model will return a chat completion response.
The Chat API is accessed via OpenAIClient.ChatEndpoint
Chat Completions
Creates a completion for the chat message
using var api = new OpenAIClient();
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful assistant."),
new Message(Role.User, "Who won the world series in 2020?"),
new Message(Role.Assistant, "The Los Angeles Dodgers won the World Series in 2020."),
new Message(Role.User, "Where was it played?"),
};
var chatRequest = new ChatRequest(messages, Model.GPT4);
var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
var choice = response.FirstChoice;
Console.WriteLine($"[{choice.Index}] {choice.Message.Role}: {choice.Message} | Finish Reason: {choice.FinishReason}");
Chat Streaming
using var api = new OpenAIClient();
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful assistant."),
new Message(Role.User, "Who won the world series in 2020?"),
new Message(Role.Assistant, "The Los Angeles Dodgers won the World Series in 2020."),
new Message(Role.User, "Where was it played?"),
};
var chatRequest = new ChatRequest(messages);
var response = await api.ChatEndpoint.StreamCompletionAsync(chatRequest, partialResponse =>
{
Console.Write(partialResponse.FirstChoice.Delta.ToString());
});
var choice = response.FirstChoice;
Console.WriteLine($"[{choice.Index}] {choice.Message.Role}: {choice.Message} | Finish Reason: {choice.FinishReason}");
Or if using IAsyncEnumerable{T}
(C# 8.0+)
using var api = new OpenAIClient();
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful assistant."),
new Message(Role.User, "Who won the world series in 2020?"),
new Message(Role.Assistant, "The Los Angeles Dodgers won the World Series in 2020."),
new Message(Role.User, "Where was it played?"),
};
var cumulativeDelta = string.Empty;
var chatRequest = new ChatRequest(messages);
await foreach (var partialResponse in OpenAIClient.ChatEndpoint.StreamCompletionEnumerableAsync(chatRequest))
{
foreach (var choice in partialResponse.Choices.Where(choice => choice.Delta?.Content != null))
{
cumulativeDelta += choice.Delta.Content;
}
}
Console.WriteLine(cumulativeDelta);
Chat Tools
Only available with the latest 0613 model series!
using var api = new OpenAIClient();
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful weather assistant."),
new Message(Role.User, "What's the weather like today?"),
};
foreach (var message in messages)
{
Console.WriteLine($"{message.Role}: {message}");
}
// Define the tools that the assistant is able to use:
var tools = new List<Tool>
{
new Function(
nameof(WeatherService.GetCurrentWeather),
"Get the current weather in a given location",
new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["location"] = new JsonObject
{
["type"] = "string",
["description"] = "The city and state, e.g. San Francisco, CA"
},
["unit"] = new JsonObject
{
["type"] = "string",
["enum"] = new JsonArray {"celsius", "fahrenheit"}
}
},
["required"] = new JsonArray { "location", "unit" }
})
};
var chatRequest = new ChatRequest(messages, tools: tools, toolChoice: "auto");
var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
messages.Add(response.FirstChoice.Message);
Console.WriteLine($"{response.FirstChoice.Message.Role}: {response.FirstChoice} | Finish Reason: {response.FirstChoice.FinishReason}");
var locationMessage = new Message(Role.User, "I'm in Glasgow, Scotland");
messages.Add(locationMessage);
Console.WriteLine($"{locationMessage.Role}: {locationMessage.Content}");
chatRequest = new ChatRequest(messages, tools: tools, toolChoice: "auto");
response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
messages.Add(response.FirstChoice.Message);
if (!string.IsNullOrEmpty(response.ToString()))
{
Console.WriteLine($"{response.FirstChoice.Message.Role}: {response.FirstChoice} | Finish Reason: {response.FirstChoice.FinishReason}");
var unitMessage = new Message(Role.User, "celsius");
messages.Add(unitMessage);
Console.WriteLine($"{unitMessage.Role}: {unitMessage.Content}");
chatRequest = new ChatRequest(messages, tools: tools, toolChoice: "auto");
response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
}
var usedTool = response.FirstChoice.Message.ToolCalls[0];
Console.WriteLine($"{response.FirstChoice.Message.Role}: {usedTool.Function.Name} | Finish Reason: {response.FirstChoice.FinishReason}");
Console.WriteLine($"{usedTool.Function.Arguments}");
var functionArgs = JsonSerializer.Deserialize<WeatherArgs>(usedTool.Function.Arguments.ToString());
var functionResult = WeatherService.GetCurrentWeather(functionArgs);
messages.Add(new Message(usedTool, functionResult));
Console.WriteLine($"{Role.Tool}: {functionResult}");
// System: You are a helpful weather assistant.
// User: What's the weather like today?
// Assistant: Sure, may I know your current location? | Finish Reason: stop
// User: I'm in Glasgow, Scotland
// Assistant: GetCurrentWeather | Finish Reason: tool_calls
// {
// "location": "Glasgow, Scotland",
// "unit": "celsius"
// }
// Tool: The current weather in Glasgow, Scotland is 20 celsius
Chat Vision
⚠️ Beta Feature Currently, GPT-4 with vision does not support the
message.name
parameter, functions/tools, nor theresponse_format
parameter.
using var api = new OpenAIClient();
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful assistant."),
new Message(Role.User, new List<Content>
{
"What's in this image?",
new ImageUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", ImageDetail.Low)
})
};
var chatRequest = new ChatRequest(messages, model: "gpt-4-vision-preview");
var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
Console.WriteLine($"{response.FirstChoice.Message.Role}: {response.FirstChoice.Message.Content} | Finish Reason: {response.FirstChoice.FinishDetails}");
Chat Json Mode
⚠️ Beta Feature
Important notes:
- When using JSON mode, always instruct the model to produce JSON via some message in the conversation, for example via your system message. If you don't include an explicit instruction to generate JSON, the model may generate an unending stream of whitespace and the request may run continually until it reaches the token limit. To help ensure you don't forget, the API will throw an error if the string "JSON" does not appear somewhere in the context.
- The JSON in the message the model returns may be partial (i.e. cut off) if
finish_reason
is length, which indicates the generation exceeded max_tokens or the conversation exceeded the token limit. To guard against this, checkfinish_reason
before parsing the response. - JSON mode will not guarantee the output matches any specific schema, only that it is valid and parses without errors.
var messages = new List<Message>
{
new Message(Role.System, "You are a helpful assistant designed to output JSON."),
new Message(Role.User, "Who won the world series in 2020?"),
};
var chatRequest = new ChatRequest(messages, "gpt-4-1106-preview", responseFormat: ChatResponseFormat.Json);
var response = await api.ChatEndpoint.GetCompletionAsync(chatRequest);
foreach (var choice in response.Choices)
{
Console.WriteLine($"[{choice.Index}] {choice.Message.Role}: {choice} | Finish Reason: {choice.FinishReason}");
}
response.GetUsage();
Audio
Converts audio into text.
The Audio API is accessed via OpenAIClient.AudioEndpoint
Create Speech
Generates audio from the input text.
using var api = new OpenAIClient();
var request = new SpeechRequest("Hello World!");
async Task ChunkCallback(ReadOnlyMemory<byte> chunkCallback)
{
// TODO Implement audio playback as chunks arrive
await Task.CompletedTask;
}
var response = await api.AudioEndpoint.CreateSpeechAsync(request, ChunkCallback);
await File.WriteAllBytesAsync("../../../Assets/HelloWorld.mp3", response.ToArray());
Create Transcription
Transcribes audio into the input language.
using var api = new OpenAIClient();
var request = new AudioTranscriptionRequest(Path.GetFullPath(audioAssetPath), language: "en");
var response = await api.AudioEndpoint.CreateTranscriptionAsync(request);
Console.WriteLine(response);
Create Translation
Translates audio into into English.
using var api = new OpenAIClient();
var request = new AudioTranslationRequest(Path.GetFullPath(audioAssetPath));
var response = await api.AudioEndpoint.CreateTranslationAsync(request);
Console.WriteLine(response);
Images
Given a prompt and/or an input image, the model will generate a new image.
The Images API is accessed via OpenAIClient.ImagesEndpoint
Create Image
Creates an image given a prompt.
using var api = new OpenAIClient();
var request = new ImageGenerationRequest("A house riding a velociraptor", Models.Model.DallE_3);
var imageResults = await api.ImagesEndPoint.GenerateImageAsync(request);
foreach (var image in imageResults)
{
Console.WriteLine(image);
// image == url or b64_string
}
Edit Image
Creates an edited or extended image given an original image and a prompt.
using var api = new OpenAIClient();
var request = new ImageEditRequest(imageAssetPath, maskAssetPath, "A sunlit indoor lounge area with a pool containing a flamingo", size: ImageSize.Small);
var imageResults = await api.ImagesEndPoint.CreateImageEditAsync(request);
foreach (var image in imageResults)
{
Console.WriteLine(image);
// image == url or b64_string
}
Create Image Variation
Creates a variation of a given image.
using var api = new OpenAIClient();
var request = new ImageVariationRequest(imageAssetPath, size: ImageSize.Small);
var imageResults = await api.ImagesEndPoint.CreateImageVariationAsync(request);
foreach (var image in imageResults)
{
Console.WriteLine(image);
// image == url or b64_string
}
Files
Files are used to upload documents that can be used with features like Fine-tuning.
The Files API is accessed via OpenAIClient.FilesEndpoint
List Files
Returns a list of files that belong to the user's organization.
using var api = new OpenAIClient();
var fileList = await api.FilesEndpoint.ListFilesAsync();
foreach (var file in fileList)
{
Console.WriteLine($"{file.Id} -> {file.Object}: {file.FileName} | {file.Size} bytes");
}
Upload File
Upload a file that can be used across various endpoints. The size of all the files uploaded by one organization can be up to 100 GB.
The size of individual files can be a maximum of 512 MB. See the Assistants Tools guide to learn more about the types of files supported. The Fine-tuning API only supports .jsonl files.
using var api = new OpenAIClient();
var file = await api.FilesEndpoint.UploadFileAsync("path/to/your/file.jsonl", "fine-tune");
Console.WriteLine(file.Id);
Delete File
Delete a file.
using var api = new OpenAIClient();
var isDeleted = await api.FilesEndpoint.DeleteFileAsync(fileId);
Assert.IsTrue(isDeleted);
Retrieve File Info
Returns information about a specific file.
using var api = new OpenAIClient();
var file = await GetFileInfoAsync(fileId);
Console.WriteLine($"{file.Id} -> {file.Object}: {file.FileName} | {file.Size} bytes");
Download File Content
Downloads the file content to the specified directory.
using var api = new OpenAIClient();
var downloadedFilePath = await api.FilesEndpoint.DownloadFileAsync(fileId, "path/to/your/save/directory");
Console.WriteLine(downloadedFilePath);
Assert.IsTrue(File.Exists(downloadedFilePath));
Fine Tuning
Manage fine-tuning jobs to tailor a model to your specific training data.
Related guide: Fine-tune models
The Files API is accessed via OpenAIClient.FineTuningEndpoint
Create Fine Tune Job
Creates a job that fine-tunes a specified model from a given dataset.
Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
using var api = new OpenAIClient();
var fileId = "file-abc123";
var request = new CreateFineTuneRequest(fileId);
var job = await api.FineTuningEndpoint.CreateJobAsync(Model.GPT3_5_Turbo, request);
Console.WriteLine($"Started {job.Id} | Status: {job.Status}");
List Fine Tune Jobs
List your organization's fine-tuning jobs.
using var api = new OpenAIClient();
var jobList = await api.FineTuningEndpoint.ListJobsAsync();
foreach (var job in jobList.Items.OrderByDescending(job => job.CreatedAt)))
{
Console.WriteLine($"{job.Id} -> {job.CreatedAt} | {job.Status}");
}
Retrieve Fine Tune Job Info
Gets info about the fine-tune job.
using var api = new OpenAIClient();
var job = await api.FineTuningEndpoint.GetJobInfoAsync(fineTuneJob);
Console.WriteLine($"{job.Id} -> {job.CreatedAt} | {job.Status}");
Cancel Fine Tune Job
Immediately cancel a fine-tune job.
using var api = new OpenAIClient();
var isCancelled = await api.FineTuningEndpoint.CancelFineTuneJobAsync(fineTuneJob);
Assert.IsTrue(isCancelled);
List Fine Tune Job Events
Get status updates for a fine-tuning job.
using var api = new OpenAIClient();
var eventList = await api.FineTuningEndpoint.ListJobEventsAsync(fineTuneJob);
Console.WriteLine($"{fineTuneJob.Id} -> status: {fineTuneJob.Status} | event count: {eventList.Events.Count}");
foreach (var @event in eventList.Items.OrderByDescending(@event => @event.CreatedAt))
{
Console.WriteLine($" {@event.CreatedAt} [{@event.Level}] {@event.Message}");
}
Embeddings
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
Related guide: Embeddings
The Edits API is accessed via OpenAIClient.EmbeddingsEndpoint
Create Embeddings
Creates an embedding vector representing the input text.
using var api = new OpenAIClient();
var response = await api.EmbeddingsEndpoint.CreateEmbeddingAsync("The food was delicious and the waiter...", Models.Embedding_Ada_002);
Console.WriteLine(response);
Moderations
Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
Related guide: Moderations
The Moderations API can be accessed via OpenAIClient.ModerationsEndpoint
Create Moderation
Classifies if text violates OpenAI's Content Policy.
using var api = new OpenAIClient();
var isViolation = await api.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(isViolation);
Additionally you can also get the scores of a given input.
using var api = new OpenAIClient();
var response = await api.ModerationsEndpoint.CreateModerationAsync(new ModerationsRequest("I love you"));
Assert.IsNotNull(response);
Console.WriteLine(response.Results?[0]?.Scores?.ToString());
Completions
⚠️ Deprecated, and soon to be removed.
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
The Completions API is accessed via OpenAIClient.CompletionsEndpoint
using var api = new OpenAIClient();
var response = await api.CompletionsEndpoint.CreateCompletionAsync("One Two Three One Two", temperature: 0.1, model: Model.Davinci);
Console.WriteLine(response);
To get the
CompletionResponse
(which is mostly metadata), use its implicit string operator to get the text if all you want is the completion choice.
Completion Streaming
⚠️ Deprecated, and soon to be removed.
Streaming allows you to get results are they are generated, which can help your application feel more responsive, especially on slow models like Davinci.
using var api = new OpenAIClient();
await api.CompletionsEndpoint.StreamCompletionAsync(response =>
{
foreach (var choice in response.Completions)
{
Console.WriteLine(choice);
}
}, "My name is Roger and I am a principal software engineer at Salesforce. This is my resume:", maxTokens: 200, temperature: 0.5, presencePenalty: 0.1, frequencyPenalty: 0.1, model: Model.Davinci);
Or if using IAsyncEnumerable{T}
(C# 8.0+)
using var api = new OpenAIClient();
await foreach (var partialResponse in api.CompletionsEndpoint.StreamCompletionEnumerableAsync("My name is Roger and I am a principal software engineer at Salesforce. This is my resume:", maxTokens: 200, temperature: 0.5, presencePenalty: 0.1, frequencyPenalty: 0.1, model: Model.Davinci))
{
Console.WriteLine(partialResponse);
}
Edits
⚠️ Deprecated, and soon to be removed.
Given a prompt and an instruction, the model will return an edited version of the prompt.
The Edits API is accessed via OpenAIClient.EditsEndpoint
Create Edit
Creates a new edit for the provided input, instruction, and parameters using the provided input and instruction.
using var api = new OpenAIClient();
var request = new EditRequest("What day of the wek is it?", "Fix the spelling mistakes");
var response = await api.EditsEndpoint.CreateEditAsync(request);
Console.WriteLine(response);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages (11)
Showing the top 5 NuGet packages that depend on OpenAI-DotNet:
Package | Downloads |
---|---|
OpenAI-DotNet-Proxy
A simple Proxy API gateway for OpenAI-DotNet to make authenticated requests from a front end application without exposing your API keys. |
|
WK.OpenAiWrapper
Package Description |
|
NewLeadAI
A client to interact with NewLead AI API |
|
AICore.Core
Package Description |
|
Universal.Tools.Core.Requests.Extensions.OpenAI
Package Description |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on OpenAI-DotNet:
Repository | Stars |
---|---|
dkgv/pinpoint
Keystroke launcher and personal command central. Alternative to Spotlight and Alfred for Windows. Alternative to Wox, PowerToys.
|
|
SlimeNull/OpenGptChat
An OpenAI Chat completion Client. 一个 OpenAI 聊天 Completion 客户端.
|
|
BoiHanny/vrcosc-magicchatbox
The ultimate companion, whether you're on desktop or in VR, we've got you covered with our handy integrations in a compact and modern UI
|
Version | Downloads | Last updated | |
---|---|---|---|
8.4.1 | 1,343 | 11/15/2024 | |
8.4.0 | 216 | 11/15/2024 | |
8.3.0 | 29,785 | 9/19/2024 | |
8.2.5 | 4,641 | 9/14/2024 | |
8.2.4 | 699 | 9/14/2024 | |
8.2.2 | 14,697 | 8/19/2024 | |
8.2.1 | 335 | 8/19/2024 | |
8.2.0 | 589 | 8/18/2024 | |
8.1.2 | 9,408 | 8/9/2024 | |
8.1.1 | 38,627 | 6/30/2024 | |
8.1.0 | 8,834 | 6/21/2024 | |
8.0.3 | 1,176 | 6/16/2024 | |
8.0.2 | 1,609 | 6/15/2024 | |
8.0.1 | 14,039 | 6/10/2024 | |
8.0.0 | 194 | 6/10/2024 | |
7.7.8 | 46,985 | 5/4/2024 | |
7.7.7 | 16,206 | 4/21/2024 | |
7.7.6 | 15,394 | 3/19/2024 | |
7.7.5 | 14,541 | 3/3/2024 | |
7.7.4 | 1,722 | 2/29/2024 | |
7.7.3 | 884 | 2/27/2024 | |
7.7.2 | 1,296 | 2/27/2024 | |
7.7.1 | 1,804 | 2/25/2024 | |
7.7.0 | 3,411 | 2/22/2024 | |
7.6.5 | 8,685 | 2/6/2024 | |
7.6.4 | 2,291 | 1/29/2024 | |
7.6.3 | 603 | 1/26/2024 | |
7.6.2 | 9,941 | 1/14/2024 | |
7.6.1 | 3,272 | 1/6/2024 | |
7.6.0 | 7,459 | 1/2/2024 | |
7.5.0 | 3,695 | 12/22/2023 | |
7.4.4 | 15,125 | 12/10/2023 | |
7.4.3 | 1,131 | 12/7/2023 | |
7.4.2 | 1,259 | 12/7/2023 | |
7.4.1 | 4,713 | 12/3/2023 | |
7.4.0 | 2,180 | 11/30/2023 | |
7.3.8 | 630 | 11/29/2023 | |
7.3.7 | 856 | 11/28/2023 | |
7.3.6 | 456 | 11/28/2023 | |
7.3.5 | 728 | 11/27/2023 | |
7.3.4 | 4,810 | 11/24/2023 | |
7.3.3 | 1,164 | 11/23/2023 | |
7.3.2 | 875 | 11/22/2023 | |
7.3.1 | 5,200 | 11/21/2023 | |
7.3.0 | 466 | 11/21/2023 | |
7.2.3 | 5,108 | 11/12/2023 | |
7.2.2 | 2,955 | 11/10/2023 | |
7.2.1 | 506 | 11/9/2023 | |
7.2.0 | 3,688 | 11/9/2023 | |
7.1.0 | 832 | 11/7/2023 | |
7.0.10 | 7,444 | 10/7/2023 | |
7.0.9 | 13,378 | 8/27/2023 | |
7.0.8 | 2,403 | 8/25/2023 | |
7.0.5 | 5,741 | 8/10/2023 | |
7.0.4 | 5,732 | 7/27/2023 | |
7.0.3 | 11,136 | 6/21/2023 | |
7.0.2 | 936 | 6/19/2023 | |
7.0.1 | 1,952 | 6/17/2023 | |
7.0.0 | 901 | 6/17/2023 | |
6.8.7 | 9,535 | 5/21/2023 | |
6.8.6 | 676 | 5/19/2023 | |
6.8.5 | 661 | 5/19/2023 | |
6.8.3 | 1,460 | 5/16/2023 | |
6.8.2 | 735 | 5/15/2023 | |
6.8.1 | 2,719 | 5/7/2023 | |
6.8.0 | 4,479 | 4/30/2023 | |
6.7.4 | 1,101 | 4/27/2023 | |
6.7.3 | 1,200 | 4/26/2023 | |
6.7.2 | 1,356 | 4/23/2023 | |
6.7.1 | 8,599 | 4/13/2023 | |
6.7.0 | 1,965 | 4/10/2023 | |
6.6.0 | 63,833 | 4/4/2023 | |
6.5.3 | 2,685 | 3/29/2023 | |
6.5.2 | 801 | 3/29/2023 | |
6.5.1 | 1,338 | 3/27/2023 | |
6.5.0 | 2,562 | 3/26/2023 | |
6.4.3 | 714 | 3/26/2023 | |
6.4.2 | 1,202 | 3/26/2023 | |
6.4.1 | 2,833 | 3/24/2023 | |
6.4.0 | 878 | 3/23/2023 | |
6.3.2 | 2,872 | 3/22/2023 | |
6.3.1 | 5,786 | 3/17/2023 | |
6.3.0 | 1,672 | 3/17/2023 | |
6.2.0 | 774 | 3/16/2023 | |
6.1.0 | 3,325 | 3/14/2023 | |
6.0.1 | 1,800 | 3/12/2023 | |
6.0.0 | 1,105 | 3/11/2023 | |
5.1.2 | 779 | 3/10/2023 | |
5.1.1 | 800 | 3/9/2023 | |
5.1.0 | 1,132 | 3/8/2023 | |
5.0.2 | 1,618 | 3/6/2023 | |
5.0.1 | 1,344 | 3/2/2023 | |
5.0.0 | 969 | 3/2/2023 | |
4.4.4 | 2,415 | 2/18/2023 | |
4.4.3 | 1,844 | 2/10/2023 | |
4.4.2 | 1,302 | 2/7/2023 | |
4.4.1 | 972 | 2/4/2023 | |
4.4.0 | 749 | 2/4/2023 | |
4.3.0 | 987 | 1/31/2023 | |
4.2.0 | 945 | 1/28/2023 | |
4.1.0 | 850 | 1/27/2023 | |
4.0.2 | 1,198 | 1/20/2023 | |
4.0.1 | 1,042 | 1/17/2023 | |
4.0.0 | 2,341 | 1/9/2023 | |
3.0.1 | 8,296 | 4/14/2022 | |
3.0.0 | 2,399 | 6/20/2021 | |
2.0.1 | 954 | 5/29/2021 | |
2.0.0 | 1,025 | 5/29/2021 | |
1.0.1 | 968 | 5/2/2021 | |
1.0.0 | 1,148 | 5/1/2021 |
Version 7.6.1
- Include Output in Threads.FunctionCall
Version 7.6.0
- Changed License to MIT
- Added OpenAI.Chat logprob parameters
- Added SourceLink references for debugging
- Added Docfx build workflow
Version 7.5.0
- Changed OpenAIClient to implement IDisposable.
- Disposing OpenAICLient is now required if you're not passing a custom HttpClient.
- If passing an custom HttpClient, it will need to be expressly disposed after use.
- Updated Chat.Message.CopyFrom Content check from string.IsNullOrEmpty to null check.
Version 7.4.4
- Updated Docs
Version 7.4.3
- Updated FileResponse.Size int -> int?
Version 7.4.2
- Fixed missing Threads.Message.Content.ImageFile property.
- Marked OpenAI.Completions Obsolete
Version 7.4.1
- Fixed AssistantExtension.UploadFileAsync spelling error with file purpose.
Version 7.4.0
- Refactored OpenAI.Threads.LastRunError -> OpenAI.Error for more generic use in future.
- Fixed OpenAI.Threads.Annotations namespace
- Fixed OpenAI.Threads.ContextText namespace
Version 7.3.8
- Added Chat.Content.ctr overloads and implicit casting for easier usage
- Internal refactoring of FilesEndpoint.DeleteFileAsync (better status checking)
- Internal refactoring of FineTuningEndpoint to ensure we're properly setting response data
- Updated unit tests
- Updated docs
Version 7.3.7
- Fixes streaming with tools not being property copied over
Version 7.3.6
- Fixed ArgumentOutOfRangeException when streaming chat completion response contains more than one tool
Version 7.3.5
- Added GetModerationChunkedAsync method in ModerationsEndpoint
- Fixed streaming function tool serialization
Version 7.3.4
- Fixed AudioTranslationRequest.Temperature type. int? -> float?
Version 7.3.3
- Fixed Threads.FileCitation json property name
Version 7.3.2
- Added detail parameter to ImageURL
Version 7.3.1
- Fixed json serialization settings when EnableDebug is disabled
Version 7.3.0
- Added AgentsEndpoint
- Added ThreadsEndpoint
- Updated ImagesEndpoint return types to ImageResult list
- Updated FilesEndpoint.ListFilesAsync with optional purpose filter query parameter.
- Refactored list responses with a more generic ListQuery and ListResponse<TObject> pattern
- EventList -> ListResponse<EventResponse>
- FineTuneJobList -> ListResponse<FineTuneJobResponse>
- Standardized names for timestamps to have suffix: UnixTimeSeconds
- Standardized response class names (existing classes depreciated)
- FileData -> FileResponse
- CompletionResult -> CompletonResponse
- Event -> EventResponse
- FineTuneJob -> FineTuneJobResponse
Version 7.2.3
- Added support for reading RateLimit information from the Headers
Version 7.2.2
- Fixed Image Generation for Azure
Version 7.2.1
- Fixed a NRE with chat Message.ToString call when dynamic content is json element
- Removed improper set accessors for Function.Arguments and Function.Parameters properties
- Removed improper ChatResponse constructor
- Fixed unit test names
- Updated docs
Version 7.2.0
- Updated chat endpoint features
- json mode
- gpt-vision
- reproducible outputs
- tool functions
Version 7.1.0
- Convert Fine Tuning endpoint to latest (Breaking Change!)
- Added Text to Speech endpoint
- Updated Image endpoints with model parameters and support for Dall E 3
- Removed Model type checks, and now lets api handle errors
Version 7.0.10
- Fixed processing time string culture conversion when parsing double
Version 7.0.9
- Fixed Model delete permission Unauthorized Access check
Version 7.0.8
- Fixed AudioTranscriptionRequest.Temperature type. int? -> float?
- Updated Moderations Categories and Scores with new metrics
Version 7.0.5
- Fixed Message.Content serialization in Role.Function message history
Version 7.0.4
- Fixed ChatRequest forced function calls
Version 7.0.3
- Fixed chat streaming message copy from delta
Version 7.0.2
- Only set response header properties if they exist
- Remove OpenAIClient.ctr overload
Version 7.0.1
- Fixed streaming chat functions
Version 7.0.0
- Added function calling to chat models
Version 6.8.7
- Added ToString and string operator to Moderation Scores
Version 6.8.6
- Populated finish reason in streaming chat final message content
Version 6.8.5
- Updated all method calls to take a Model as string
Version 6.8.3
- Revert BaseEndpoint.GetUrl changes
Version 6.8.2
- Misc internal fixes, formatting, and docs
Version 6.8.1
- Updated basic and chat completions choices to default to empty string.
- Fixed Completions.CompletionResult.ToString first completion index lookup
- Update the HttpClient creation to set the PooledConnectionLifetime property per Microsofts recommendation.
Version 6.8.0
- Removed Obsolete ChatPrompt
- ChatEndpoint.StreamCompletionAsync will now also raise additional ChatResponse with completed Message
- ChatEndpoint.StreamCompletionEnumerableAsync will now also raise additional ChatResponse with completed Message
- Refactored all streaming endpoints to use a new string extension for centralized parsing of event stream data
- Added optional paramter cancelJob to FineTuningEndpoint.StreamFineTuneEventsEnumerableAsync. Default is false.
- Added optional parameter cancelJob to FineTuningEndpoint.StreamFineTuneEventsAsync. Default is false.
- Added optional parameter deleteCachedFile to FileEndpoint.DownloadFileAsync. Defaults to false.
- Updated Completions.LogProbabilities.TopLogProbabilities to properly use immutable IReadOnlyList<IReadOnlyDictionary<string, double>>
Version 6.7.4
- Fixed Model.Permissions
- Added Model.CreatedAt
Version 6.7.3
- added missing IDisposable to audio requests
Version 6.7.2
- Made it easier to specify a specific configuration file path
- Added optional author name property to chat message
- Added implicit string conversions to make ChatResponses easier to work with
- Updated Docs
Version 6.7.1
- Fixed parsing old env file format
- Fixed parsing missing ORGANIZATION env variables
- Fixed checking of CancellationToken.IsCancellationRequested in streaming endpoints
- Updated Docs
Version 6.7.0
- Deprecated ChatPrompt -> Message
- Added Role enum for Chat.Messages and Chat.Delta
- Updated ChatRequest constructor to use IEnumerable<Message> messages
- Updated ChatRequest.Messages to IReadonlyList<Message>
- Updated unit tests
Version 6.6.0
- Added ResponseFormat to ImageGenerationRequests
- Refactored Image Requests with AbstractBaseImageRequest
Version 6.5.3
- Added missing ConfigureAwait to await calls
Version 6.5.2
- Updated SetResponseData to better reflect the difference between OpenAI and Azure responses.
- Updated ProcessingTime parsing from int to double
Version 6.5.1
- Removed Obsolete from EditEndpoint as it has now been fixed by OpenAI
Version 6.5.0
- Marked EditEndpoint Obsolete as codex and edit models have been removed
Version 6.4.3
- Fixed support for Azure Active Directory authentication for Azure OpenAI
Version 6.4.2
- Misc fixes and added validation for OpenAICLientSettings
- Updated docs
- Decoupled proxy version from main package
Version 6.4.1
- Added ImageEditRequest overloads for optional mask parameter
Version 6.4.0
- Moved OpenAI-DotNet-Proxy back into its own project and package
- Make a few classes sealed that are not meant to be extended
Version 6.3.2
- Attempt to fix dependency requirement for dotnet/runtime docker base images
- Made internal OpenAIClient constructor with HttpClient public
- Make sure we only copy the appropriate headers in the proxy
Version 6.3.1
- Fixed apikey requiring sk- prefix with Azure OpenAI
Version 6.3.0
- Removed OpenAI-DotNet-Proxy and put it directly in package on its own
Version 6.2.0
- Added OpenAI-DotNet-Proxy project and package.
- Added support for custom domains
- Updated unit tests
- Updated docs
Version 6.1.0
- Added support for gpt-4 models
Version 6.0.1
- Updated package info
- Updated docs
Version 6.0.0
- Added support for Azure OpenAI
Version 5.1.2
- Fixed an issue when deleting personal account fine tuned models
Version 5.1.1
- Refactored Model validation
- Added additional default models
- Deprecate OpenAIClient.DefaultModel
- Implemented chat completion streaming
- Refactored immutable types
Version 5.1.0
- Add support for Audio endpoint and Whisper models
- Audio Speech to text
- Audio translation
Version 5.0.2
- Support multiple inputs in embedding
- Added better model validation in all endpoints
Version 5.0.1
- Fixed chat parameters
Version 5.0.0
- Added Chat endpoint
Version 4.4.4
- ImageEditRequest mask is now optional so long as texture has alpha transparency
- ImageVariationRequest added constructor overload for memory stream image
- Updated AuthInfo parameter validation
- Renamed OPEN_AI_ORGANIZATION_ID -> OPENAI_ORGANIZATION_ID
Version 4.4.3
- added OPEN_AI_ORGANIZATION_ID environment variable
- deprecated Organization use OrganizationId instead
Version 4.4.2
- Removed a useless assert
- Updated docs
Version 4.4.1
- hotfix to CompletionsEndpoint to use IEnumerable<string>
- hotfix to cleanup Images endpoints
Version 4.4.0
- Renamed Choice.Logprobs -> Choice.LogProbabilities
- Renamed OpenAI.Completions.Logprobs -> OpenAI.Completions.OpenAI.Completions
- Renamed CompletionRequest parameter names:
- max_tokens -> maxTokens
- top_p -> topP
- Updated CompletionRequest to accept IEnumerable<string> values for prompts and stopSequences
- Refactored all endpoints to use new response validation extension
- Added CancellationToken to most endpoints that had long running operations