|
| 1 | +using Microsoft.Extensions.Options; |
| 2 | + |
| 3 | +namespace Autofac.Agents.Knowledge; |
| 4 | + |
| 5 | +public sealed record KnowledgeDocument(string Source, string Text); |
| 6 | + |
| 7 | +public sealed record KnowledgeSnippet(string Source, string Text, double Score); |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Retrieves context relevant to a query, with provenance, for agents to ground on (#176). |
| 11 | +/// The backing store is pluggable — a lexical corpus today, a vector/embeddings store later. |
| 12 | +/// </summary> |
| 13 | +public interface IKnowledgeRetriever |
| 14 | +{ |
| 15 | + IReadOnlyList<KnowledgeSnippet> Search(string query, int topK); |
| 16 | +} |
| 17 | + |
| 18 | +public sealed class KnowledgeOptions |
| 19 | +{ |
| 20 | + public const string Section = "Knowledge"; |
| 21 | + |
| 22 | + /// <summary>In-memory corpus the default lexical retriever searches.</summary> |
| 23 | + public List<KnowledgeDocumentOptions> Documents { get; set; } = []; |
| 24 | + |
| 25 | + public int DefaultTopK { get; set; } = 3; |
| 26 | +} |
| 27 | + |
| 28 | +public sealed class KnowledgeDocumentOptions |
| 29 | +{ |
| 30 | + public string Source { get; set; } = string.Empty; |
| 31 | + |
| 32 | + public string Text { get; set; } = string.Empty; |
| 33 | +} |
| 34 | + |
| 35 | +/// <summary> |
| 36 | +/// Default retriever: deterministic term-overlap scoring over a configured in-memory corpus. |
| 37 | +/// Dependency-free so RAG works out of the box; a pgvector/embeddings retriever can replace it |
| 38 | +/// behind <see cref="IKnowledgeRetriever"/> without touching the agent-facing tool. |
| 39 | +/// </summary> |
| 40 | +public sealed class LexicalKnowledgeRetriever : IKnowledgeRetriever |
| 41 | +{ |
| 42 | + private static readonly char[] Separators = |
| 43 | + [' ', '\t', '\n', '\r', '.', ',', ';', ':', '!', '?', '(', ')', '[', ']', '{', '}', '"', '\'', '/', '\\', '-']; |
| 44 | + |
| 45 | + private readonly IReadOnlyList<KnowledgeDocument> _corpus; |
| 46 | + |
| 47 | + public LexicalKnowledgeRetriever(IOptions<KnowledgeOptions> options) |
| 48 | + : this((options.Value.Documents ?? []).Select(d => new KnowledgeDocument(d.Source, d.Text))) |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + public LexicalKnowledgeRetriever(IEnumerable<KnowledgeDocument> corpus) |
| 53 | + { |
| 54 | + _corpus = corpus.Where(d => !string.IsNullOrWhiteSpace(d.Text)).ToArray(); |
| 55 | + } |
| 56 | + |
| 57 | + public IReadOnlyList<KnowledgeSnippet> Search(string query, int topK) |
| 58 | + { |
| 59 | + var queryTerms = Tokenize(query).Distinct().ToArray(); |
| 60 | + if (queryTerms.Length == 0 || _corpus.Count == 0 || topK <= 0) |
| 61 | + { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + return _corpus |
| 66 | + .Select(doc => new { doc, score = Score(queryTerms, Tokenize(doc.Text)) }) |
| 67 | + .Where(scored => scored.score > 0) |
| 68 | + .OrderByDescending(scored => scored.score) |
| 69 | + .Take(topK) |
| 70 | + .Select(scored => new KnowledgeSnippet(scored.doc.Source, scored.doc.Text, scored.score)) |
| 71 | + .ToArray(); |
| 72 | + } |
| 73 | + |
| 74 | + private static double Score(IReadOnlyCollection<string> queryTerms, IEnumerable<string> docTerms) |
| 75 | + { |
| 76 | + var docSet = new HashSet<string>(docTerms); |
| 77 | + if (docSet.Count == 0) |
| 78 | + { |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + var matches = queryTerms.Count(docSet.Contains); |
| 83 | + return (double)matches / queryTerms.Count; // fraction of query terms present in the doc |
| 84 | + } |
| 85 | + |
| 86 | + private static IEnumerable<string> Tokenize(string? text) => |
| 87 | + (text ?? string.Empty) |
| 88 | + .ToLowerInvariant() |
| 89 | + .Split(Separators, StringSplitOptions.RemoveEmptyEntries) |
| 90 | + .Where(token => token.Length >= 2); |
| 91 | +} |
0 commit comments