Skip to content

Commit

Permalink
Merge branch 'main' into agents-graduation
Browse files Browse the repository at this point in the history
  • Loading branch information
crickman authored Feb 21, 2025
2 parents 39bf092 + ef56875 commit 26722ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
30 changes: 18 additions & 12 deletions dotnet/samples/GettingStartedWithAgents/Step07_Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
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;
Expand All @@ -17,15 +19,15 @@ namespace GettingStarted;
/// <summary>
/// A repeat of <see cref="Step03_Chat"/> with telemetry enabled.
/// </summary>
public class Step07_Telemetry(ITestOutputHelper output) : BaseAgentsTest(output)
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"/> and <see cref="AgentGroupChat"/>.
/// 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>
Expand All @@ -44,7 +46,7 @@ public async Task LoggingAsync()
}

/// <summary>
/// Demonstrates tracing in <see cref="ChatCompletionAgent"/>.
/// 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>
Expand Down Expand Up @@ -99,11 +101,12 @@ private async Task RunExampleAsync(
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory),
};

ChatCompletionAgent agentWriter =
new()
{
Name = "CopyWriter",
Instructions =
// 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.
Expand All @@ -112,10 +115,13 @@ Only provide a single proposal per response.
Don't waste time with chit chat.
Consider suggestions when refining an idea.
""",
Description = "A copywriter with ten years of experience and are known for brevity and a dry humor.",
Kernel = this.CreateKernelWithChatCompletion(),
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory),
};
metadata: SampleMetadata);

// Create the agent
OpenAIAssistantAgent agentWriter = new(assistant, this.AssistantClient)
{
LoggerFactory = GetLoggerFactoryOrDefault(loggerFactory)
};

// Create a chat for agent interaction.
AgentGroupChat chat =
Expand Down
12 changes: 10 additions & 2 deletions dotnet/src/Agents/AzureAI/AzureAIChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Threading.Tasks;
using Azure.AI.Projects;
using Microsoft.SemanticKernel.Agents.AzureAI.Internal;
using Microsoft.SemanticKernel.Agents.Extensions;
using Microsoft.SemanticKernel.Diagnostics;

namespace Microsoft.SemanticKernel.Agents.AzureAI;

Expand All @@ -27,13 +29,19 @@ protected override async Task ReceiveAsync(IEnumerable<ChatMessageContent> histo
AzureAIAgent agent,
CancellationToken cancellationToken)
{
return AgentThreadActions.InvokeAsync(agent, client, threadId, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken);
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description),
() => AgentThreadActions.InvokeAsync(agent, client, threadId, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),
cancellationToken);
}

/// <inheritdoc/>
protected override IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(AzureAIAgent agent, IList<ChatMessageContent> messages, CancellationToken cancellationToken = default)
{
return AgentThreadActions.InvokeStreamingAsync(agent, client, threadId, messages, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken);
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description),
() => AgentThreadActions.InvokeStreamingAsync(agent, client, threadId, messages, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),
cancellationToken);
}

/// <inheritdoc/>
Expand Down
12 changes: 10 additions & 2 deletions dotnet/src/Agents/OpenAI/OpenAIAssistantChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Agents.Extensions;
using Microsoft.SemanticKernel.Agents.OpenAI.Internal;
using Microsoft.SemanticKernel.Diagnostics;
using OpenAI.Assistants;

namespace Microsoft.SemanticKernel.Agents.OpenAI;
Expand Down Expand Up @@ -32,13 +34,19 @@ protected override async Task ReceiveAsync(IEnumerable<ChatMessageContent> histo
OpenAIAssistantAgent agent,
CancellationToken cancellationToken)
{
return AssistantThreadActions.InvokeAsync(agent, this._client, this._threadId, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken);
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description),
() => AssistantThreadActions.InvokeAsync(agent, this._client, this._threadId, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),
cancellationToken);
}

/// <inheritdoc/>
protected override IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(OpenAIAssistantAgent agent, IList<ChatMessageContent> messages, CancellationToken cancellationToken = default)
{
return AssistantThreadActions.InvokeStreamingAsync(agent, this._client, this._threadId, messages, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken);
return ActivityExtensions.RunWithActivityAsync(
() => ModelDiagnostics.StartAgentInvocationActivity(agent.Id, agent.GetDisplayName(), agent.Description),
() => AssistantThreadActions.InvokeStreamingAsync(agent, this._client, this._threadId, messages, invocationOptions: null, this.Logger, agent.Kernel, agent.Arguments, cancellationToken),
cancellationToken);
}

/// <inheritdoc/>
Expand Down

0 comments on commit 26722ad

Please sign in to comment.