Linq.AI.OpenAI 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Linq.AI.OpenAI --version 1.2.1                
NuGet\Install-Package Linq.AI.OpenAI -Version 1.2.1                
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="Linq.AI.OpenAI" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Linq.AI.OpenAI --version 1.2.1                
#r "nuget: Linq.AI.OpenAI, 1.2.1"                
#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 Linq.AI.OpenAI as a Cake Addin
#addin nuget:?package=Linq.AI.OpenAI&version=1.2.1

// Install Linq.AI.OpenAI as a Cake Tool
#tool nuget:?package=Linq.AI.OpenAI&version=1.2.1                

Linq.AI.OpenAI

This library adds Linq extension methods using OpenAI structured outputs.

This library was heaviy inspired by stevenic's agentm-js library, Kudos!

Installation

dotnet add package Linq.AI.OpenAI

Architecture

For each element in a collection an model API call is made to evaluate and return the result. These are done in parallel on background threads.

OpenAI model

To use these methods you will need to instantiate a ChatClient model like this:

var model = new ChatClient(model: "gpt-4o-mini", "<modelKey>");

NOTE: The model must support structured output.

String Extensions

These extensions use an OpenAI model to work with text.

Extension Description
.ClassifyAsync() classify the text using a model.
.SummarizeAsync() Create a summarization for the text by using a model.
.MatchesAsync() Return whether the text matches using a model.
.AnswerAsync() get the answer to a question from the text using a model.
.SelectAsync() Generate a collection of items from the text using a model.

Examples

.Classify() text

enum Genres { Rock, Pop, Electronica, Country, Classical };
var classification = await text.ClassifyAsync<Genres>(model);

.Summarize() text

var summary = await text.SummarizeAsync(model, "with 3 words");

.Matches() text

if (await text.MatchesAsync(model, "there is date"))
  ...

.Answer() text

var summary = await text.AnswerAsync(model, "what is the birthday?");

.Select() text

Example using model to select

var words = await text.SelectAsync<string>(model, "The second word of every paragraph");

Example using model to select structed data.

public class HREF 
{ 
	public string Url {get;set;}
	public string Title {get;set;}
}
var summary = await text.SelectAsync<HREF>(model);

Collection Extensions

These collection extensions use an OpenAI model to work with collections using Linq style methods.

Extension Description
.Where() Filter the collection of items by using a model. filter
.Select() transform the item into another format using a model.
.Remove() Remove items from a collection of items by using a model. filter
.Summarize() Create a summarization for each item by using a model.
.Classify() classify each item using a model.
.Answer() get the answer to a question for each item using a model.

NOTE: These methods are synchronous linq methods, but internally they run all of the AI calls as throttled parallel background tasks.

Examples

.Classify() items

This allows you to classify each item using a model;

enum Genres { Rock, Pop, Electronica, Country, Classical };
var classifiedItems = items.Classify<Genres>(model);

.Where()/.Remove() items

Filter a collection using natural language

var breadboxItems = items.Where(model, "item would fit in a bread box");
var bigItems = items.Remove(model, "item would fit in a bread box");

.Select() items

.Select() let's you transform the source into target using an OpenAI model.

Object transformation

You can use it to transform an object from one format to another by simply giving the types. It will use model. to do the transformation.

var targetItems = items.Select<SourceItem,TargetItem>(model)
String transformation
var markdownItems = items.Select(model, "transform each item into markdown like this:\n# {{TITLE}}\n{{AUTHOR}}\n{{Description}}")

.Summarize() items

Generate text summary for each item using an OpenAI model.

var summaries = items.Summarize(model);

You can control the summarization with a hint

var summaries = items.Summarize(model, "generate a 3 word summary");

.Answer() items

This operator let's you ask a question for each item in a collection.

var answers = items.Answer(model, "What is total cost of the item as a float?").Select(answer => Convert.ToFloat(answer));
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
2.2.0 55 10/15/2024
2.1.0 51 10/11/2024
2.0.6 85 9/15/2024
2.0.5 64 9/14/2024
2.0.4 80 9/14/2024
2.0.3 53 9/14/2024
2.0.2 53 9/14/2024
2.0.1 66 9/14/2024
1.3.0 56 9/14/2024
1.2.3 55 9/13/2024
1.2.2 58 9/12/2024
1.2.1 64 9/12/2024
1.2.0 57 9/11/2024
1.1.0 59 9/11/2024
1.0.1 58 9/11/2024
1.0.0 59 9/11/2024