MIT License

项目介绍

betalgo/openai 是一个托管在 GitHub 上的开源项目,主要使用 C# 编写,采用 MIT License 许可证,目前获得 3.0k 个 Star、529 次 Fork。

仓库全名
betalgo/openai
开源许可证
MIT License
Star 数
3.0k
Fork 数
529
未关闭 Issue
13

README 摘要

Overview A .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. Community Links Install Packages Core Library ⚠️ We now have new PackageId and new Namespace. ⚠️ ⚠️ Betalgo.OpenAI is now Betalgo.Ranul.OpenAI ⚠️ Experimental Utilities Library Documentation and Links Realtime API ✨NEW Wiki Page Feature…

完整内容见 GitHub 原始 README 或本页 。

常见问题

openai 用什么语言开发?

主要使用 C# 编写,采用 MIT License 许可证,目前有 3.0k 个 Star。

openai 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 openai 的源码?

可以直接克隆仓库:git clone https://github.com/betalgo/openai.git,默认分支为 master;安装与运行步骤见仓库 README。

openai 最近还在维护吗?

仓库最近一次更新为 3月22日,当前有 13 个未关闭 Issue;Star 数为 3.0k,可作为社区活跃度的参考。

简要分析

热度分
4148
价值分
1978
活跃状态
活跃

语言占比

C#
HTML

Release

README

Betalgo Ranul OpenAI Github readme banner

Overview

A .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! GitHub Repo stars

  • Discord Please come and help us build the .NET AI community
  • Static Badge
  • Static Badge

Install Packages

Core Library

⚠️ We now have new PackageId and new Namespace. ⚠️
⚠️ Betalgo.OpenAI is now Betalgo.Ranul.OpenAI ⚠️

Betalgo.Ranul.OpenAI

Install-Package Betalgo.Ranul.OpenAI
Experimental Utilities Library

Betalgo.OpenAI.Utilities

Install-Package Betalgo.OpenAI.Utilities

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

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.

Changelog

9.2.4

  • Fixed function tool schema generation so parameters always emit a JSON Schema type: "object" and avoid invalid_function_parameters errors like schema must be a JSON Schema of 'type: "object"', got 'type: "None"'.

9.2.0

  • Introduced Betalgo.Ranul.OpenAI.Contracts project for centralized request/response models, enums/value types, and capability interfaces.
  • Replaced legacy image request models with Contracts equivalents:
    • CreateImageRequest, CreateImageEditRequest, CreateImageVariationRequest
  • Reorganized image enums under Betalgo.Ranul.OpenAI.Contracts.Enums.Image.* and added value types (ImageOutputFormat, ImageResponseFormat, ImageSize, ImageModeration).
  • Replaced VoiceEnum with Voice (value type). Replaced MessageRole with ChatCompletionRole and/or AssistantMessageRole.
  • Added new response base types: ResponseBase, ResponseBaseHeaderValues, improved header parsing and usage exposure.
  • Updated IImageService to consume Contracts request models and return ImageResponse for image create. Edit/variation remain legacy responses for now.
  • This is the first step of a gradual migration; changes are kept minimal and may evolve after testing.
  • See the detailed guide: Contracts Upgrade Guide (9.2.0)

9.1.0

Image Generation

  • Added UsageModel to include detailed token usage tracking for image generation responses.

JSON Schema Validation

  • Enhanced FunctionParameters with MultipleOf, Minimum, Maximum, and Pattern support.
  • Added SingleOrArrayToListConverter to handle single values or arrays.

Code Improvements

  • Refactored MessageContent with expression-bodied helpers and added binary file support.
  • Converted filter type definitions in ToolDefinition from static string to const string for clarity and performance.
  • Simplified filter creation methods.

Documentation and Formatting

  • Improved comments and formatting in ToolDefinition.
  • Fixed BOM character issue in FunctionParameters.

9.0.4

  • Updated Microsoft.Extensions.AI to version 9.5.0

9.0.3

  • Updated Microsoft.Extensions.AI to version 9.4.0-preview.1.25207.5
  • Added new models to the model list

9.0.2

  • Updated Microsoft.Extensions.AI to version 9.3.0-preview.1.25114.11
  • Added reasoning effort parameters
  • Added o1 and o3-mini models to the model list

9.0.1

  • Message list now accept RunId
  • Upgraded to Microsoft.Extensions.AI version 9.0.1, which resolves the “Method not found: '!!0” error when used alongside other SDKs with different versions.

9.0.0

  • .NET 9 support added.
  • ⚠️ Support for .NET 6 and .NET 7 has ended.
  • Fixed utility library issues and synced with latest version.

More Change Logs


For 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

评论

暂无评论