Linq.AI.OpenAI
1.3.0
See the version list below for details.
dotnet add package Linq.AI.OpenAI --version 1.3.0
NuGet\Install-Package Linq.AI.OpenAI -Version 1.3.0
<PackageReference Include="Linq.AI.OpenAI" Version="1.3.0" />
paket add Linq.AI.OpenAI --version 1.3.0
#r "nuget: Linq.AI.OpenAI, 1.3.0"
// Install Linq.AI.OpenAI as a Cake Addin #addin nuget:?package=Linq.AI.OpenAI&version=1.3.0 // Install Linq.AI.OpenAI as a Cake Tool #tool nuget:?package=Linq.AI.OpenAI&version=1.3.0
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.
Object Extensions
These extensions use an OpenAI model to work with text.
Extension | Description |
---|---|
.Classify()/.ClassifyAsync() | classify the text using a model. |
.Summarize()/.SummarizeAsync() | Create a summarization for the text by using a model. |
.Matches()/.MatchesAsync() | Return whether the text matches using a model. |
.Answer()/.AnswerAsync() | get the answer to a question from the text using a model. |
.Select()/.SelectAsync() | Select a collection of items from the text using a model. |
item.Classify()
enum Genres { Rock, Pop, Electronica, Country, Classical };
var classification = item.Classify<Genres>(model);
item.Summarize()
var summary = item.Summarize(model, "with 3 words");
item.Matches()
if (item.Matches(model, "there is date"))
...
item.Answer()
var summary = text.Answer(model, "what is the birthday?");
item.Select()
Select pulls a collection of items from the source.
Example using model to select
var words = text.Select<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 = text.Select<HREF>(model);
Linq 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 internally run AI calls as throttled parallel background tasks.
enumerable.Classify()
This allows you to classify each item using a model;
enum Genres { Rock, Pop, Electronica, Country, Classical };
var classifiedItems = items.Classify<Genres>(model);
enumerable.Where()/enumerable.Remove()
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");
enumerable.Select()
.Select() let's you transform the source into target using an OpenAI model.
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)
You can use it to transform a collection of text
var markdownItems = items.Select(model, "transform each item into markdown like this:\n# {{TITLE}}\n{{AUTHOR}}\n{{Description}}")
enumerable.Summarize()
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");
enumerable.Answer()
This operator let's you ask a question for each item in a collection.
var answers = items.Answer<float>(model, "What is the cost?");
Model Extensions
All of these extensions are built up using 3 ChatClient extensions as primitives.
Extension | Description |
---|---|
.Generate()/.GenerateAsync() | use a model and a goal to return a shaped result. |
.TransformItem()/.TransformItemAsync() | use a model, object and goal to transform object into shaped result. |
.TransformItems()/.TransformItemsAsync() | use a model, collection and goal to transform each object in the collection into a collection of shaped results. |
model.Generate()/model.GenerateAsync()
Given a model and a goal return a shaped result.
var names = model.Generate<string[]>("funny names for people named bob");
var cities = model.Generate<City>("return the top 5 largest cities in the world.");
model.TransformItem()/model.TransformItemAsync()
Given a model and item, transform it into the shaped result using goal to guide the transformation.
var piglatin = model.TransformItem<string>("cow", "transform to pig latin"); // outputs "owcay"
model.TransformItem()/model.TransformItemAsync()
Given a model and item, transform it into the shaped result using goal to guide the transformation.
var piglatin = model.TransformItems<string>(["cow", "dog", "pig"], "transform to pig latin"); // outputs ["owcay", "ogday", "igpay"]
Defining new operators
To create a custom operator you create an static class and define static methods for transforming an object or collection of objects.
For example, here is the implementation of Summarize():
- The SummarizeAsync() method defines object operator which calls TransformItemAsync with a default goal of "Create a summarization" with result type of string.
- The Summarize() method defines a collection operator which calls TransformItems with a default goal of "Create a summarization" with result type for each item in the collection of string.
public static class SummarizeExtension
{
// operator to summarize object
public static string Summarize(this object source, ChatClient model, string? goal, string? instructions = null, CancellationToken cancellationToken = default)
=> source.TransformItem<string>(model, goal ?? "create a summarization", instructions, cancellationToken);
// operator to summarize object
public static Task<string> SummarizeAsync(this object source, ChatClient model, string? goal, string? instructions = null, CancellationToken cancellationToken = default)
=> source.TransformItemAsync<string>(model, goal ?? "create a summarization", instructions, cancellationToken);
// operator to summarize collection of objects
public static IList<string> Summarize(this IEnumerable<object> source, ChatClient model, string? goal = null, string? instructions = null, int? maxParallel = null, CancellationToken cancellationToken = default)
=> source.TransformItems<string>(model, goal ?? "create a summarization", instructions, maxParallel, cancellationToken);
}
Product | Versions 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. |
-
net8.0
- Iciclecreek.Async (>= 2.0.0)
- Newtonsoft.Json (>= 13.0.3)
- OpenAI (>= 2.0.0-beta.11)
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.1 | 48 | 12/16/2024 |
2.2.0 | 68 | 10/15/2024 |
2.1.0 | 60 | 10/11/2024 |
2.0.6 | 96 | 9/15/2024 |
2.0.5 | 72 | 9/14/2024 |
2.0.4 | 87 | 9/14/2024 |
2.0.3 | 60 | 9/14/2024 |
2.0.2 | 62 | 9/14/2024 |
2.0.1 | 73 | 9/14/2024 |
1.3.0 | 65 | 9/14/2024 |
1.2.3 | 64 | 9/13/2024 |
1.2.2 | 65 | 9/12/2024 |
1.2.1 | 74 | 9/12/2024 |
1.2.0 | 64 | 9/11/2024 |
1.1.0 | 66 | 9/11/2024 |
1.0.1 | 65 | 9/11/2024 |
1.0.0 | 66 | 9/11/2024 |