| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Intentum.Core.Contracts; |
| | | 3 | | using Intentum.Core.Intents; |
| | | 4 | | |
| | | 5 | | namespace Intentum.Core.Pipeline; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Runs the intent resolution pipeline: signal → vector → inference → confidence. |
| | | 9 | | /// Each step is pluggable (ISignalToVector, IIntentInferenceStep, IConfidenceCalculator). |
| | | 10 | | /// Implements IIntentModel so it can be used wherever an intent model is required. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class IntentResolutionPipeline : IIntentModel |
| | | 13 | | { |
| | | 14 | | private readonly ISignalToVector _signalToVector; |
| | | 15 | | private readonly IIntentInferenceStep _inferenceStep; |
| | | 16 | | private readonly IConfidenceCalculator _confidenceCalculator; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a pipeline with the given steps. Pass null for any step to use the default. |
| | | 20 | | /// </summary> |
| | 83 | 21 | | public IntentResolutionPipeline( |
| | 83 | 22 | | IIntentInferenceStep inferenceStep, |
| | 83 | 23 | | ISignalToVector? signalToVector = null, |
| | 83 | 24 | | IConfidenceCalculator? confidenceCalculator = null) |
| | 83 | 25 | | { |
| | 83 | 26 | | _inferenceStep = inferenceStep ?? throw new ArgumentNullException(nameof(inferenceStep)); |
| | 83 | 27 | | _signalToVector = signalToVector ?? new DefaultSignalToVector(); |
| | 83 | 28 | | _confidenceCalculator = confidenceCalculator ?? new DefaultConfidenceCalculator(); |
| | 83 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null) |
| | 107 | 33 | | { |
| | 107 | 34 | | var vector = precomputedVector ?? _signalToVector.ToVector(behaviorSpace); |
| | | 35 | | |
| | 107 | 36 | | var result = _inferenceStep.Infer(behaviorSpace, vector); |
| | | 37 | | |
| | 107 | 38 | | var score = Math.Clamp(result.Score, 0, 1); |
| | 107 | 39 | | var confidence = _confidenceCalculator.FromScore(score); |
| | | 40 | | |
| | 107 | 41 | | return new Intent( |
| | 107 | 42 | | Name: result.Name, |
| | 107 | 43 | | Signals: result.Signals, |
| | 107 | 44 | | Confidence: confidence, |
| | 107 | 45 | | Reasoning: result.Reasoning |
| | 107 | 46 | | ); |
| | 107 | 47 | | } |
| | | 48 | | } |