-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (137 loc) · 5.13 KB
/
Copy pathProgram.cs
File metadata and controls
155 lines (137 loc) · 5.13 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
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Microsoft.Extensions.AI;
using MongoDB.AgentFramework;
OnDemandOptions command = OnDemandOptions.Parse(args);
OnDemandSettings settings = OnDemandSettings.Load();
var providerOptions = new MongoDBRAGProviderOptions
{
SearchMode = MongoDBSearchMode.FullText,
SearchIndexName = settings.SearchIndexName,
SearchTextFieldNames = ["text"],
TopK = 3,
MandatoryFilter = MongoDBRAGFilter.Equal("tenant_id", settings.TenantId),
};
await using var provider = new MongoDBRAGProvider(
settings.ConnectionString,
settings.DatabaseName,
settings.CollectionName,
providerOptions);
AIFunction retrievalTool = BuildRetrievalTool(provider);
ValidateQueryOnlySchema(retrievalTool);
if (command.ValidateOnly)
{
Console.WriteLine("Validated query-only retrieval tool configuration.");
return;
}
await provider.ValidateSearchIndexAsync();
string rendered = await InvokeQueryOnlyToolAsync(retrievalTool, settings.QueryText);
if (string.IsNullOrWhiteSpace(rendered))
{
throw new InvalidOperationException(
"No authorized knowledge matched the sample query. Preload tenant-scoped Search documents before running " +
"this sample.");
}
Console.WriteLine(rendered);
static AIFunction BuildRetrievalTool(MongoDBRAGProvider provider)
{
async Task<string> RetrieveKnowledgeAsync(string query, CancellationToken cancellationToken)
{
IReadOnlyList<MongoDBRAGResult> results = await provider.SearchAsync(query, cancellationToken);
return string.Join(
Environment.NewLine + Environment.NewLine,
results.Select(static result =>
$"[{result.SourceName ?? result.Id}] {result.Text}"));
}
return AIFunctionFactory.Create(
(Func<string, CancellationToken, Task<string>>)RetrieveKnowledgeAsync,
new AIFunctionFactoryOptions
{
Name = "retrieve_knowledge",
Description = "Retrieve application-authorized knowledge for one natural-language query.",
SerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver(),
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
},
});
}
static async Task<string> InvokeQueryOnlyToolAsync(AIFunction retrievalTool, string queryText)
{
object? invoked = await retrievalTool.InvokeAsync(
new AIFunctionArguments(
new Dictionary<string, object?>
{
["query"] = queryText,
}));
return invoked switch
{
string text => text,
JsonElement { ValueKind: JsonValueKind.String } json => json.GetString() ?? string.Empty,
JsonElement json => json.ToString(),
_ => invoked?.ToString() ?? string.Empty,
};
}
static void ValidateQueryOnlySchema(AIFunction retrievalTool)
{
JsonElement schema = retrievalTool.JsonSchema;
if (!schema.TryGetProperty("properties", out JsonElement properties))
{
throw new InvalidOperationException("The retrieval tool must expose a JSON schema with a properties object.");
}
string[] propertyNames = properties
.EnumerateObject()
.Select(static property => property.Name)
.OrderBy(static name => name, StringComparer.Ordinal)
.ToArray();
if (propertyNames.Length != 1 || propertyNames[0] != "query")
{
throw new InvalidOperationException(
"The retrieval tool schema must expose only the natural-language query parameter.");
}
if (!schema.TryGetProperty("required", out JsonElement required) ||
!required.EnumerateArray().Any(static item => item.GetString() == "query"))
{
throw new InvalidOperationException("The retrieval tool must require the query parameter.");
}
}
internal sealed record OnDemandOptions(bool ValidateOnly)
{
public static OnDemandOptions 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 OnDemandSettings(
string ConnectionString,
string DatabaseName,
string CollectionName,
string SearchIndexName,
string TenantId,
string QueryText)
{
public static OnDemandSettings Load() =>
new(
Required("MONGODB_URI"),
Required("MONGODB_DATABASE"),
Required("MONGODB_RAG_COLLECTION"),
Required("MONGODB_RAG_SEARCH_INDEX"),
Required("MONGODB_RAG_TENANT"),
"How is tenant access enforced?");
private static string Required(string name)
{
string? value = Environment.GetEnvironmentVariable(name)?.Trim();
return !string.IsNullOrWhiteSpace(value)
? value
: throw new InvalidOperationException($"Set {name} before running on-demand retrieval.");
}
}