| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Intentum.Core.Behavior; |
| | | 3 | | using Intentum.Core.Contracts; |
| | | 4 | | using Intentum.Core.Intents; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Intentum.AI; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Streaming and batch extensions for <see cref="IIntentModel"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | [UsedImplicitly] |
| | | 13 | | public static class IntentModelStreamingExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Infers intent for each behavior space and returns results as a sequence (lazy enumeration). |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="model">The intent model.</param> |
| | | 19 | | /// <param name="spaces">Behavior spaces to infer.</param> |
| | | 20 | | /// <returns>Sequence of inferred intents in the same order as <paramref name="spaces"/>.</returns> |
| | | 21 | | [UsedImplicitly] |
| | | 22 | | public static IEnumerable<Intent> InferMany( |
| | | 23 | | this IIntentModel model, |
| | | 24 | | IEnumerable<BehaviorSpace> spaces) |
| | 5 | 25 | | { |
| | 5 | 26 | | ArgumentNullException.ThrowIfNull(model); |
| | 4 | 27 | | ArgumentNullException.ThrowIfNull(spaces); |
| | 3 | 28 | | return InferManyIterator(model, spaces); |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static IEnumerable<Intent> InferManyIterator(IIntentModel model, IEnumerable<BehaviorSpace> spaces) |
| | 3 | 32 | | { |
| | 17 | 33 | | foreach (var space in spaces) |
| | 4 | 34 | | yield return model.Infer(space); |
| | 3 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Infers intent for each behavior space as they are enumerated, yielding results as they are ready (async stream). |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="model">The intent model.</param> |
| | | 41 | | /// <param name="spaces">Async sequence of behavior spaces.</param> |
| | | 42 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 43 | | /// <returns>Async sequence of inferred intents in the same order as <paramref name="spaces"/>.</returns> |
| | | 44 | | [UsedImplicitly] |
| | | 45 | | public static IAsyncEnumerable<Intent> InferManyAsync( |
| | | 46 | | this IIntentModel model, |
| | | 47 | | IAsyncEnumerable<BehaviorSpace> spaces, |
| | | 48 | | CancellationToken cancellationToken = default) |
| | 5 | 49 | | { |
| | 5 | 50 | | ArgumentNullException.ThrowIfNull(model); |
| | 4 | 51 | | ArgumentNullException.ThrowIfNull(spaces); |
| | 3 | 52 | | return InferManyAsyncIterator(model, spaces, cancellationToken); |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | private static async IAsyncEnumerable<Intent> InferManyAsyncIterator( |
| | | 56 | | IIntentModel model, |
| | | 57 | | IAsyncEnumerable<BehaviorSpace> spaces, |
| | | 58 | | [EnumeratorCancellation] CancellationToken cancellationToken) |
| | 3 | 59 | | { |
| | 22 | 60 | | await foreach (var space in spaces.WithCancellation(cancellationToken)) |
| | 7 | 61 | | { |
| | 7 | 62 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 7 | 63 | | yield return model.Infer(space); |
| | 6 | 64 | | } |
| | 3 | 65 | | } |
| | | 66 | | } |