-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotAgent.cs
More file actions
201 lines (171 loc) · 7.21 KB
/
Copy pathCopilotAgent.cs
File metadata and controls
201 lines (171 loc) · 7.21 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using OperationsCopilot.Agent.Options;
using OperationsCopilot.Agent.Plugins;
using OperationsCopilot.Domain.Abstractions;
using OperationsCopilot.Domain.Chat;
// Both OpenAI.Chat and Microsoft.SemanticKernel define ChatMessageContent; alias the one
// type needed from the OpenAI package rather than importing the whole namespace.
using ChatTokenUsage = OpenAI.Chat.ChatTokenUsage;
namespace OperationsCopilot.Agent;
/// <summary>
/// The single Semantic Kernel agent behind <c>POST /api/chat</c>.
/// </summary>
/// <remarks>
/// There is no orchestration logic here on purpose. The kernel is given four tools and
/// <see cref="FunctionChoiceBehavior.Auto()"/>, and the model decides which to call and in what
/// order — including calling a database tool and the knowledge base in the same turn and
/// combining them. This class's job is to assemble the request, and to turn what happened into
/// an auditable response: the answer, the passages retrieved, the tools invoked, and the timings.
/// </remarks>
public sealed class CopilotAgent(
Kernel kernel,
IToolCallRecorder recorder,
IConversationStore conversationStore,
IOptions<CopilotAgentOptions> agentOptions,
TimeProvider timeProvider,
ILogger<CopilotAgent> logger) : ICopilotAgent
{
/// <summary>Citation excerpts are trimmed for display; the full passage stays in the database.</summary>
private const int MaxExcerptLength = 400;
private readonly CopilotAgentOptions _options = agentOptions.Value;
public async Task<ChatResponse> AskAsync(
ChatRequest request,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrWhiteSpace(request.Message);
var stopwatch = Stopwatch.StartNew();
var conversationId = request.ConversationId is { Length: > 0 } id ? id : Guid.CreateVersion7().ToString("n");
var askedAt = timeProvider.GetUtcNow();
var thread = await BuildThreadAsync(conversationId, cancellationToken);
var agent = BuildAgent();
var answer = await InvokeAsync(agent, thread, request.Message, cancellationToken);
stopwatch.Stop();
await conversationStore.AppendAsync(
conversationId,
[
new ChatTurn(ChatRole.User, request.Message, askedAt),
new ChatTurn(ChatRole.Assistant, answer.Text, timeProvider.GetUtcNow()),
],
cancellationToken);
var toolCalls = recorder.ToolCalls;
logger.LogInformation(
"Answered conversation {ConversationId} in {LatencyMs}ms using {ToolCount} tool call(s) and {CitationCount} passage(s).",
conversationId,
stopwatch.ElapsedMilliseconds,
toolCalls.Count,
recorder.RetrievedPassages.Count);
return new ChatResponse(
answer.Text,
conversationId,
BuildCitations(),
toolCalls,
stopwatch.ElapsedMilliseconds)
{
Usage = answer.Usage,
};
}
private ChatCompletionAgent BuildAgent()
{
var today = DateOnly.FromDateTime(timeProvider.GetUtcNow().UtcDateTime);
return new ChatCompletionAgent
{
Name = _options.Name,
Description = "Answers questions about Aurora Supply Co. stock, sales, products and policy.",
Instructions = CopilotSystemPrompt.Build(today, _options.AdditionalInstructions),
Kernel = kernel,
Arguments = new KernelArguments(new AzureOpenAIPromptExecutionSettings
{
// Auto is the whole point of the demo: the model picks the tools, not us.
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
Temperature = _options.Temperature,
MaxTokens = _options.MaxOutputTokens,
}),
};
}
private async Task<ChatHistoryAgentThread> BuildThreadAsync(
string conversationId,
CancellationToken cancellationToken)
{
var history = new ChatHistory();
foreach (var turn in await conversationStore.GetHistoryAsync(conversationId, cancellationToken))
{
history.Add(new ChatMessageContent(
turn.Role == ChatRole.User ? AuthorRole.User : AuthorRole.Assistant,
turn.Content));
}
return new ChatHistoryAgentThread(history, conversationId);
}
private static async Task<AgentAnswer> InvokeAsync(
ChatCompletionAgent agent,
AgentThread thread,
string message,
CancellationToken cancellationToken)
{
var text = new List<string>();
var promptTokens = 0;
var completionTokens = 0;
await foreach (var item in agent.InvokeAsync(message, thread, options: null, cancellationToken))
{
if (!string.IsNullOrWhiteSpace(item.Message.Content))
{
text.Add(item.Message.Content);
}
// Tool rounds each produce their own completion; usage has to be summed across them
// or the reported cost of a multi-tool answer is understated.
if (TryReadUsage(item.Message.Metadata, out var usage))
{
promptTokens += usage.InputTokenCount;
completionTokens += usage.OutputTokenCount;
}
}
var answer = text.Count > 0
? string.Join("\n\n", text).Trim()
: "I could not produce an answer for that. Try rephrasing the question.";
return new AgentAnswer(
answer,
promptTokens + completionTokens > 0 ? new TokenUsage(promptTokens, completionTokens) : null);
}
private static bool TryReadUsage(
IReadOnlyDictionary<string, object?>? metadata,
out ChatTokenUsage usage)
{
if (metadata is not null
&& metadata.TryGetValue("Usage", out var raw)
&& raw is ChatTokenUsage reported)
{
usage = reported;
return true;
}
usage = null!;
return false;
}
/// <summary>
/// Turns the passages retrieved this turn into citations, numbered in the same order the
/// model saw them so a <c>[2]</c> in the answer text resolves to citation 2 here.
/// </summary>
private IReadOnlyList<Citation> BuildCitations()
{
var passages = recorder.RetrievedPassages;
return [.. passages.Select((passage, index) => new Citation(
CitationReference.At(index),
passage.SourceFile,
passage.DocumentTitle,
passage.Heading,
Excerpt(passage.Content),
Math.Round(passage.Score, 4)))];
}
private static string Excerpt(string content)
{
var normalized = content.ReplaceLineEndings(" ").Trim();
return normalized.Length <= MaxExcerptLength
? normalized
: normalized[..MaxExcerptLength].TrimEnd() + "…";
}
private sealed record AgentAnswer(string Text, TokenUsage? Usage);
}