tryAGI.OpenAI
3.9.3-dev.14
Prefix Reserved
See the version list below for details.
dotnet add package tryAGI.OpenAI --version 3.9.3-dev.14
NuGet\Install-Package tryAGI.OpenAI -Version 3.9.3-dev.14
<PackageReference Include="tryAGI.OpenAI" Version="3.9.3-dev.14" />
<PackageVersion Include="tryAGI.OpenAI" Version="3.9.3-dev.14" />
<PackageReference Include="tryAGI.OpenAI" />
paket add tryAGI.OpenAI --version 3.9.3-dev.14
#r "nuget: tryAGI.OpenAI, 3.9.3-dev.14"
#:package tryAGI.OpenAI@3.9.3-dev.14
#addin nuget:?package=tryAGI.OpenAI&version=3.9.3-dev.14&prerelease
#tool nuget:?package=tryAGI.OpenAI&version=3.9.3-dev.14&prerelease
OpenAI
Features 🔥
- Fully generated C# SDK based on official OpenAI OpenAPI specification using AutoSDK
- Same day update to support new features
- Updated and supported automatically if there are no breaking changes
- Contains a supported list of constants such as current prices, models, and other
- Source generator to define functions natively through C# interfaces
- All modern .NET features - nullability, trimming, NativeAOT, etc.
- Support .Net Framework/.Net Standard 2.0
- Support all OpenAI API endpoints including completions, chat, embeddings, images, assistants and more.
- Regularly tested for compatibility with popular custom providers like OpenRouter/DeepSeek/Ollama/LM Studio and many others
Documentation
Examples and documentation can be found here: https://tryagi.github.io/OpenAI/
Usage
using var api = new OpenAiApi("API_KEY");
string response = await api.Chat.CreateChatCompletionAsync(
messages: ["Generate five random words."],
model: CreateChatCompletionRequestModel.Gpt4oMini);
Console.WriteLine(response); // "apple, banana, cherry, date, elderberry"
var enumerable = api.Chat.CreateChatCompletionAsStreamAsync(
messages: ["Generate five random words."],
model: CreateChatCompletionRequestModel.Gpt4oMini);
await foreach (string response in enumerable)
{
Console.WriteLine(response);
}
It uses three implicit conversions:
- from
stringtoChatCompletionRequestUserMessage. It will always be converted to the user message. - from
ChatCompletionResponseMessagetostring. It will always contain the first choice message content. - from
CreateChatCompletionStreamResponsetostring. It will always contain the first delta content.
You still can use the full response objects if you need more information, just replace string response to var response.
Tools
using OpenAI;
using CSharpToJsonSchema;
public enum Unit
{
Celsius,
Fahrenheit,
}
public class Weather
{
public string Location { get; set; } = string.Empty;
public double Temperature { get; set; }
public Unit Unit { get; set; }
public string Description { get; set; } = string.Empty;
}
[GenerateJsonSchema(Strict = true)] // false by default. You can't use parameters with default values in Strict mode.
public interface IWeatherFunctions
{
[Description("Get the current weather in a given location")]
public Task<Weather> GetCurrentWeatherAsync(
[Description("The city and state, e.g. San Francisco, CA")] string location,
Unit unit,
CancellationToken cancellationToken = default);
}
public class WeatherService : IWeatherFunctions
{
public Task<Weather> GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default)
{
return Task.FromResult(new Weather
{
Location = location,
Temperature = 22.0,
Unit = unit,
Description = "Sunny",
});
}
}
using var api = new OpenAiApi("API_KEY");
var service = new WeatherService();
var tools = service.AsTools().AsOpenAiTools();
var messages = new List<ChatCompletionRequestMessage>
{
"You are a helpful weather assistant.".AsSystemMessage(),
"What is the current temperature in Dubai, UAE in Celsius?".AsUserMessage(),
};
var model = CreateChatCompletionRequestModel.Gpt4oMini;
var result = await api.Chat.CreateChatCompletionAsync(
messages,
model: model,
tools: tools);
var resultMessage = result.Choices.First().Message;
messages.Add(resultMessage.AsRequestMessage());
foreach (var call in resultMessage.ToolCalls)
{
var json = await service.CallAsync(
functionName: call.Function.Name,
argumentsAsJson: call.Function.Arguments);
messages.Add(json.AsToolMessage(call.Id));
}
var result = await api.Chat.CreateChatCompletionAsync(
messages,
model: model,
tools: tools);
var resultMessage = result.Choices.First().Message;
messages.Add(resultMessage.AsRequestMessage());
> System:
You are a helpful weather assistant.
> User:
What is the current temperature in Dubai, UAE in Celsius?
> Assistant:
call_3sptsiHzKnaxF8bs8BWxPo0B:
GetCurrentWeather({"location":"Dubai, UAE","unit":"celsius"})
> Tool(call_3sptsiHzKnaxF8bs8BWxPo0B):
{"location":"Dubai, UAE","temperature":22,"unit":"celsius","description":"Sunny"}
> Assistant:
The current temperature in Dubai, UAE is 22°C with sunny weather.
Structured Outputs
using OpenAI;
using var api = new OpenAiApi("API_KEY");
var response = await api.Chat.CreateChatCompletionAsAsync<Weather>(
messages: ["Generate random weather."],
model: CreateChatCompletionRequestModel.Gpt4oMini,
jsonSerializerOptions: new JsonSerializerOptions
{
Converters = {new JsonStringEnumConverter()},
});
// or (if you need trimmable/NativeAOT version)
var response = await api.Chat.CreateChatCompletionAsAsync(
jsonTypeInfo: SourceGeneratedContext.Default.Weather,
messages: ["Generate random weather."],
model: CreateChatCompletionRequestModel.Gpt4oMini);
// response.Value1 contains the structured output
// response.Value2 contains the CreateChatCompletionResponse object
Weather:
Location: San Francisco, CA
Temperature: 65
Unit: Fahrenheit
Description: Partly cloudy with a light breeze and occasional sunshine.
Raw Response:
{"Location":"San Francisco, CA","Temperature":65,"Unit":"Fahrenheit","Description":"Partly cloudy with a light breeze and occasional sunshine."}
Additional code for trimmable/NativeAOT version:
[JsonSourceGenerationOptions(Converters = [typeof(JsonStringEnumConverter<Unit>)])]
[JsonSerializable(typeof(Weather))]
public partial class SourceGeneratedContext : JsonSerializerContext;
Custom providers
using OpenAI;
using var api = CustomProviders.GitHubModels("GITHUB_TOKEN");
using var api = CustomProviders.Azure("API_KEY", "ENDPOINT");
using var api = CustomProviders.DeepInfra("API_KEY");
using var api = CustomProviders.Groq("API_KEY");
using var api = CustomProviders.XAi("API_KEY");
using var api = CustomProviders.DeepSeek("API_KEY");
using var api = CustomProviders.Fireworks("API_KEY");
using var api = CustomProviders.OpenRouter("API_KEY");
using var api = CustomProviders.Together("API_KEY");
using var api = CustomProviders.Perplexity("API_KEY");
using var api = CustomProviders.SambaNova("API_KEY");
using var api = CustomProviders.Mistral("API_KEY");
using var api = CustomProviders.Codestral("API_KEY");
using var api = CustomProviders.Cerebras("API_KEY");
using var api = CustomProviders.Ollama();
using var api = CustomProviders.LmStudio();
Constants
All tryGetXXX methods return null if the value is not found.
There also non-try methods that throw an exception if the value is not found.
using OpenAI;
// You can try to get the enum from string using:
var model = CreateChatCompletionRequestModelExtensions.ToEnum("gpt-4o") ?? throw new Exception("Invalid model");
// Chat
var model = CreateChatCompletionRequestModel.Gpt4oMini;
double? priceInUsd = model.TryGetPriceInUsd(
inputTokens: 500,
outputTokens: 500)
double? priceInUsd = model.TryGetFineTunePriceInUsd(
trainingTokens: 500,
inputTokens: 500,
outputTokens: 500)
int contextLength = model.TryGetContextLength() // 128_000
int outputLength = model.TryGetOutputLength() // 16_000
// Embeddings
var model = CreateEmbeddingRequestModel.TextEmbedding3Small;
int? maxInputTokens = model.TryGetMaxInputTokens() // 8191
double? priceInUsd = model.TryGetPriceInUsd(tokens: 500)
// Images
double? priceInUsd = CreateImageRequestModel.DallE3.TryGetPriceInUsd(
size: CreateImageRequestSize.x1024x1024,
quality: CreateImageRequestQuality.Hd)
// Speech to Text
double? priceInUsd = CreateTranscriptionRequestModel.Whisper1.TryGetPriceInUsd(
seconds: 60)
// Text to Speech
double? priceInUsd = CreateSpeechRequestModel.Tts1Hd.TryGetPriceInUsd(
characters: 1000)
Support
Priority place for bugs: https://github.com/tryAGI/OpenAI/issues
Priority place for ideas and general questions: https://github.com/tryAGI/OpenAI/discussions
Discord: https://discord.gg/Ca2xhfBf3v
Acknowledgments
This project is supported by JetBrains through the Open Source Support Program.
This project is supported by CodeRabbit through the Open Source Support Program.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- CSharpToJsonSchema (>= 3.10.1)
- System.Text.Json (>= 9.0.0)
- Tiktoken (>= 2.1.1)
-
.NETStandard 2.0
- CSharpToJsonSchema (>= 3.10.1)
- System.Text.Json (>= 9.0.0)
- Tiktoken (>= 2.1.1)
-
net8.0
- CSharpToJsonSchema (>= 3.10.1)
- System.Text.Json (>= 9.0.0)
- Tiktoken (>= 2.1.1)
-
net9.0
- CSharpToJsonSchema (>= 3.10.1)
- System.Text.Json (>= 9.0.0)
- Tiktoken (>= 2.1.1)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on tryAGI.OpenAI:
| Package | Downloads |
|---|---|
|
LangChain.Providers.OpenAI
OpenAI API LLM and Chat model provider. |
|
|
Anyscale
SDK for Anyscale Endpoint that makes it easy and cheap to use LLama 2 |
|
|
LangChain.Serve.OpenAI
LangChain Serve as OpenAI sdk compatible API |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on tryAGI.OpenAI:
| Repository | Stars |
|---|---|
|
tryAGI/LangChain
C# implementation of LangChain. We try to be as close to the original as possible in terms of abstractions, but are open to new entities.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 4.2.1-dev.6 | 2,656 | 3/9/2025 |
| 4.2.1-dev.3 | 136 | 2/27/2025 |
| 4.2.0 | 18,792 | 2/11/2025 |
| 4.1.0 | 628 | 2/3/2025 |
| 4.0.0 | 35,598 | 1/27/2025 |
| 3.9.3-dev.40 | 196 | 1/7/2025 |
| 3.9.3-dev.38 | 107 | 1/7/2025 |
| 3.9.3-dev.37 | 125 | 1/7/2025 |
| 3.9.3-dev.36 | 294 | 1/7/2025 |
| 3.9.3-dev.35 | 128 | 1/7/2025 |
| 3.9.3-dev.31 | 154 | 12/19/2024 |
| 3.9.3-dev.14 | 133 | 11/15/2024 |
| 3.9.3-dev.13 | 133 | 11/15/2024 |
| 3.9.3-dev.1 | 125 | 10/26/2024 |
| 3.9.2 | 3,516 | 10/26/2024 |
| 3.9.2-dev.7 | 3,678 | 10/25/2024 |
| 3.9.2-dev.6 | 119 | 10/25/2024 |
| 3.9.2-dev.4 | 110 | 10/25/2024 |
| 3.9.2-dev.1 | 222 | 10/24/2024 |
| 3.9.1 | 186 | 10/24/2024 |
| 3.8.2-dev.13 | 141 | 10/14/2024 |
| 3.8.2-dev.10 | 122 | 10/13/2024 |
| 3.8.2-dev.9 | 123 | 10/13/2024 |
| 3.8.1 | 1,063 | 10/9/2024 |
| 3.8.1-dev.14 | 130 | 10/2/2024 |
| 3.8.1-dev.13 | 117 | 10/2/2024 |
| 3.8.1-dev.6 | 103 | 9/28/2024 |
| 3.8.1-dev.3 | 105 | 9/28/2024 |
| 3.8.1-dev.2 | 109 | 9/26/2024 |
| 3.8.0 | 1,145 | 9/26/2024 |
| 3.7.1-dev.14 | 115 | 9/24/2024 |
| 3.7.1-dev.12 | 128 | 9/23/2024 |
| 3.7.1-dev.5 | 155 | 9/16/2024 |
| 3.7.1-dev.3 | 30,368 | 9/15/2024 |
| 3.7.0 | 474 | 9/14/2024 |
| 3.6.4-dev.10 | 118 | 9/14/2024 |
| 3.6.4-dev.9 | 117 | 9/14/2024 |
| 3.6.4-dev.7 | 118 | 9/13/2024 |
| 3.6.4-dev.5 | 121 | 9/9/2024 |
| 3.6.3 | 750 | 9/2/2024 |
| 3.6.2 | 232 | 9/1/2024 |
| 3.6.1 | 251 | 9/1/2024 |
| 3.5.2-dev.24 | 140 | 8/31/2024 |
| 3.5.2-dev.22 | 114 | 8/31/2024 |
| 3.5.2-dev.21 | 129 | 8/31/2024 |
| 3.5.2-dev.14 | 115 | 8/29/2024 |
| 3.5.2-dev.10 | 144 | 8/24/2024 |
| 3.5.2-dev.9 | 147 | 8/21/2024 |
| 3.5.2-dev.7 | 145 | 8/19/2024 |
| 3.5.2-dev.6 | 114 | 8/19/2024 |
| 3.5.2-dev.5 | 137 | 8/19/2024 |
| 3.5.2-dev.2 | 149 | 8/18/2024 |
| 3.5.1 | 958 | 8/18/2024 |
| 3.5.1-dev.4 | 138 | 8/18/2024 |
| 3.5.1-dev.3 | 144 | 8/18/2024 |
| 3.5.0 | 245 | 8/17/2024 |
| 3.4.2-dev.5 | 141 | 8/17/2024 |
| 3.4.2-dev.1 | 133 | 8/17/2024 |
| 3.4.1 | 338 | 8/16/2024 |
| 3.4.0 | 255 | 8/16/2024 |
| 3.3.1-dev.17 | 145 | 8/15/2024 |
| 3.3.1-dev.16 | 122 | 8/15/2024 |
| 3.3.1-dev.14 | 127 | 8/15/2024 |
| 3.3.1-dev.12 | 155 | 8/13/2024 |
| 3.3.1-dev.11 | 146 | 8/13/2024 |
| 3.3.1-dev.8 | 123 | 8/12/2024 |
| 3.3.1-dev.7 | 134 | 8/12/2024 |
| 3.3.1-dev.1 | 99 | 8/6/2024 |
| 3.3.0 | 432 | 8/6/2024 |
| 3.2.2-dev.3 | 93 | 8/6/2024 |
| 3.2.1 | 1,177 | 8/6/2024 |
| 3.2.1-dev.2 | 105 | 8/6/2024 |
| 3.2.1-dev.1 | 93 | 8/5/2024 |
| 3.2.0 | 207 | 8/5/2024 |
| 3.1.1-dev.1 | 105 | 8/4/2024 |
| 3.1.0 | 269 | 8/1/2024 |
| 3.0.4-dev.11 | 109 | 8/1/2024 |
| 3.0.4-dev.9 | 717 | 7/24/2024 |
| 3.0.4-dev.8 | 167 | 7/23/2024 |
| 3.0.4-dev.6 | 226 | 7/20/2024 |
| 3.0.4-dev.5 | 97 | 7/20/2024 |
| 3.0.4-dev.4 | 110 | 7/20/2024 |
| 3.0.4-dev.3 | 100 | 7/20/2024 |
| 3.0.4-dev.2 | 125 | 7/19/2024 |
| 3.0.4-dev.1 | 120 | 7/18/2024 |
| 3.0.3 | 265 | 7/18/2024 |
| 3.0.3-dev.1 | 106 | 7/18/2024 |
| 3.0.1 | 209 | 7/13/2024 |
| 3.0.0 | 226 | 7/11/2024 |
| 3.0.0-alpha.3 | 111 | 7/7/2024 |
| 3.0.0-alpha.2 | 109 | 7/6/2024 |
| 2.0.9 | 44,471 | 5/14/2024 |
| 2.0.8 | 244 | 4/29/2024 |
| 2.0.7 | 224 | 4/29/2024 |
| 2.0.6 | 10,760 | 4/22/2024 |
| 2.0.5 | 732 | 4/10/2024 |
| 2.0.4 | 605 | 4/4/2024 |
| 2.0.3 | 226 | 4/3/2024 |
| 2.0.2 | 301 | 4/3/2024 |
| 2.0.1 | 237 | 4/2/2024 |
| 2.0.0 | 402 | 3/22/2024 |
| 2.0.0-alpha.10 | 3,888 | 2/23/2024 |
| 2.0.0-alpha.9 | 44,356 | 1/27/2024 |
| 2.0.0-alpha.8 | 114 | 1/27/2024 |
| 2.0.0-alpha.7 | 149 | 1/20/2024 |
| 2.0.0-alpha.5 | 562 | 12/5/2023 |
| 2.0.0-alpha.4 | 251 | 11/16/2023 |
| 2.0.0-alpha.3 | 158 | 11/16/2023 |
| 2.0.0-alpha.2 | 121 | 11/16/2023 |
| 2.0.0-alpha.1 | 122 | 11/15/2023 |
| 2.0.0-alpha.0 | 120 | 11/15/2023 |
| 1.8.2 | 164,544 | 10/13/2023 |
| 1.8.1 | 620 | 10/11/2023 |
| 1.8.0 | 369 | 9/29/2023 |
| 1.7.2 | 1,998 | 8/15/2023 |
| 1.7.1 | 269 | 8/15/2023 |
| 1.7.0 | 431 | 8/15/2023 |
| 1.6.3 | 2,498 | 7/29/2023 |
| 1.6.1 | 259 | 7/29/2023 |
| 1.6.0 | 449 | 7/29/2023 |
| 1.5.0 | 791 | 7/13/2023 |
| 1.4.1 | 276 | 7/12/2023 |
| 1.4.0 | 282 | 7/12/2023 |
| 1.3.0 | 270 | 7/12/2023 |
| 1.2.0 | 763 | 7/5/2023 |
| 1.1.2 | 519 | 6/30/2023 |
| 1.1.1 | 250 | 6/30/2023 |
| 1.1.0 | 275 | 6/29/2023 |
| 1.0.0 | 284 | 6/28/2023 |
| 0.9.0 | 332 | 6/24/2023 |
| 0.0.0-dev.150 | 122 | 7/18/2024 |
| 0.0.0-dev | 181 | 9/14/2024 |