Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jodendaal/openai.net
OpenAI library for .NET
https://github.com/jodendaal/openai.net
csharp dotnet dotnet-core gpt-3 gpt3 gpt3-library http-client machine-learning ml nuget-package openai sdk
Last synced: 6 days ago
JSON representation
OpenAI library for .NET
- Host: GitHub
- URL: https://github.com/jodendaal/openai.net
- Owner: jodendaal
- License: mit
- Created: 2022-12-22T13:59:03.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-10T14:05:14.000Z (about 1 year ago)
- Last Synced: 2024-04-26T14:05:37.809Z (7 months ago)
- Topics: csharp, dotnet, dotnet-core, gpt-3, gpt3, gpt3-library, http-client, machine-learning, ml, nuget-package, openai, sdk
- Language: C#
- Homepage:
- Size: 1.82 MB
- Stars: 117
- Watchers: 7
- Forks: 26
- Open Issues: 5
-
Metadata Files:
- Readme: README.json
- License: LICENSE
Awesome Lists containing this project
README
{"text":" [![.NET](https://github.com/jodendaal/OpenAI.Net/actions/workflows/dotnet-desktop.yml/badge.svg?branch=main)](https://github.com/jodendaal/OpenAI.Net/actions/workflows/dotnet-desktop.yml) [![NuGet Badge](https://buildstats.info/nuget/OpenAI.Net.Client)](https://www.nuget.org/packages/OpenAI.Net.Client) ![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com//jodendaal/1823aaf39c6273b92442849479616daf/raw/OpenAI.Net-code-coverage.json) [![Stryker Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fjodendaal%2FOpenAI.Net%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/jodendaal/OpenAI.Net/main) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jodendaal/OpenAI.Net/blob/main/LICENSE)\r\n\r\n# OpenAI.Net\r\nOpenAI library for .NET\r\n\r\nC# .NET library for use with the OpenAI API. \r\n\r\nThis is community-maintained library.\r\n\r\nThis library supports .net core 6.0 and above.\r\n\r\n\r\n# Getting started\r\n\r\nInstall package [Nuget package](https://www.nuget.org/packages/OpenAI.Net.Client)\r\n\r\n\u0060\u0060\u0060powershell\r\nInstall-Package OpenAI.Net.Client\r\n\u0060\u0060\u0060\r\n\r\nRegister services using the provided extension methods\r\n\r\n\u0060\u0060\u0060csharp\r\nservices.AddOpenAIServices(options =\u003E {\r\n options.ApiKey = builder.Configuration[\u0022OpenAI:ApiKey\u0022];\r\n});\r\n\u0060\u0060\u0060\r\n\r\nN.B We recommened using environment variables, configuration files or secret file for storing the API key securely. See [here](https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0\u0026tabs=windows) for further details.\r\n\r\n\r\n# Example Usage\r\n\r\nYou can view examples of a console, web application and Blazor app using the streaming API [here](https://github.com/jodendaal/OpenAI.Net/tree/main/examples). \r\n\r\nYou can also have a look at the Integration Tests for usage examples [here](https://github.com/jodendaal/OpenAI.Net/tree/main/src/OpenAI.Net.Integration.Tests).\r\n\r\nSimple console app usage below.\r\n\r\n\u0060\u0060\u0060csharp\r\nusing Microsoft.Extensions.DependencyInjection;\r\nusing Microsoft.Extensions.Hosting;\r\nusing OpenAI.Net;\r\n\r\nnamespace ConsoleApp\r\n{\r\n internal class Program\r\n {\r\n static async void Main(string[] args)\r\n {\r\n using var host = Host.CreateDefaultBuilder(args)\r\n .ConfigureServices((builder, services) =\u003E\r\n {\r\n services.AddOpenAIServices(options =\u003E {\r\n options.ApiKey = builder.Configuration[\u0022OpenAI:ApiKey\u0022];\r\n });\r\n })\r\n .Build();\r\n\r\n var openAi = host.Services.GetService\u003CIOpenAIService\u003E();\r\n var response = await openAi.TextCompletion.Get(\u0022How long until we reach mars?\u0022);\r\n\r\n if (response.IsSuccess)\r\n {\r\n foreach(var result in response.Result.Choices)\r\n {\r\n Console.WriteLine(result.Text);\r\n }\r\n }\r\n else\r\n {\r\n Console.WriteLine($\u0022{response.ErrorMessage}\u0022);\r\n }\r\n }\r\n }\r\n}\r\n\u0060\u0060\u0060\r\n\r\n# Configuring Http Client Options\r\n### The registration extension allows for configuration of the http client via the IHttpClientBuilder interface.\r\n### This allows for adding a Polly retry policy for example. See example app [here](https://github.com/jodendaal/OpenAI.Net/tree/main/examples/Console/ConsoleAppWithPolly)\r\n\r\n\u0060\u0060\u0060csharp\r\nservices.AddOpenAIServices(options =\u003E {\r\n options.ApiKey = builder.Configuration[\u0022OpenAI:ApiKey\u0022];\r\n}, \r\n(httpClientOptions) =\u003E {\r\n httpClientOptions.AddPolicyHandler(GetRetryPolicy());\r\n});\r\n\r\nstatic IAsyncPolicy\u003CHttpResponseMessage\u003E GetRetryPolicy()\r\n{\r\n return HttpPolicyExtensions\r\n .HandleTransientHttpError()\r\n .WaitAndRetryAsync(3, retryAttempt =\u003E TimeSpan.FromSeconds(Math.Pow(2,\r\n retryAttempt)));\r\n}\r\n\u0060\u0060\u0060\r\n\r\n# Supported API\u0027s\r\n### Full support of all current API\u0027s\r\n- [x] [Models](https://beta.openai.com/docs/api-reference/models)\r\n - [x] [List Models](https://beta.openai.com/docs/api-reference/models/list)\r\n - [x] [Retrieve model](https://beta.openai.com/docs/api-reference/models/retrieve)\r\n- [x] [Completions](https://beta.openai.com/docs/api-reference/completions)\r\n - [x] [Create completion](https://beta.openai.com/docs/api-reference/completions/create) \r\n - [x] [Create completion with streaming](https://beta.openai.com/docs/api-reference/completions#completions/create-stream) \r\n- [x] [Edits](https://beta.openai.com/docs/api-reference/edits) \r\n - [x] [Create edit](https://beta.openai.com/docs/api-reference/edits/create)\r\n- [x] [Images](https://beta.openai.com/docs/api-reference/images)\r\n - [x] [Create image](https://beta.openai.com/docs/api-reference/images/create) \r\n - [x] [Create image edit](https://beta.openai.com/docs/api-reference/images/)\r\n - [x] [Create image variation](https://beta.openai.com/docs/api-reference/images/create-variation)\r\n- [x] [Embeddings](https://beta.openai.com/docs/api-reference/embeddings)\r\n - [x] [Create embeddings](https://beta.openai.com/docs/api-reference/embeddings/create)\r\n- [x] [Files](https://beta.openai.com/docs/api-reference/files)\r\n - [x] [List files](https://beta.openai.com/docs/api-reference/files/list) \r\n - [x] [Upload file](https://beta.openai.com/docs/api-reference/files/upload) \r\n - [x] [Delete file](https://beta.openai.com/docs/api-reference/files/delete) \r\n - [x] [Retrieve file](https://beta.openai.com/docs/api-reference/files/retrieve) \r\n - [x] [Retrieve file content](https://beta.openai.com/docs/api-reference/files/retrieve-content) \r\n\r\n- [x] [Fine-tunes](https://beta.openai.com/docs/api-reference/fine-tunes)\r\n - [x] [Create fine-tune](https://beta.openai.com/docs/api-reference/fine-tunes)\r\n - [x] [List fine-tunes](https://beta.openai.com/docs/api-reference/fine-tunes/list)\r\n - [x] [Retrieve fine-tune](https://beta.openai.com/docs/api-reference/fine-tunes/retrieve)\r\n - [x] [Cancel fine-tune](https://beta.openai.com/docs/api-reference/fine-tunes/cancel)\r\n - [x] [List fine-tune events](https://beta.openai.com/docs/api-reference/fine-tunes/events)\r\n - [x] [Delete fine-tune model](https://beta.openai.com/docs/api-reference/fine-tunes/delete-model)\r\n- [x] [Moderations](https://beta.openai.com/docs/api-reference/moderations)\r\n - [x] [Create moderation](https://beta.openai.com/docs/api-reference/moderations/create)\r\n\r\n# Testing\r\nThis project has 100% code coverage with Unit tests and 100% pass rate with [Stryker mutation testing](https://stryker-mutator.io/docs/stryker-net/introduction/). \r\n\r\nSee latest Stryker report [here](https://dashboard.stryker-mutator.io/reports/github.com/jodendaal/OpenAI.Net/main#mutant).\r\n\r\nWe also have Integration tests foreach service.\r\n\r\nThis should provide confidence in the library going forwards.\r\n\r\n\r\n\r\n# Examples\r\n\r\n### Completion\r\n\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.TextCompletion.Get(\u0022Say this is a test\u0022,(o) =\u003E {\r\n o.MaxTokens = 1024;\r\n o.BestOf = 2;\r\n });\r\n\u0060\u0060\u0060\r\n\r\n### Completion Stream\r\n\u0060\u0060\u0060csharp\r\nawait foreach(var response in service.TextCompletion.GetStream(\u0022Say this is a test\u0022))\r\n{\r\n Console.WriteLine(response?.Result?.Choices[0].Text);\r\n}\r\n\u0060\u0060\u0060\r\n### Completion with array input\r\n\u0060\u0060\u0060csharp\r\n\r\nvar prompts = new List\u003Cstring\u003E()\r\n{\r\n \u0022Say this is a test\u0022,\r\n \u0022Say this is not a test\u0022\r\n};\r\n\r\nvar response = await service.TextCompletion.Get(prompts,(o) =\u003E {\r\n o.MaxTokens = 1024;\r\n o.BestOf = 2;\r\n });\r\n\u0060\u0060\u0060\r\n\r\n### Text Edit\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.TextEdit.Get(\u0022Fix the spelling mistakes\u0022, \u0022What day of the wek is it?\u0022, (o =\u003E{\r\n o.TopP = 0.1;\r\n o.Temperature = 100;\r\n}));\r\n\u0060\u0060\u0060\r\n\r\n### Image Edit\r\n##### Using file paths\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Images.Edit(\u0022A cute baby sea otter\u0022, @\u0022Images\\BabyCat.png\u0022, @\u0022Images\\Mask.png\u0022, o =\u003E {\r\n o.N = 99;\r\n});\r\n\u0060\u0060\u0060\r\n\r\n##### Using file bytes\r\n\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Images.Edit(\u0022A cute baby sea otter\u0022,File.ReadAllBytes(@\u0022Images\\BabyCat.png\u0022), File.ReadAllBytes(@\u0022Images\\BabyCat.png\u0022), o =\u003E {\r\n o.N = 99;\r\n});\r\n\u0060\u0060\u0060 \r\n\r\n### Image Generate\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Images.Generate(\u0022A cute baby sea otter\u0022,2, \u00221024x1024\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Image Variation\r\n##### Using file paths\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Images.Variation(@\u0022Images\\BabyCat.png\u0022, o =\u003E {\r\n o.N = 2;\r\n o.Size = \u00221024x1024\u0022;\r\n});\r\n\u0060\u0060\u0060\r\n##### Using file bytes\r\n\u0060\u0060\u0060csharp\r\n var response = await service.Images.Variation(File.ReadAllBytes(@\u0022Images\\BabyCat.png\u0022), o =\u003E {\r\n o.N = 2;\r\n o.Size = \u00221024x1024\u0022;\r\n});\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Create\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.Create(\u0022myfile.jsonl\u0022, o =\u003E {\r\n o.BatchSize = 1;\r\n});\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Get All\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.Get();\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Get By Id\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.Get(\u0022fineTuneId\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Get Events\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.GetEvents(\u0022fineTuneId\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Delete\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.Delete(\u0022modelId\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Fine Tune Cancel\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.FineTune.Cancel(\u0022fineTuneId\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### File Upload\r\n##### Using file path\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Files.Upload(@\u0022Images\\BabyCat.png\u0022);\r\n\u0060\u0060\u0060\r\n##### Using file bytes\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Files.Upload(bytes, \u0022mymodel.jsonl\u0022);\r\n\u0060\u0060\u0060\r\n### File Get Content\r\n\u0060\u0060\u0060csharp\r\n var response = await service.Files.GetContent(\u0022fileId\u0022);\r\n\u0060\u0060\u0060\r\n### File Get File Detail\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Files.Get(\u0022fileId\u0022);\r\n\u0060\u0060\u0060\r\n### File Get File All\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Files.Get();\r\n\u0060\u0060\u0060\r\n### File Delete\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Files.Delete(\u00221\u0022);\r\n\u0060\u0060\u0060\r\n### Emdeddings Create\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Embeddings.Create(\u0022The food was delicious and the waiter...\u0022, \u0022text-embedding-ada-002\u0022, \u0022test\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Models Get All\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Models.Get();\r\n\u0060\u0060\u0060\r\n\r\n### Models Get By Id\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Get(\u0022babbage\u0022);\r\n\u0060\u0060\u0060\r\n\r\n### Moderation Create\r\n\u0060\u0060\u0060csharp\r\nvar response = await service.Moderation.Create(\u0022input text\u0022, \u0022test\u0022);\r\n\u0060\u0060\u0060\r\n\r\n# Contributions\r\n\r\nContributions are welcome.\r\n\r\nMinimum requirements for any PR\u0027s.\r\n\r\n- MUST include Unit tests and maintain 100% coverage.\r\n\r\n- MUST pass Stryker mutation testing with 100%\r\n- SHOULD have integration tests"}