< Summary

Information
Class: Intentum.AI.FewShot.FewShotIntentModel
Assembly: Intentum.AI
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AI/FewShot/FewShotIntentModel.cs
Line coverage
95%
Covered lines: 22
Uncovered lines: 1
Coverable lines: 23
Total lines: 43
Line coverage: 95.6%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
Infer(...)66.66%6694.44%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AI/FewShot/FewShotIntentModel.cs

#LineLine coverage
 1using Intentum.Core.Behavior;
 2using Intentum.Core.Contracts;
 3using Intentum.Core.Intents;
 4
 5namespace Intentum.AI.FewShot;
 6
 7public sealed class FewShotIntentModel : IIntentModel
 8{
 9    private readonly IFewShotStore _store;
 10    private readonly double _similarityThreshold;
 11
 312    public FewShotIntentModel(IFewShotStore store, double similarityThreshold = 0.3)
 313    {
 314        _store = store;
 315        _similarityThreshold = similarityThreshold;
 316    }
 17
 18    public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null)
 319    {
 320        var keys = behaviorSpace.Events
 421            .Select(e => e.Action)
 322            .Distinct()
 323            .ToArray();
 24
 325        var matches = _store.FindSimilar(keys, topK: 1);
 26
 327        if (matches.Count == 0)
 228            return new Intent("Unknown", [], new IntentConfidence(0, "Low"));
 29
 130        var best = matches[0];
 131        var overlap = best.BehaviorKeys.Intersect(keys).Count();
 132        var maxPossible = Math.Max(best.BehaviorKeys.Count, keys.Length);
 133        var similarity = maxPossible > 0 ? (double)overlap / maxPossible : 0;
 34
 135        var score = similarity * best.Confidence;
 36
 137        if (score < _similarityThreshold)
 038            return new Intent("Unknown", [], new IntentConfidence(score, "Low"));
 39
 140        return new Intent(best.IntentName, [], new IntentConfidence(score,
 141            IntentConfidence.FromScore(score).Level));
 342    }
 43}