-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathStep07_Telemetry.cs
236 lines (205 loc) · 9.79 KB
/
Step07_Telemetry.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Microsoft. All rights reserved.
using System.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Chat;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
namespace GettingStarted;
/// <summary>
/// A repeat of <see cref="Step03_Chat"/> with telemetry enabled.
/// </summary>
public class Step07_Telemetry(ITestOutputHelper output) : BaseAssistantTest(output)
{
/// <summary>
/// Instance of <see cref="ActivitySource"/> for the example's main activity.
/// </summary>
private static readonly ActivitySource s_activitySource = new("AgentsTelemetry.Example");
/// <summary>
/// Demonstrates logging in <see cref="ChatCompletionAgent"/>, <see cref="OpenAIAssistantAgent"/> and <see cref="AgentGroupChat"/>.
/// Logging is enabled through the <see cref="Agent.LoggerFactory"/> and <see cref="AgentChat.LoggerFactory"/> properties.
/// This example uses <see cref="XunitLogger"/> to output logs to the test console, but any compatible logging provider can be used.
/// </summary>
[Fact]
public async Task LoggingAsync()
{
await RunExampleAsync(loggerFactory: this.LoggerFactory);
// Output:
// [AddChatMessages] Adding Messages: 1.
// [AddChatMessages] Added Messages: 1.
// [InvokeAsync] Invoking chat: Microsoft.SemanticKernel.Agents.ChatCompletionAgent:63c505e8-cf5b-4aa3-a6a5-067a52377f82/CopyWriter, Microsoft.SemanticKernel.Agents.ChatCompletionAgent:85f6777b-54ef-4392-9608-67bc85c42c5b/ArtDirector
// [InvokeAsync] Selecting agent: Microsoft.SemanticKernel.Agents.Chat.SequentialSelectionStrategy.
// [NextAsync] Selected agent (0 / 2): 63c505e8-cf5b-4aa3-a6a5-067a52377f82/CopyWriter
// and more...
}
/// <summary>
/// Demonstrates tracing in <see cref="ChatCompletionAgent"/> and <see cref="OpenAIAssistantAgent"/>.
/// Tracing is enabled through the <see cref="TracerProvider"/>.
/// For output this example uses Console as well as Application Insights.
/// </summary>
[Theory]
[InlineData(true, false)]
[InlineData(false, false)]
[InlineData(true, true)]
[InlineData(false, true)]
public async Task TracingAsync(bool useApplicationInsights, bool useStreaming)
{
using var tracerProvider = GetTracerProvider(useApplicationInsights);
using var activity = s_activitySource.StartActivity("MainActivity");
Console.WriteLine($"Operation/Trace ID: {Activity.Current?.TraceId}");
await RunExampleAsync(useStreaming: useStreaming);
// Output:
// Operation/Trace ID: 132d831ef39c13226cdaa79873f375b8
// Activity.TraceId: 132d831ef39c13226cdaa79873f375b8
// Activity.SpanId: 891e8f2f32a61123
// Activity.TraceFlags: Recorded
// Activity.ParentSpanId: 5dae937c9438def9
// Activity.ActivitySourceName: Microsoft.SemanticKernel.Diagnostics
// Activity.DisplayName: chat.completions gpt-4
// Activity.Kind: Client
// Activity.StartTime: 2025-02-03T23:32:57.1363560Z
// Activity.Duration: 00:00:02.1339320
// and more...
}
#region private
private async Task RunExampleAsync(
bool useStreaming = false,
ILoggerFactory? loggerFactory = null)
{
// Define the agents
ChatCompletionAgent agentReviewer =
new()
{
Name = "ArtDirector",
Instructions =
"""
You are an art director who has opinions about copywriting born of a love for David Ogilvy.
The goal is to determine if the given copy is acceptable to print.
If so, state that it is approved.
If not, provide insight on how to refine suggested copy without examples.
""",
Description = "An art director who has opinions about copywriting born of a love for David Ogilvy",
Kernel = this.CreateKernelWithChatCompletion(),
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory),
};
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
name: "CopyWriter",
instructions:
"""
You are a copywriter with ten years of experience and are known for brevity and a dry humor.
The goal is to refine and decide on the single best copy as an expert in the field.
Only provide a single proposal per response.
You're laser focused on the goal at hand.
Don't waste time with chit chat.
Consider suggestions when refining an idea.
""",
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agentWriter = new(assistant, this.AssistantClient)
{
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory)
};
// Create a chat for agent interaction.
AgentGroupChat chat =
new(agentWriter, agentReviewer)
{
// This is all that is required to enable logging across the Agent Framework.
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory),
ExecutionSettings =
new()
{
// Here a TerminationStrategy subclass is used that will terminate when
// an assistant message contains the term "approve".
TerminationStrategy =
new ApprovalTerminationStrategy()
{
// Only the art-director may approve.
Agents = [agentReviewer],
// Limit total number of turns
MaximumIterations = 10,
}
}
};
// Invoke chat and display messages.
ChatMessageContent input = new(AuthorRole.User, "concept: maps made out of egg cartons.");
chat.AddChatMessage(input);
this.WriteAgentChatMessage(input);
if (useStreaming)
{
string lastAgent = string.Empty;
await foreach (StreamingChatMessageContent response in chat.InvokeStreamingAsync())
{
if (string.IsNullOrEmpty(response.Content))
{
continue;
}
if (!lastAgent.Equals(response.AuthorName, StringComparison.Ordinal))
{
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:");
lastAgent = response.AuthorName ?? string.Empty;
}
Console.WriteLine($"\t > streamed: '{response.Content}'");
}
// Display the chat history.
Console.WriteLine("================================");
Console.WriteLine("CHAT HISTORY");
Console.WriteLine("================================");
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync();
for (int index = 0; index < history.Length; index++)
{
this.WriteAgentChatMessage(history[index]);
}
}
else
{
await foreach (ChatMessageContent response in chat.InvokeAsync())
{
this.WriteAgentChatMessage(response);
}
}
Console.WriteLine($"\n[IS COMPLETED: {chat.IsComplete}]");
}
private TracerProvider? GetTracerProvider(bool useApplicationInsights)
{
// Enable diagnostics.
AppContext.SetSwitch("Microsoft.SemanticKernel.Experimental.GenAI.EnableOTelDiagnostics", true);
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Semantic Kernel Agents Tracing Example"))
.AddSource("Microsoft.SemanticKernel*")
.AddSource(s_activitySource.Name);
if (useApplicationInsights)
{
var connectionString = TestConfiguration.ApplicationInsights.ConnectionString;
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new ConfigurationNotFoundException(
nameof(TestConfiguration.ApplicationInsights),
nameof(TestConfiguration.ApplicationInsights.ConnectionString));
}
tracerProviderBuilder.AddAzureMonitorTraceExporter(o => o.ConnectionString = connectionString);
}
else
{
tracerProviderBuilder.AddConsoleExporter();
}
return tracerProviderBuilder.Build();
}
private ILoggerFactory GetLoggerFactoryOrDefault(ILoggerFactory? loggerFactory = null) => loggerFactory ?? NullLoggerFactory.Instance;
private sealed class ApprovalTerminationStrategy : TerminationStrategy
{
// Terminate when the final message contains the term "approve"
protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken)
=> Task.FromResult(history[history.Count - 1].Content?.Contains("approve", StringComparison.OrdinalIgnoreCase) ?? false);
}
#endregion
}