Documentation
Table of Contents

Collections

Six methods apply an intent to a whole collection in one call: map, filter, sort, group, reduce and map-values. Each is an extension method in the IntentSharp.Extensions namespace that takes the client, the intent and, optionally, AIRequestOptions and a CancellationToken, and returns an AIResult like any other call: check Success before you read Value. Every method but reduce adds a check of its own, so an answer that does not fit the collection fails, and the model is asked again with the problem, up to MaxAttempts.

Method You get back It fails when
AIMapAsync<TIn, TOut> List<TOut>, one result per element, in the input's order the count of results differs from the count of elements
AIFilterAsync<T> List<T>: your elements that match, in their original order the answer names an element the collection does not have
AISortAsync<T> List<T>: all your elements, in the new order an element is missing or repeated
AIGroupAsync<T> Dictionary<string, List<T>>: your elements under labels the model chooses an element is in no group or in two, or a label is empty or repeated, ignoring case
AIReduceAsync<TIn, TOut> one TOut for the whole collection never on its own; add your own check
AIMapValuesAsync<TKey, TIn, TOut> Dictionary<TKey, TOut> with your original keys a key is missing, added or repeated, or a value is null and TOut is not a nullable value type

Map

AIResult<List<string>> summaries = await tickets.AIMapAsync<SupportTicket, string>(client,
    "Summarize the ticket in at most ten words.");

Filter, sort and group hand back your own objects

On success the original objects come back, selected or ordered by the model: a filter's matches once each, and every element exactly once for sort and group. They are the same instances, with any state that does not serialize still on them. Filter returns an empty list when nothing matches.

AIResult<List<SupportTicket>> defects = await tickets.AIFilterAsync(client,
    "Tickets that describe a defect rather than a feature request.");
if (defects.Success)
    foreach (var ticket in defects.Value!)
        Console.WriteLine($"#{ticket.Id} {ticket.Title}");
AIResult<List<SupportTicket>> ranked = await tickets.AISortAsync(client,
    "Most severe first: data loss, then outages, then wrong results, then cosmetic problems.");
AIResult<Dictionary<string, List<SupportTicket>>> byArea = await tickets.AIGroupAsync(client,
    "Group by the product area the ticket is about.");
if (byArea.Success)
    foreach (var (area, inArea) in byArea.Value!)
        Console.WriteLine($"{area}: {inArea.Count}");

The group labels are the model's, so write the intent to steer them ("by product area"). The dictionary's keys compare ignoring case.

Reduce

AIResult<string> digest = await tickets.AIReduceAsync<SupportTicket, string>(client,
    "Write a three-sentence digest of these tickets for the support lead.");

Map-values keeps your keys

// Key: SKU. Value: the product name in English.
AIResult<Dictionary<string, string>> french = await productNames.AIMapValuesAsync<string, string, string>(client,
    "Translate the product name into French.");

Two keys whose ToString() is the same throw ArgumentException, naming them, before any model call.

Add a check of your own

Pass AIRequestOptions with Require<T> or Validate<T>, as for any call; a call never changes the options you pass, so one options object can be reused:

var options = new AIRequestOptions { MaxAttempts = 3 }
    .Require<List<string>>(list => list.All(s => s.Length <= 80), "Every summary must be at most 80 characters long.");

var summaries = await tickets.AIMapAsync<SupportTicket, string>(client,
    "Summarize the ticket in at most ten words.", options);

On map your check receives the List<TOut>, and on reduce the TOut. On filter, sort, group and map-values it does not receive your elements, so a check typed on them (Require<List<Ticket>>, for example) fails every attempt: check what they return after the call instead.

Without passing the client

Inside client.AsAmbient(), the six methods work without the client argument:

using var scope = client.AsAmbient();

var defects = await tickets.AIFilterAsync("Tickets that describe a defect rather than a feature request.");

The scope belongs to the current async flow and ends when it is disposed; open it in the method that makes the calls, or in one of its callers. With no scope open, these methods throw InvalidOperationException.

Limits

  • The whole collection goes in one request, so its size counts against MaxInputChars (200,000 characters by default); a larger one throws ArgumentException before any model call. With AIRequestOptions.Partition = CollectionPartition.ByItems, AIMapAsync, AIFilterAsync and AIMapValuesAsync split such a collection into batches of whole items instead, run one after another, each with its own checks, and merge them: map joins the lists in order, filter returns the original objects, map-values unions the keys. The result succeeds only when every batch does, and the first failed batch stops the rest; MaxAttempts, MaxModelCalls and the time budget apply to each batch, a warning says how many batches ran, and an item too long on its own still throws. Sort, group and reduce need the whole set and ignore it.
  • The checks are about the collection's shape. Whether an element belongs in a group is the model's judgement; add checks of your own where a rule can say it.
  • The methods for one value, AIAsync, AIAsync<T>, AIConvertAsync<T> and AIClassifyAsync<TEnum>, are in AIExtensions.