| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Intentum.Core.Contracts; |
| | | 3 | | using Intentum.Core.Intents; |
| | | 4 | | |
| | | 5 | | namespace Intentum.AI.Ensemble; |
| | | 6 | | |
| | | 7 | | public sealed class EnsembleIntentModel : IIntentModel |
| | | 8 | | { |
| | | 9 | | private readonly IReadOnlyList<IIntentModel> _models; |
| | | 10 | | private readonly IEnsembleStrategy _strategy; |
| | | 11 | | private readonly IReadOnlyList<double> _weights; |
| | | 12 | | |
| | 0 | 13 | | public EnsembleIntentModel( |
| | 0 | 14 | | IReadOnlyList<IIntentModel> models, |
| | 0 | 15 | | IEnsembleStrategy strategy, |
| | 0 | 16 | | IReadOnlyList<double>? weights = null) |
| | 0 | 17 | | { |
| | 0 | 18 | | _models = models; |
| | 0 | 19 | | _strategy = strategy; |
| | 0 | 20 | | _weights = weights ?? models.Select(_ => 1.0).ToList(); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null) |
| | 0 | 24 | | { |
| | 0 | 25 | | var results = _models.Select((model, i) => |
| | 0 | 26 | | { |
| | 0 | 27 | | var intent = model.Infer(behaviorSpace, precomputedVector); |
| | 0 | 28 | | return new ModelResult(intent.Name, intent.Confidence.Score, _weights[i]); |
| | 0 | 29 | | }).ToList(); |
| | | 30 | | |
| | 0 | 31 | | return _strategy.Combine(results); |
| | 0 | 32 | | } |
| | | 33 | | } |