fAI 1.0.28

There is a newer version of this package available.
See the version list below for details.
dotnet add package fAI --version 1.0.28                
NuGet\Install-Package fAI -Version 1.0.28                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="fAI" Version="1.0.28" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add fAI --version 1.0.28                
#r "nuget: fAI, 1.0.28"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install fAI as a Cake Addin
#addin nuget:?package=fAI&version=1.0.28

// Install fAI as a Cake Tool
#tool nuget:?package=fAI&version=1.0.28                

fAI

Introduction

  • AI Experimentation With Different Providers in C#
  • OpenAI
    • Querying GPT
    • Image generation (DALL-E)
    • Embeddings
    • Speech To Text
    • Text To Speech
  • Microsoft Cognitive Services
    • Text To Speech
    • Azure Search Index with Embeddings
  • Deepgram
    • Speech To Text

Logo

Overview

Library fAI available on NuGet.

Querying OpenAI GPT models

Querying and expecting answer in JSON format.

[Fact()]
public void Completion_JsonMode_WorldCup()
{
    var client = new OpenAI();
    var p = new Prompt_GPT_35_Turbo_JsonAnswer
    {
        Messages = new List<GPTMessage>()
        {
            new GPTMessage { Role =  MessageRole.system, Content = "You are a helpful assistant designed to output JSON." },
            new GPTMessage { Role =  MessageRole.user,   Content = "Who won the soccer world cup in 1998?" }
        }
    };
    var response = client.Completions.Create(p);
    Assert.True(response.Success);
    var answer = response.JsonObject["winner"];
    Assert.Equal("France", answer);
}

Conversation and data analysis with GPT 4

[Fact()]
public void Completion_Chat_QuestionAboutPastSchedule()
{
    var client = new OpenAI();
    var prompt = new Prompt_GPT_4
    {
        Messages = new List<GPTMessage> 
        {
            new GPTMessage { Role =  MessageRole.system, Content = "You are a helpful assistant." },
            new GPTMessage { Role =  MessageRole.user,   Content = $"08/02/2021 15:00 Meeting with Eric." },
            new GPTMessage { Role =  MessageRole.user,   Content = $"09/01/2021 15:00 Meeting with Eric." },
            new GPTMessage { Role =  MessageRole.user,   Content = $"09/10/2021 10:00 Take the dog to the vet." },
            new GPTMessage { Role =  MessageRole.user,   Content = $"09/20/2021 15:00 Meeting with Rick and John" },
        }
    };
    var response = client.Completions.Create(prompt);

    prompt.Messages.Add(new GPTMessage { Role = MessageRole.user, Content = "When was the last time I talked with Eric?" });
    response = client.Completions.Create(prompt);
    Assert.Matches(@"Eric.*09\/01\/2021 at 15:00", response.Text);

    prompt.Messages.Add(new GPTMessage { Role = MessageRole.user, Content = "What do I have to do on 09/10/2021?" });
    response = client.Completions.Create(prompt);
    Assert.Matches(@"dog.*vet.*10:00", response.Text);
}

Translation with model gpt-3.5-turbo

public string Translate(string text, TranslationLanguages sourceLangague, TranslationLanguages targetLanguage);
const string ReferenceEnglishSentence = "Hello world.";
[Fact()]
public void Translate_EnglishToSpanish()
{
    var client = new OpenAI();
    var translation = client.Completions.Translate(ReferenceEnglishSentence, TranslationLanguages.English, TranslationLanguages.Spanish);
    Assert.Equal("'Hola mundo.'", translation);
}

Others helper to execute translations. Translating dictionary or list of strings can do 2 things

  1. Translate more than one text with one call to GPT.
  2. For a dictionary the key is not translated as part of the answer and can be use to map the translation with the original texts.
public Dictionary<string, string> Translate(Dictionary<string, string> inputDictionary, TranslationLanguages sourceLangague, TranslationLanguages targetLanguage);
public List<string>               Translate(List<string> strings, TranslationLanguages sourceLangague, TranslationLanguages targetLanguage);

Querying OpenAI Embeddings API

 public class OpenAIEmbeddingsTests
 {
     [Fact()]
     public void Embeddings_Create()
     {
         var input = "I am he as you are he as you are me. And we are all together. See how they run like pigs from a gun. See how they fly. I'm crying.";
         var client = new OpenAI();
         var r = client.Embeddings.Create(input);

         // r.Data[0].Embedding contains the list of float 

         Assert.Equal("embedding", r.Data[0].Object);
         Assert.Equal(r.Data[0].EmbeddingMaxValue, r.Data[0].Embedding.Count);
         Assert.Equal(37, r.Usage.PromptTokens);
         Assert.Equal(37, r.Usage.TotalTokens);
     }
 }

Querying OpenAI DALL-E. Image Generation

[Fact()]
public void Image_Generate()
{
    var prompt = @"Generate an image inspired by Victor Hugo's classic novel, 'Les Misérables'. 
The image should depict three characters, each with distinct characteristics. 
The first is an older, physically strong man with a scarred face, wearing threadbare clothes, indicative of a hard life — this represents Jean Valjean. 
The second is a young woman radiating innocence and kindness; she wears modest clothes and has beautiful shining eyes — this is Cosette. 
The third is a stern-looking middle-aged man in a gentleman's attire and hat,  representing law and order — representative of Javert. 
Their expressions should reflect the nuances of the complex relationships 
they share in the story.
";
    var client = new OpenAI();
    var r = client.Image.Generate(prompt, size :  OpenAIImageSize._1792x1024);
    var pngFileNames = r.DownloadImage();
    Assert.True(pngFileNames.Count == 1);
    Assert.True(File.Exists(pngFileNames[0]));
}

<img src=".\Image_Generate.example.png" alt="Image_Generate.example.png" width="448"/>

Third parties API Key

API keys can be hard coded in code or read automatically from environment variable.

Environment Variable Names
MICROSOFT_AZURE_SEARCH_KEY
MICROSOFT_COGNITIVE_SERVICES_KEY
MICROSOFT_COGNITIVE_SERVICES_REGION
OPENAI_API_KEY
OPENAI_ORGANIZATION_ID
OPENAI_LOG_FILE
DEEPGRAM_API_KEY
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.31 82 11/30/2024
1.0.30 91 10/27/2024
1.0.29 86 10/27/2024
1.0.28 117 8/8/2024
1.0.27 134 3/2/2024
1.0.26 131 2/25/2024
1.0.25 134 2/25/2024
1.0.24 124 2/17/2024
1.0.23 188 12/30/2023
1.0.22 118 12/30/2023
1.0.21 112 12/30/2023
1.0.20 125 12/30/2023
1.0.19 116 12/30/2023
1.0.18 126 12/27/2023
1.0.17 126 12/27/2023
1.0.16 125 12/27/2023
1.0.15 179 11/24/2023
1.0.14 124 11/24/2023
1.0.13 116 11/24/2023
1.0.12 130 11/24/2023
1.0.11 128 11/17/2023
1.0.10 119 11/17/2023
1.0.9 110 11/13/2023
1.0.8 117 11/12/2023
1.0.7 115 11/11/2023
1.0.6 104 11/11/2023
1.0.5 110 11/11/2023
1.0.4 104 11/9/2023
1.0.3 108 11/9/2023
1.0.2 111 11/9/2023
1.0.1 110 11/9/2023
1.0.0 113 11/8/2023

Creation