Giter VIP home page Giter VIP logo

cledev.openai's Introduction

Cledev.OpenAI

Unofficial .NET SDK for OpenAI with a Blazor Server Playground.

Main Nuget Package

Install-Package Cledev.OpenAI

API

  • Models
    • List Models
    • Retrieve Model
  • Completions
    • Create Completion
  • ChatGPT
    • Create Chat Completion
  • ChatGPT-4
    • (coming soon)
  • Edits
    • Create Edit
  • Images
    • Create Image
    • Create Image Edit
    • Create Image Variation
  • Embeddings
    • Create Embeddings
  • Audio
    • Create Transcription
    • Create Translation
  • Files
    • List Files
    • Upload File
    • Delete File
    • Retrieve File
  • Fine-tunes
    • Create Fine-tune
    • List Fine-tunes
    • Retrieve Fine-tune
    • Cancel Fine-tune
    • List Fine-tune Events
    • Delete Fine-tune Model
  • Moderations
    • Create Moderation

Utilities

  • Azure OpenAI Support
    • (coming soon)

Configuration

Option 1

services.AddOpenAIClient();

This option requires an appsettings.json file

{
  "OpenAI": {
    "ApiKey": "YOUR_API_KEY",
    "Organization": "OUR_ORGANIZATION"
  }
}

Option 2

services.AddOpenAIClient(options =>
{
     options.ApiKey = Environment.GetEnvironmentVariable("YOUR_API_KEY");
     options.Organization = Environment.GetEnvironmentVariable("YOUR_ORGANIZATION");
 });

Important: use environment variables to load the OpenAI API key, do not hardcode it in the source code.

Usage

Inject IOpenAIClient interface into your service

ChatGPT Stream Example

var request = new CreateChatCompletionRequest
{
    Model = ChatModel.Gpt35Turbo.ToStringModel(),
    Stream = true,
    MaxTokens = 500,
    Messages = new List<ChatCompletionMessage>
    {
        new("system", "You are a helpful assistant."),
        new("user", "Who won the world series in 2020?"),
        new("assistant", "The Los Angeles Dodgers won the World Series in 2020."),
        new("user", "Where was it played?")
    }
};

var completions = client.CreateChatCompletionAsStream(request);

await foreach (var completion in completions)
{
    Console.Write(completion.Choices[0].Message?.Content);
}

Completion Stream Example

var request = new CreateCompletionRequest
{
    Model = CompletionsModel.TextDavinciV3.ToStringModel(),
    Stream = true,
    Prompt = "Please write a 1000 word assay about differences between functional programming and object oriented programming",
    MaxTokens = 500
};

var completions = client.CreateCompletionAsStream(request);

await foreach (var completion in completions)
{
    Console.Write(completion.Choices[0].Text);
}

Audio Transcription Example (Whisper)

const string fileName = "YOUR_RECORDING.m4a";
var fileBytes = await File.ReadAllBytesAsync($"Data/{fileName}");

var request = new CreateAudioTranslationRequest
{
    Model = AudioModel.Whisper1.ToStringModel(),
    File = fileBytes,
    FileName = fileName,
    ResponseFormat = AudioResponseFormat.VerboseJson.ToStringFormat()
};

var response = await client.CreateAudioTranslation(request);

Console.Write($"{JsonSerializer.Serialize(response, jsonSerializerOptions)}");

Create Image Example (Dall-E)

var request = new CreateImageRequest
{
    Prompt = "Once upon a time",
    Size = ImageSize.Size512x512.ToStringSize(),
    ResponseFormat = ImageResponseFormat.B64Json.ToStringFormat(),
    N = 1
};

var response = await client.CreateImage(Request);

<img src="@response.Data[0].Url" />

Other Examples

Please, have a look at the Blazor Server Playground for more examples on how to use the client.

Index

ChapGPT

Completions

Edits

Images

Audio Transcriptions

cledev.openai's People

Contributors

lucabriguglia 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

cledev.openai's Issues

API key in OpenAI.NET.Playground.Web/wwwroot/appsettings.json

Putting the API key in this location would cause it to end up in the web browser cache and completely exposed if deployed to the web.

Having that key leaked would be very bad and that mistake could be made by a novice user. If anyone deployed the app to the public web it would be a disaster (because the hackers would be looking for this).

Perhaps use Blazor Server or Blazor MAUI rather than Blazor WebAssembly so that the key could be placed in appsettings.json (or secrets file)?

GPT-4

Just curious to know if GPT-4 support is planned. Greatly appreciate your effort with this project.

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.