| | | 1 | | using System.Globalization; |
| | | 2 | | using Intentum.Core.Behavior; |
| | | 3 | | using Intentum.Core.Contracts; |
| | | 4 | | using Intentum.Core.Intents; |
| | | 5 | | |
| | | 6 | | namespace Intentum.AI.SystemOne; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Uses a System One compatible engine as an <see cref="IIntentModel"/>: the behavior space |
| | | 10 | | /// is sent as state, the configured intent catalog becomes one choice question, and the |
| | | 11 | | /// engine's answer (choice + calibrated probabilities) is mapped to an <see cref="Intent"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class SystemOneIntentModel : IIntentModel |
| | | 14 | | { |
| | | 15 | | private const string IntentQuestionId = "intent"; |
| | | 16 | | |
| | | 17 | | private readonly SystemOneOptions _options; |
| | | 18 | | private readonly HttpClient _httpClient; |
| | | 19 | | |
| | | 20 | | /// <summary>Creates the model for the given engine options.</summary> |
| | 5 | 21 | | public SystemOneIntentModel(SystemOneOptions options, HttpClient httpClient) |
| | 5 | 22 | | { |
| | 5 | 23 | | _options = options ?? throw new ArgumentNullException(nameof(options)); |
| | 5 | 24 | | _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); |
| | 5 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null) |
| | 4 | 29 | | { |
| | 4 | 30 | | ArgumentNullException.ThrowIfNull(behaviorSpace); |
| | 4 | 31 | | _options.Validate(); |
| | | 32 | | |
| | 3 | 33 | | if (_options.IntentCatalog.Count == 0) |
| | 1 | 34 | | throw new InvalidOperationException( |
| | 1 | 35 | | "SystemOneOptions.IntentCatalog must contain at least one intent (name → description)."); |
| | | 36 | | |
| | 2 | 37 | | var client = new SystemOneClient(_options, _httpClient); |
| | 2 | 38 | | var response = client.Decide(BuildState(behaviorSpace), BuildQuestions()); |
| | | 39 | | |
| | 2 | 40 | | if (!response.Answers.TryGetValue(IntentQuestionId, out var answer)) |
| | 1 | 41 | | { |
| | 1 | 42 | | return new Intent( |
| | 1 | 43 | | Name: "unknown", |
| | 1 | 44 | | Signals: Array.Empty<IntentSignal>(), |
| | 1 | 45 | | Confidence: IntentConfidence.FromScore(0), |
| | 1 | 46 | | Reasoning: "System One engine returned no answer for the intent question."); |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | var probabilities = answer.Probabilities; |
| | 1 | 50 | | var name = answer.Choice ?? "unknown"; |
| | 1 | 51 | | var confidence = ResolveConfidence(answer, probabilities); |
| | 1 | 52 | | var signals = BuildSignals(answer, probabilities, confidence); |
| | | 53 | | |
| | 1 | 54 | | return new Intent( |
| | 1 | 55 | | Name: name, |
| | 1 | 56 | | Signals: signals, |
| | 1 | 57 | | Confidence: IntentConfidence.FromScore(confidence), |
| | 1 | 58 | | Reasoning: BuildReasoning(answer, probabilities, name, confidence)); |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | private object BuildState(BehaviorSpace space) |
| | 2 | 62 | | { |
| | 2 | 63 | | var events = space.Events |
| | 2 | 64 | | .Select(e => string.Create(CultureInfo.InvariantCulture, $"{e.Actor}:{e.Action}")) |
| | 2 | 65 | | .ToArray(); |
| | | 66 | | |
| | 2 | 67 | | if (space.Metadata.Count == 0) |
| | 2 | 68 | | return new Dictionary<string, object> { ["events"] = events }; |
| | | 69 | | |
| | 0 | 70 | | var state = new Dictionary<string, object> { ["events"] = events }; |
| | 0 | 71 | | state["metadata"] = space.Metadata.ToDictionary(m => m.Key, m => m.Value?.ToString() ?? ""); |
| | 0 | 72 | | return state; |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | private IReadOnlyDictionary<string, SystemOneQuestion> BuildQuestions() => |
| | 2 | 76 | | new Dictionary<string, SystemOneQuestion> |
| | 2 | 77 | | { |
| | 2 | 78 | | [IntentQuestionId] = SystemOneQuestion.Choice( |
| | 2 | 79 | | _options.IntentQuestionInstructions, |
| | 6 | 80 | | _options.IntentCatalog.ToDictionary(c => c.Key, c => (string?)c.Value)), |
| | 2 | 81 | | }; |
| | | 82 | | |
| | | 83 | | private static double ResolveConfidence(SystemOneAnswer answer, IReadOnlyDictionary<string, double>? probabilities) |
| | 1 | 84 | | { |
| | 1 | 85 | | var confidence = answer.AnswerConfidence |
| | 1 | 86 | | ?? answer.Confidence |
| | 1 | 87 | | ?? (probabilities is { Count: > 0 } ? probabilities.Values.Max() : 0.0); |
| | | 88 | | |
| | 1 | 89 | | return Math.Clamp(confidence, 0.0, 1.0); |
| | 1 | 90 | | } |
| | | 91 | | |
| | | 92 | | private IReadOnlyCollection<IntentSignal> BuildSignals( |
| | | 93 | | SystemOneAnswer answer, |
| | | 94 | | IReadOnlyDictionary<string, double>? probabilities, |
| | | 95 | | double confidence) |
| | 1 | 96 | | { |
| | 1 | 97 | | if (probabilities is null || probabilities.Count == 0) |
| | 0 | 98 | | { |
| | 0 | 99 | | return new[] |
| | 0 | 100 | | { |
| | 0 | 101 | | new IntentSignal( |
| | 0 | 102 | | Source: _options.Engine, |
| | 0 | 103 | | Description: answer.Choice ?? "no-answer", |
| | 0 | 104 | | Weight: confidence), |
| | 0 | 105 | | }; |
| | | 106 | | } |
| | | 107 | | |
| | 1 | 108 | | return probabilities |
| | 2 | 109 | | .Select(p => new IntentSignal( |
| | 2 | 110 | | Source: _options.Engine, |
| | 2 | 111 | | Description: p.Key, |
| | 2 | 112 | | Weight: p.Value)) |
| | 1 | 113 | | .ToArray(); |
| | 1 | 114 | | } |
| | | 115 | | |
| | | 116 | | private static string BuildReasoning( |
| | | 117 | | SystemOneAnswer answer, |
| | | 118 | | IReadOnlyDictionary<string, double>? probabilities, |
| | | 119 | | string name, |
| | | 120 | | double confidence) |
| | 1 | 121 | | { |
| | 1 | 122 | | if (probabilities is null || probabilities.Count == 0) |
| | 0 | 123 | | return $"choice={name}, confidence={confidence.ToString("0.00", CultureInfo.InvariantCulture)}"; |
| | | 124 | | |
| | 1 | 125 | | var parts = probabilities |
| | 2 | 126 | | .OrderByDescending(p => p.Value) |
| | 3 | 127 | | .Select(p => string.Create( |
| | 3 | 128 | | CultureInfo.InvariantCulture, |
| | 3 | 129 | | $"{p.Key}={p.Value.ToString("0.00", CultureInfo.InvariantCulture)}")); |
| | | 130 | | |
| | 1 | 131 | | var answerType = answer.Score is not null ? "score" : answer.Noul is not null ? "noul" : "choice"; |
| | 1 | 132 | | return $"{answerType}: {string.Join(", ", parts)}"; |
| | 1 | 133 | | } |
| | | 134 | | } |