Skip to content

Latest commit

 

History

History
134 lines (103 loc) · 5.98 KB

Readme.md

File metadata and controls

134 lines (103 loc) · 5.98 KB

.NET SDK for OpenAI

Overview

A .NET SDK for accessing OpenAI's API, provided as a community library. This SDK allows you to integrate OpenAI's capabilities into your .NET applications with ease.

Install Packages

Core Library

Betalgo.OpenAI

Install-Package Betalgo.OpenAI

Experimental Utilities Library

Betalgo.OpenAI.Utilities

Install-Package Betalgo.OpenAI.Utilities

Documentation and Links

API Documentation

  • Betalgo.OpenAI: Static Badge
  • Betalgo.OpenAI.Utilities: Static Badge

Acknowledgements

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


Sample Usage

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

Without Using Dependency Injection

var openAiService = new OpenAIService(new OpenAiOptions()
{
    ApiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY")
});

Using Dependency Injection

secrets.json

"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.)

Program.cs

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);

Chat GPT Sample

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);
}

Notes

Library Renaming

This library was previously known as Betalgo.OpenAI.GPT3, and now it has a new package ID: Betalgo.OpenAI.

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.

I initially developed this SDK for my personal use and decided to share it with the community. As I have not maintained open-source projects before, any assistance or feedback would be greatly appreciated. Feel free to reach out with your suggestions.

Please be aware that future releases may frequently include breaking changes. Consider this before deciding to use the library. I cannot accept responsibility for any damage caused by using the library. You are free to explore alternative libraries or the OpenAI Web-API if you feel this is not suitable for your purposes.

Changelog

8.3.0

  • Updated Assistant tests, added sample for CreateMessageWithImage
  • Azure Assistant endpoints are updated since documentation reference still earlier version (Assistant v1). I am not sure if Azure supports all Assistant v2 features. So, feedback is much appreciated.
  • Fixed error handling and response parsing for audio transcription result in text mode.
  • Fixed Culture issue for number conversions (Audio Temperature and Image N)
  • Removed file_ids from Create Assistant
  • Added Support for Chat LogProbs
  • Fixed File_Id Typo in file VisionImageUrl
  • Updated File purpose enum list

For any issues, contributions, or feedback, feel free to reach out or submit a pull request.