Git Product home page Git Product logo

standard.ai.openai's Introduction

Standard.AI.OpenAI

Standard.AI.OpenAI

.NET Nuget Nuget The Standard - COMPLIANT The Standard The Standard Community

Introduction

Standard.AI.OpenAI is a Standard-Compliant .NET library built on top of OpenAI API RESTful endpoints to enable software engineers to develop AI-Powered solutions in .NET.

Standard-Compliance

This library was built according to The Standard. The library follows engineering principles, patterns and tooling as recommended by The Standard.

This library is also a community effort which involved many nights of pair-programming, test-driven development and in-depth exploration research and design discussions.

Standard-Promise

The most important fulfillment aspect in a Standard complaint system is aimed towards contributing to people, its evolution, and principles. An organization that systematically honors an environment of learning, training, and sharing knowledge is an organization that learns from the past, makes calculated risks for the future, and brings everyone within it up to speed on the current state of things as honestly, rapidly, and efficiently as possible.

We believe that everyone has the right to privacy, and will never do anything that could violate that right. We are committed to writing ethical and responsible software, and will always strive to use our skills, coding, and systems for the good. We believe that these beliefs will help to ensure that our software(s) are safe and secure and that it will never be used to harm or collect personal data for malicious purposes.

The Standard Community as a promise to you is in upholding these values.

How to use this library?

In order to use this library there are prerequists that you must complete before you can write your first AI-Powered C#.NET program. These steps are as follows:

OpenAI Account

You must create an OpenAI account with the following link: Click here

Nuget Package

Install the Standard.AI.OpenAI library in your project. Use the method best suited for your development preference listed at the Nuget Link above or below.

Nuget

API Keys

Once you've created an OpenAI account. Now, go ahead and generate an API key from the following link: Click here

Hello, World!

Once you've completed the aforementioned steps, let's write our very first program with Standard.AI.OpenAI as follows:

Completions

The following example demonstrate how you can write your first Completions program.

Program.cs

using System;
using System.Threading.Tasks;
using Standard.AI.OpenAI.Clients.OpenAIs;
using Standard.AI.OpenAI.Models.Configurations;
using Standard.AI.OpenAI.Models.Services.Foundations.Completions;

namespace ExampleOpenAIDotNet
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var openAIConfigurations = new OpenAIConfigurations
            {
                ApiKey = "YOUR_API_KEY_HERE",
                OrganizationId = "YOUR_OPTIONAL_ORG_ID_HERE"
            };

            var openAIClient = new OpenAIClient(openAIConfigurations);

            var inputCompletion = new Completion
            {
                Request = new CompletionRequest
                {
                    Prompts = new string[] { "Human: Hello!" },

                    Model = "text-davinci-003"
                }
            };

            Completion resultCompletion =
                await openAIClient.Completions.PromptCompletionAsync(
                    inputCompletion);

            Array.ForEach(
                resultCompletion.Response.Choices, 
                choice => Console.WriteLine(choice.Text));
        }
    }
}

Chat Completions

The following example demonstrate how you can write your first Chat Completions program.

Program.cs

using System;
using System.Threading.Tasks;
using Standard.AI.OpenAI.Clients.OpenAIs;
using Standard.AI.OpenAI.Models.Configurations;
using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;

namespace ExampleOpenAIDotNet
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var openAIConfigurations = new OpenAIConfigurations
            {
                ApiKey = "YOUR_API_KEY_HERE",
                OrganizationId = "YOUR_OPTIONAL_ORG_ID_HERE"
            };

            var openAIClient = new OpenAIClient(openAIConfigurations);

            var chatCompletion = new ChatCompletion
            {
                Request = new ChatCompletionRequest
                {
                    Model = "gpt-3.5-turbo",
                    Messages = new ChatCompletionMessage[]
                    {
                        new ChatCompletionMessage
                        {
                            Content = "What is c#?",
                            Role = "user",
                        }
                    },
                }
            };

            ChatCompletion resultChatCompletion =
                await openAIClient.ChatCompletions.SendChatCompletionAsync(
                    chatCompletion);

            Array.ForEach(
                resultChatCompletion.Response.Choices,
                choice => Console.WriteLine(
                    value: $"{choice.Message.Role}: {choice.Message.Content}"));
        }
    }
}

Fine-Tunes

The following example demonstrate how you can write your first Fine-tunes program.

Program.cs

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Standard.AI.OpenAI.Clients.OpenAIs;
using Standard.AI.OpenAI.Models.Configurations;
using Standard.AI.OpenAI.Models.Services.Foundations.AIFiles;
using Standard.AI.OpenAI.Models.Services.Foundations.FineTunes;

namespace Examples.Standard.AI.OpenAI.Clients.FineTunes
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var openAIConfigurations = new OpenAIConfigurations
            {
                ApiKey = "YOUR_API_KEY_HERE",
                ApiUrl = "https://api.openai.com"
            };

            IOpenAIClient openAIClient =
                new OpenAIClient(openAIConfigurations);

            MemoryStream memoryStream = CreateRandomStream();

            var aiFile = new AIFile
            {
                Request = new AIFileRequest
                {
                    Name = "Test",
                    Content = memoryStream,
                    Purpose = "fine-tune"
                }
            };

            AIFile file = await openAIClient.AIFiles
                .UploadFileAsync(aiFile);

            var fineTune = new FineTune();
            fineTune.Request = new FineTuneRequest();

            fineTune.Request.FileId =
                file.Response.Id;

            FineTune fineTuneResult =
                await openAIClient.FineTuneClient
                    .SubmitFineTuneAsync(fineTune);

            Console.WriteLine(fineTuneResult);
        }

        private static MemoryStream CreateRandomStream()
        {
            string content = "{\"prompt\": \"<prompt text>\", \"completion\": \"<ideal generated text>\"}";

            return new MemoryStream(Encoding.UTF8.GetBytes(content));
        }
    }
}

Exceptions

Standard.AI.OpenAI may throw following exceptions:

Exception Name When it will be thrown
ChatCompletionClientValidationException This exception is thrown when a validation error occurs while using the chat completion client. For example, if required data is missing or invalid.
ChatCompletionClientDependencyException This exception is thrown when a dependency error occurs while using the chat completion client. For example, if a required dependency is unavailable or incompatible.
ChatCompletionClientServiceException This exception is thrown when a service error occurs while using the chat completion client. For example, if there is a problem with the server or any other service failure.
FineTuneClientValidationException This exception is thrown when a validation error occurs while using the fine-tunes client. For example, if required data is missing or invalid.
FineTuneClientDependencyException This exception is thrown when a dependency error occurs while using the fine-tunes client. For example, if a required dependency is unavailable or incompatible.
FineTuneClientDependencyException This exception is thrown when a service error occurs while using the fine-tunes client. For example, if there is a problem with the server or any other service failure.

How to Contribute

If you want to contribute to this project please review the following documents:

If you have a question make sure you either open an issue or join our The Standard Community discord server.

Live-Sessions

Our live-sessions are scheduled on The Standard Calendar make sure you adjust the time to your city/timezone to be able to join us.

We broadcast on multiple platforms:

YouTube

LinkedIn

Twitch

Twitter

Facebook

Past-Sessions

Here's our live sessions to show you how this library was developed from scratch:

Standard.AI.OpenAI YouTube Playlist

Important Notice

This library is a community effort - it is not associated or maintained by OpenAI in any capacity

standard.ai.openai's People

Contributors

abbosbeck avatar abboshaydarov avatar adavidoaiei avatar allen-turing avatar brianlparker avatar catalin-andronie avatar cjdutoit avatar dependabot[bot] avatar diogod3 avatar elbekdeveloper avatar glhays avatar gulchexrasgithub avatar gummimundur avatar hassanhabib avatar hukailu avatar icnocop avatar khabibulladev avatar khusanrahmatullayev avatar lezune avatar mabroukmahdhi avatar menfra avatar mirzayevdeveloper avatar rcruzfreelance avatar ricardomtc avatar sammy-timalsina avatar sharaf-mansour avatar shohruhuzdev avatar sulton-max avatar ulugbek4499 avatar yusc3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

standard.ai.openai's Issues

[INFRASTRUCTURE] - Separate projects for DependencyInjection

Can we have a separate project for Dependency Injection?

In .NET community, this pattern is pretty common:

  1. We have a library for General Purpose programming i.e. like in a Console App (let assume ours Standard.OpenAI)
  2. We need a separate library in order for the Standard.OpenAI library to work with ASP.NET Core and its dependency injection framework. (Standard.OpenAI.AspNetCore)

Some examples:

  1. HotChocolate, HotChocolate.AspNetCore
  2. Newtonsoft.Json, Microsoft.AspnetCore.Mvc.NewtonsoftJson
  3. ReactiveUI, ReactiveUI.Blazor, ReactiveUI.Maui

CODE RUB: Restructure Models folder

The models folder needs to be restructured to be more Standard Compliant.
Create a subdirectory for Foundations and Services and moved the relevant classes to each.

CODE RUB: Change Acceptance Project Name to Plural Tests

Right now, the acceptance tests project is called: OpenAI.NET.Test.Acceptance

We need to change it to: OpenAI.NET.Tests.Acceptance

This will include the .csproj file, namespaces and making sure the solution targets the new .csproj name

[CONTINUOUS TEST] - Improve Automation Process

  • After performing all the tests, can we report back the code coverage results in the PR so that we can take a look at how much of the code-base is covered by out test suites?
  • Can we apply some quality gates using SonarQube to ensure the following
    • Static Code analysis passes with less number of code smells, bugs, vulnerabilities and security hotspots?
    • Code Coverage passes with a minimum percentage of Line/Branch Coverage.
  • Integrate "Mutation Testing" in out continuous testing automation process? Sometimes Code coverage are not enough to ensure the quality of our test code. Mutation tests can ensure us we have a quality test suite that fails whenever any changes is done in the app code. Also let's put a quality gate on this as well.

Support for TextCompletion Streaming API

Would be good to support the streaming method for the TextCompletion API.

In order to implement the streaming TextCompletion API RestFulSense may need to support a Streaming method that returns IAsyncEnumerable.

The key bits are

  • HttpCompletionOption.ResponseHeadersRead
  • Reading response stream as it is returned

Example of why it would be usefull https://youtu.be/hRkVGSMijjs?t=647

Example code from existing project of how I done it.

public static async IAsyncEnumerable<OpenAIHttpOperationResult<T, TError>> PostStream<T, TError>(this HttpClient httpClient, string? path, Object @object, JsonSerializerOptions? jsonSerializerOptions = null)
        {

            using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, path))
            {
                req.Content = new StringContent(JsonSerializer.Serialize(@object, jsonSerializerOptions), UnicodeEncoding.UTF8, "application/json");
                
                var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);

                if (response.IsSuccessStatusCode)
                {
                    var responseStream = await response.Content.ReadAsStreamAsync();
                    using var reader = new StreamReader(responseStream);
                    string? line = null;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {
                        if (line.StartsWith("data: "))
                            line = line.Substring("data: ".Length);

                        if (!string.IsNullOrWhiteSpace(line) && line != "[DONE]")
                        {
                            var t = JsonSerializer.Deserialize<T>(line.Trim(), jsonSerializerOptions);
                            yield return new OpenAIHttpOperationResult<T, TError>(t, response.StatusCode);
                        }
                    }
                }
            }
        }

ACCEPTANCE: CompletionClientTests Errors On Run

WireMock.Exceptions.WireMockException : Service start failed with error: One or more errors occurred. (An attempt was made to access a socket in a way forbidden by its access permissions.)

MINOR FIX: PENDING New Version of RESTFulSense with HttpFactory

If creating a HttpClient manually, i.e new HttpClient() that is either disposed frequently or is long running,it is recommended to create your own [HttpMessageHandler] and handle lifetimes etc to avoid socket exhaustion and DNS update failures. I would recommend just using the IHttpClientFactory to create new http instances which handles all of this really well.

I have dealt with this issue quite a few times in production environments with long running high through put scenarios. It's not an obvious issue, but eventually rears its ugly head.

Further Reading

https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

DOCUMENTATION: Find A New Name for This Repo

Nuget.org already has packages with the name OpenAI.NET!
That restriction on having a unique name will require us as a community to come up with a different name for this project.

Therefore, I'm asking our amazing community to do one of the following:

  • Suggest a new name
  • Vote for an existing name
  • Talk about an idea of a name even if you can't zero-in on a specific name

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.