tryAGI.OpenAI
3.2.1
Prefix Reserved
See the version list below for details.
dotnet add package tryAGI.OpenAI --version 3.2.1
NuGet\Install-Package tryAGI.OpenAI -Version 3.2.1
<PackageReference Include="tryAGI.OpenAI" Version="3.2.1" />
<PackageVersion Include="tryAGI.OpenAI" Version="3.2.1" />
<PackageReference Include="tryAGI.OpenAI" />
paket add tryAGI.OpenAI --version 3.2.1
#r "nuget: tryAGI.OpenAI, 3.2.1"
#:package tryAGI.OpenAI@3.2.1
#addin nuget:?package=tryAGI.OpenAI&version=3.2.1
#tool nuget:?package=tryAGI.OpenAI&version=3.2.1
OpenAI
Features 🔥
- Fully generated C# SDK based on official OpenAI OpenAPI specification using OpenApiGenerator
- 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.
Usage
using OpenAI;
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;
}
[OpenAiFunctions]
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 = Unit.Celsius,
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.AuthorizeUsingBearer(apiKey);
var service = new WeatherService();
var tools = service.AsTools();
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.Gpt35Turbo;
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.
Custom providers
using OpenAI;
using var api = Providers.GitHubModels("GITHUB_TOKEN");
using var api = Providers.Azure("API_KEY", "ENDPOINT");
using var api = Providers.DeepInfra("API_KEY");
using var api = Providers.DeepSeek("API_KEY");
using var api = Providers.Fireworks("API_KEY");
using var api = Providers.OpenRouter("API_KEY");
using var api = Providers.Together("API_KEY");
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;
// 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
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. 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 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 was computed. 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
- System.Text.Json (>= 8.0.4)
- Tiktoken (>= 2.0.3)
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.4)
- Tiktoken (>= 2.0.3)
-
net6.0
- System.Text.Json (>= 8.0.4)
- Tiktoken (>= 2.0.3)
-
net8.0
- Tiktoken (>= 2.0.3)
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 | 5,996 | 3/9/2025 |
| 4.2.1-dev.3 | 157 | 2/27/2025 |
| 4.2.0 | 39,879 | 2/11/2025 |
| 4.1.0 | 10,976 | 2/3/2025 |
| 4.0.0 | 70,542 | 1/27/2025 |
| 3.9.3-dev.40 | 230 | 1/7/2025 |
| 3.9.3-dev.38 | 123 | 1/7/2025 |
| 3.9.3-dev.37 | 152 | 1/7/2025 |
| 3.9.3-dev.36 | 318 | 1/7/2025 |
| 3.9.3-dev.35 | 150 | 1/7/2025 |
| 3.9.3-dev.31 | 181 | 12/19/2024 |
| 3.9.3-dev.14 | 147 | 11/15/2024 |
| 3.9.3-dev.13 | 159 | 11/15/2024 |
| 3.9.3-dev.1 | 145 | 10/26/2024 |
| 3.9.2 | 3,573 | 10/26/2024 |
| 3.9.2-dev.7 | 3,938 | 10/25/2024 |
| 3.9.2-dev.6 | 147 | 10/25/2024 |
| 3.9.2-dev.4 | 133 | 10/25/2024 |
| 3.9.2-dev.1 | 252 | 10/24/2024 |
| 3.2.1 | 54,961 | 8/6/2024 |