Repository navigation
Expand file tree
/
Copy pathScriptedModel.cs
More file actions
141 lines (118 loc) · 6.8 KB
/
Copy pathScriptedModel.cs
File metadata and controls
141 lines (118 loc) · 6.8 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
using System.Globalization;
using System.Text.RegularExpressions;
using AgentExperience.MicrosoftAgentFramework.Injection;
using Microsoft.Extensions.AI;
namespace AgentExperience.Sample.QuickStart;
/// <summary>
/// A scripted stand-in for a model, so the demo needs no key and no network. It is not a model and is never presented
/// as one: the demo's output says so.
/// </summary>
/// <remarks>
/// It reads only what a real model is sent, the messages and the instructions. It tries the tool's strategies in their listed order, one
/// call per turn, until one exits 0, with two exceptions: when its input holds a Historical Reference whose
/// <c>Worked:</c> attempt has a call marked <c>[returned]</c> with a strategy, it tries the first such one first; and when
/// its instructions hold a recall hint (<c>--recall</c>) whose <c>worked:</c> line has such a call, it tries that one
/// before anything else. So whatever changes in the second run comes from what the library retrieved and injected, not
/// from this script.
/// </remarks>
public sealed partial class ScriptedModel : IChatClient
{
private int _sequence;
public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
var list = messages.ToList();
var results = list.SelectMany(message => message.Contents.OfType<FunctionResultContent>());
if (results.Any(result => result.Result?.ToString()?.StartsWith("exit=0", StringComparison.Ordinal) == true))
{
return Reply(new ChatMessage(ChatRole.Assistant, "The refund is released."));
}
var tried = list.SelectMany(message => message.Contents.OfType<FunctionCallContent>())
.Select(call => call.Arguments?.TryGetValue("strategy", out var value) == true ? value?.ToString() : null)
.ToHashSet(StringComparer.Ordinal);
var block = list.Select(message => message.Text).FirstOrDefault(text => text.Contains(HistoricalReferenceWriter.BlockBegin, StringComparison.Ordinal));
var order = new List<string>();
if (Hinted(options?.Instructions) is { } hinted)
{
order.Add(hinted);
}
if (Remembered(block) is { } remembered)
{
order.Add(remembered);
}
order.AddRange(RefundDesk.Strategies);
if (order.FirstOrDefault(strategy => !tried.Contains(strategy)) is not { } next)
{
return Reply(new ChatMessage(ChatRole.Assistant, "Every strategy failed; escalating the ticket."));
}
var task = list.LastOrDefault(message => message.Role == ChatRole.User && TicketNumber().IsMatch(message.Text))?.Text ?? string.Empty;
var callId = "call-" + (++_sequence).ToString(CultureInfo.InvariantCulture);
return Reply(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent(callId, RefundDesk.ToolName, new Dictionary<string, object?>
{
["ticketId"] = TicketNumber().Match(task).Groups[1].Value,
["strategy"] = next,
})]));
}
/// <summary>
/// The strategy of the first call marked <c>[returned]</c> (or <c>[returned: "…"]</c>, with a recipe) that carries one, on the <c>Tried:</c> line of the attempt
/// the block's <c>Worked:</c> line names: the call that did not fail, wherever it sits on the line and wherever
/// <c>strategy</c> sits among its arguments. <see langword="null"/> when there is no such line or no returned call on
/// it carries a strategy.
/// </summary>
internal static string? Remembered(string? block)
{
if (block is null || WorkedLine().Match(block) is not { Success: true } worked)
{
return null;
}
var tried = Regex.Match(block, "^ - attempt " + worked.Groups[1].Value + ": (.*)$", RegexOptions.Multiline | RegexOptions.CultureInvariant);
// Walks the arguments one by one, so a value can never be read as a key.
return FirstReturnedStrategy(tried.Groups[1].Value);
}
/// <summary>
/// The strategy of the first call marked <c>[returned]</c> on a recall hint's <c>worked:</c> line, when the
/// instructions carry one (<c>options.Recall</c>, after a failed call); otherwise <see langword="null"/>.
/// </summary>
internal static string? Hinted(string? instructions)
{
if (instructions is null || instructions.IndexOf(HistoricalReferenceWriter.RecallBegin, StringComparison.Ordinal) is not (>= 0 and var at))
{
return null;
}
var worked = Regex.Match(instructions[at..], "^" + Regex.Escape(HistoricalReferenceWriter.RecallWorkedPrefix) + "(.*)$", RegexOptions.Multiline | RegexOptions.CultureInvariant);
return worked.Success ? FirstReturnedStrategy(worked.Groups[1].Value) : null;
}
private static string? FirstReturnedStrategy(string line)
{
foreach (Match call in ReturnedCall().Matches(line))
{
foreach (Match argument in Argument().Matches(call.Groups[1].Value))
{
if (argument.Groups["key"].Value == "strategy" && argument.Groups["quoted"].Success)
{
return argument.Groups["quoted"].Value;
}
}
}
return null;
}
public IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default) =>
throw new NotSupportedException("The scripted stand-in does not stream.");
public object? GetService(Type serviceType, object? serviceKey = null) => null;
public void Dispose()
{
}
private static Task<ChatResponse> Reply(ChatMessage message) => Task.FromResult(new ChatResponse(message) { ModelId = "scripted-stand-in" });
[GeneratedRegex(@"#(\d+)", RegexOptions.CultureInvariant)]
private static partial Regex TicketNumber();
[GeneratedRegex(@"^Worked: attempt (\d+)", RegexOptions.Multiline | RegexOptions.CultureInvariant)]
private static partial Regex WorkedLine();
/// <summary>
/// One call's argument list followed by its own <c>[returned]</c> marker. A quoted value is matched whole (the
/// writer never lets one hold a double quote), so a parenthesis or a marker inside a value cannot end the list.
/// </summary>
[GeneratedRegex("\\(((?:\"[^\"]*\"|\\([^()\"]*\\)|[^\"()])*)\\) \\[returned(?:: \"[^\"]*\"(?:\\[\\.\\.\\.\\])?)?\\]", RegexOptions.CultureInvariant)]
private static partial Regex ReturnedCall();
/// <summary>One <c>key=value</c> of a call's argument list, each starting where the previous one ended.</summary>
[GeneratedRegex("\\G(?:, )?(?<key>[^=,\"]+)=(?:\"(?<quoted>[^\"]*)\"(?:\\[\\.\\.\\.\\])?|\\([^)]*\\)|[^,\"]*)", RegexOptions.CultureInvariant)]
private static partial Regex Argument();
}