.NET library for the OpenAI service API by Betalgo Ranul
See the codeA .NET Library for accessing OpenAI's API, provided as a community library. This Library allows you to integrate OpenAI's capabilities into your .NET applications with ease.
⭐ We appreciate your star, it helps!
⚠️ We now have new PackageId and new Namespace. ⚠️
⚠️ Betalgo.OpenAI is now Betalgo.Ranul.OpenAI ⚠️
Install-Package Betalgo.Ranul.OpenAI
Install-Package Betalgo.OpenAI.Utilities
Maintenance of this project is made possible by all the bug reporters, contributors, and sponsors.
💖 Sponsors:
@betalgo, Laser Cat Eyes
@tylerje, @oferavnery, @MayDay-wpf, @AnukarOP, @Removable, @Scar11
The repository contains a sample project named OpenAI.Playground to help you understand how the library works. However, please exercise caution while experimenting, as some test methods may result in unintended consequences such as file deletion or fine-tuning.
!! It is highly recommended that you use a separate account instead of your primary account while using the playground. Some test methods may add or delete your files and models, potentially causing unwanted issues. !!
Your API Key can be obtained from here: https://platform.openai.com/account/api-keys
Your Organization ID can be found here: https://platform.openai.com/account/org-settings
var openAIService = new OpenAIService(new OpenAIOptions()
{
ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY")
});
"OpenAIServiceOptions": {
"ApiKey": "Your api key goes here",
"Organization": "Your Organization Id goes here (optional)",
"UseBeta": "true/false (optional)"
}
(To use user secrets: Right-click your project name in "Solution Explorer", then click "Manage User Secrets". This is a good way to keep your API keys secure.)
serviceCollection.AddOpenAIService();
OR
serviceCollection.AddOpenAIService(settings => { settings.ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"); });
After injecting your service, you can retrieve it from the service provider:
var openAiService = serviceProvider.GetRequiredService<IOpenAIService>();
You can set a default model (optional):
openAiService.SetDefaultModelId(Models.Gpt_4o);
var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful assistant."),
ChatMessage.FromUser("Who won the world series in 2020?"),
ChatMessage.FromAssistant("The Los Angeles Dodgers won the World Series in 2020."),
ChatMessage.FromUser("Where was it played?")
},
Model = Models.Gpt_4o,
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
}
Due to time constraints, not all methods have been thoroughly tested or fully documented. If you encounter any issues, please report them or submit a pull request. Your contributions are always appreciated.
Needless to say, I cannot accept responsibility for any damage caused by using the library.
type: "object" and avoid invalid_function_parameters errors like schema must be a JSON Schema of 'type: "object"', got 'type: "None"'.Betalgo.Ranul.OpenAI.Contracts project for centralized request/response models, enums/value types, and capability interfaces.CreateImageRequest, CreateImageEditRequest, CreateImageVariationRequestBetalgo.Ranul.OpenAI.Contracts.Enums.Image.* and added value types (ImageOutputFormat, ImageResponseFormat, ImageSize, ImageModeration).VoiceEnum with Voice (value type). Replaced MessageRole with ChatCompletionRole and/or AssistantMessageRole.ResponseBase, ResponseBaseHeaderValues, improved header parsing and usage exposure.IImageService to consume Contracts request models and return ImageResponse for image create. Edit/variation remain legacy responses for now.UsageModel to include detailed token usage tracking for image generation responses.FunctionParameters with MultipleOf, Minimum, Maximum, and Pattern support.SingleOrArrayToListConverter to handle single values or arrays.MessageContent with expression-bodied helpers and added binary file support.ToolDefinition from static string to const string for clarity and performance.ToolDefinition.FunctionParameters.Microsoft.Extensions.AI to version 9.5.0Microsoft.Extensions.AI to version 9.4.0-preview.1.25207.5Microsoft.Extensions.AI to version 9.3.0-preview.1.25114.11o1 and o3-mini models to the model listFor any issues, contributions, or feedback, feel free to reach out or submit a pull request.
Betalgo X: Betalgo (@Betalgo) / X (twitter.com)
Betalgo Linkedin: Betalgo | LinkedIn
Tolga X: Tolga Kayhan (@kayhantolga) / X (twitter.com)
Tolga Linkedin: Tolga Kayhan | LinkedIn
4,143 followers · starred Mar 2023
165 followers · starred Apr 2024
708 followers · starred Mar 2023
519 followers · starred Sep 2024
.NET library for the OpenAI service API by Betalgo Ranul
See the codeA .NET Library for accessing OpenAI's API, provided as a community library. This Library allows you to integrate OpenAI's capabilities into your .NET applications with ease.
⭐ We appreciate your star, it helps!
⚠️ We now have new PackageId and new Namespace. ⚠️
⚠️ Betalgo.OpenAI is now Betalgo.Ranul.OpenAI ⚠️
Install-Package Betalgo.Ranul.OpenAI
Install-Package Betalgo.OpenAI.Utilities
Maintenance of this project is made possible by all the bug reporters, contributors, and sponsors.
💖 Sponsors:
@betalgo, Laser Cat Eyes
@tylerje, @oferavnery, @MayDay-wpf, @AnukarOP, @Removable, @Scar11
The repository contains a sample project named OpenAI.Playground to help you understand how the library works. However, please exercise caution while experimenting, as some test methods may result in unintended consequences such as file deletion or fine-tuning.
!! It is highly recommended that you use a separate account instead of your primary account while using the playground. Some test methods may add or delete your files and models, potentially causing unwanted issues. !!
Your API Key can be obtained from here: https://platform.openai.com/account/api-keys
Your Organization ID can be found here: https://platform.openai.com/account/org-settings
var openAIService = new OpenAIService(new OpenAIOptions()
{
ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY")
});
"OpenAIServiceOptions": {
"ApiKey": "Your api key goes here",
"Organization": "Your Organization Id goes here (optional)",
"UseBeta": "true/false (optional)"
}
(To use user secrets: Right-click your project name in "Solution Explorer", then click "Manage User Secrets". This is a good way to keep your API keys secure.)
serviceCollection.AddOpenAIService();
OR
serviceCollection.AddOpenAIService(settings => { settings.ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY"); });
After injecting your service, you can retrieve it from the service provider:
var openAiService = serviceProvider.GetRequiredService<IOpenAIService>();
You can set a default model (optional):
openAiService.SetDefaultModelId(Models.Gpt_4o);
var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful assistant."),
ChatMessage.FromUser("Who won the world series in 2020?"),
ChatMessage.FromAssistant("The Los Angeles Dodgers won the World Series in 2020."),
ChatMessage.FromUser("Where was it played?")
},
Model = Models.Gpt_4o,
});
if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
}
Due to time constraints, not all methods have been thoroughly tested or fully documented. If you encounter any issues, please report them or submit a pull request. Your contributions are always appreciated.
Needless to say, I cannot accept responsibility for any damage caused by using the library.
type: "object" and avoid invalid_function_parameters errors like schema must be a JSON Schema of 'type: "object"', got 'type: "None"'.Betalgo.Ranul.OpenAI.Contracts project for centralized request/response models, enums/value types, and capability interfaces.CreateImageRequest, CreateImageEditRequest, CreateImageVariationRequestBetalgo.Ranul.OpenAI.Contracts.Enums.Image.* and added value types (ImageOutputFormat, ImageResponseFormat, ImageSize, ImageModeration).VoiceEnum with Voice (value type). Replaced MessageRole with ChatCompletionRole and/or AssistantMessageRole.ResponseBase, ResponseBaseHeaderValues, improved header parsing and usage exposure.IImageService to consume Contracts request models and return ImageResponse for image create. Edit/variation remain legacy responses for now.UsageModel to include detailed token usage tracking for image generation responses.FunctionParameters with MultipleOf, Minimum, Maximum, and Pattern support.SingleOrArrayToListConverter to handle single values or arrays.MessageContent with expression-bodied helpers and added binary file support.ToolDefinition from static string to const string for clarity and performance.ToolDefinition.FunctionParameters.Microsoft.Extensions.AI to version 9.5.0Microsoft.Extensions.AI to version 9.4.0-preview.1.25207.5Microsoft.Extensions.AI to version 9.3.0-preview.1.25114.11o1 and o3-mini models to the model listFor any issues, contributions, or feedback, feel free to reach out or submit a pull request.
Betalgo X: Betalgo (@Betalgo) / X (twitter.com)
Betalgo Linkedin: Betalgo | LinkedIn
Tolga X: Tolga Kayhan (@kayhantolga) / X (twitter.com)
Tolga Linkedin: Tolga Kayhan | LinkedIn
4,143 followers · starred Mar 2023
165 followers · starred Apr 2024
708 followers · starred Mar 2023
519 followers · starred Sep 2024