-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
174 lines (151 loc) · 5.29 KB
/
Copy pathProgram.cs
File metadata and controls
174 lines (151 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System.Text.Json;
using System.Text.Json.Serialization;
using MongoDB.AgentFramework;
using MongoDB.Bson;
StructuredMetadataOptions command = StructuredMetadataOptions.Parse(args);
StructuredMetadataSettings settings = StructuredMetadataSettings.Load();
RetrievalPlan plan = RetrievalPlan.Parse();
var providerOptions = new MongoDBRAGProviderOptions
{
SearchMode = MongoDBSearchMode.FullText,
SearchIndexName = settings.SearchIndexName,
SearchTextFieldNames = ["text"],
MetadataFieldNames = ["metadata.category", "visibility"],
TopK = 3,
MandatoryFilter = plan.ToMandatoryFilter(settings.TenantId),
};
await using var provider = new MongoDBRAGProvider(
settings.ConnectionString,
settings.DatabaseName,
settings.CollectionName,
providerOptions);
if (command.ValidateOnly)
{
Console.WriteLine("Validated structured metadata retrieval configuration.");
return;
}
await provider.ValidateSearchIndexAsync();
IReadOnlyList<MongoDBRAGResult> results = await provider.SearchAsync(plan.Query);
if (results.Count == 0)
{
throw new InvalidOperationException(
"No structured-match knowledge was found. Preload tenant-scoped Search documents with metadata.category " +
"and visibility fields before running this sample.");
}
foreach (MongoDBRAGResult result in results)
{
string category = result.Metadata.TryGetValue("metadata.category", out BsonValue? categoryValue)
? categoryValue.ToString() ?? "n/a"
: "n/a";
string visibility = result.Metadata.TryGetValue("visibility", out BsonValue? visibilityValue)
? visibilityValue.ToString() ?? "n/a"
: "n/a";
Console.WriteLine(
$"[{result.SourceName ?? result.Id}] category={category} visibility={visibility} {result.Text}");
}
internal sealed record StructuredMetadataOptions(bool ValidateOnly)
{
public static StructuredMetadataOptions Parse(string[] args)
{
if (args.Length == 0)
{
return new(false);
}
if (args.Length == 1 && string.Equals(args[0], "--validate-only", StringComparison.Ordinal))
{
return new(true);
}
throw new ArgumentException("Usage: dotnet run --project ... -- [--validate-only]");
}
}
internal sealed record StructuredMetadataSettings(
string ConnectionString,
string DatabaseName,
string CollectionName,
string SearchIndexName,
string TenantId)
{
public static StructuredMetadataSettings Load() =>
new(
Required("MONGODB_URI"),
Required("MONGODB_DATABASE"),
Required("MONGODB_RAG_COLLECTION"),
Required("MONGODB_RAG_SEARCH_INDEX"),
Required("MONGODB_RAG_TENANT"));
private static string Required(string name)
{
string? value = Environment.GetEnvironmentVariable(name)?.Trim();
return !string.IsNullOrWhiteSpace(value)
? value
: throw new InvalidOperationException($"Set {name} before running structured metadata retrieval.");
}
}
internal sealed class RetrievalPlan
{
private const string SamplePlanJson = """
{
"query": "How is tenant access enforced?",
"category": "security",
"visibility": ["public"]
}
""";
public string Query { get; init; } = string.Empty;
public string Category { get; init; } = string.Empty;
public List<VisibilityLevel> Visibility { get; init; } = [];
public static RetrievalPlan Parse()
{
RetrievalPlan? plan = JsonSerializer.Deserialize<RetrievalPlan>(
SamplePlanJson,
new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
});
if (plan is null)
{
throw new InvalidOperationException("The sample retrieval plan could not be deserialized.");
}
plan.Validate();
return plan;
}
public MongoDBRAGFilter ToMandatoryFilter(string tenantId)
{
Validate();
return MongoDBRAGFilter.And(
MongoDBRAGFilter.Equal("tenant_id", tenantId),
MongoDBRAGFilter.Equal("metadata.category", Category),
MongoDBRAGFilter.In(
"visibility",
Visibility.Select(static value => (object)value.ToFilterValue()).ToArray()));
}
private void Validate()
{
if (string.IsNullOrWhiteSpace(Query))
{
throw new InvalidOperationException("The retrieval plan query must not be empty.");
}
if (string.IsNullOrWhiteSpace(Category))
{
throw new InvalidOperationException("The retrieval plan category must not be empty.");
}
if (Visibility.Count == 0)
{
throw new InvalidOperationException("The retrieval plan must contain at least one approved visibility.");
}
}
}
internal enum VisibilityLevel
{
Public,
Internal,
}
internal static class VisibilityLevelExtensions
{
public static string ToFilterValue(this VisibilityLevel visibility) =>
visibility switch
{
VisibilityLevel.Public => "public",
VisibilityLevel.Internal => "internal",
_ => throw new ArgumentOutOfRangeException(nameof(visibility)),
};
}