From 4d3b17eb273fb96d455202fd9aa464ff476cf478 Mon Sep 17 00:00:00 2001 From: westey <164392973+westey-m@users.noreply.github.com> Date: Fri, 13 Jun 2025 17:11:56 +0100 Subject: [PATCH] Add sample for OpenAIAssistant ChatClientAgent (#74) * Add sample for OpenAIAssistant * Fix warning * Add tools sample and simplify running samples. * Restructure samples to show common features separate from each type of underlying IChatClient implementation. * Remove unecessary suppression. * Renaming namespaces based on suggestion from PR. --- .../ChatClientAgent_With_OpenAIAssistant.cs | 65 +++++++++++++++++++ ...atClientAgent_With_OpenAIChatCompletion.cs | 51 +++++++++++++++ .../Step01_Running.cs | 2 +- .../Step02_UsingTools.cs | 2 +- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIAssistant.cs create mode 100644 dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIChatCompletion.cs rename dotnet/samples/GettingStarted/{ChatClientAgent => Steps}/Step01_Running.cs (99%) rename dotnet/samples/GettingStarted/{ChatClientAgent => Steps}/Step02_UsingTools.cs (99%) diff --git a/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIAssistant.cs b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIAssistant.cs new file mode 100644 index 0000000000..148c71b605 --- /dev/null +++ b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIAssistant.cs @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.Agents; +using Microsoft.Extensions.AI; +using Microsoft.Shared.Samples; +using OpenAI; + +#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. + +namespace Providers; + +/// +/// Shows how to use with Open AI Assistants. +/// +public sealed class ChatClientAgent_With_OpenAIAssistant(ITestOutputHelper output) : AgentSample(output) +{ + private const string JokerName = "Joker"; + private const string JokerInstructions = "You are good at telling jokes."; + + [Fact] + public async Task RunWithOpenAIAssistant() + { + // Get a client to create server side agents with. + var openAIClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey); + var assistantClient = openAIClient.GetAssistantClient(); + + // Create a server side agent to work with. + var assistantCreateResult = await assistantClient.CreateAssistantAsync( + TestConfiguration.OpenAI.ChatModelId, + new() + { + Name = JokerName, + Instructions = JokerInstructions + }); + + var assistantId = assistantCreateResult.Value.Id; + + // Get the chat client to use for the agent. + using var chatClient = assistantClient.AsIChatClient(assistantId); + + // Define the agent + ChatClientAgent agent = new(chatClient); + + // Start a new thread for the agent conversation. + AgentThread thread = agent.GetNewThread(); + + // Respond to user input + await InvokeAgentAsync("Tell me a joke about a pirate."); + await InvokeAgentAsync("Now add some emojis to the joke."); + + // Local function to invoke agent and display the conversation messages for the thread. + async Task InvokeAgentAsync(string input) + { + this.WriteUserMessage(input); + + var response = await agent.RunAsync(input, thread); + + this.WriteResponseOutput(response); + } + + // Cleanup + await assistantClient.DeleteThreadAsync(thread.Id); + await assistantClient.DeleteAssistantAsync(assistantId); + } +} diff --git a/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIChatCompletion.cs b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIChatCompletion.cs new file mode 100644 index 0000000000..b2ba8c6982 --- /dev/null +++ b/dotnet/samples/GettingStarted/Providers/ChatClientAgent_With_OpenAIChatCompletion.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.Agents; +using Microsoft.Extensions.AI; +using Microsoft.Shared.Samples; +using OpenAI; + +namespace Providers; + +/// +/// Shows how to use with Open AI Chat Completion. +/// +public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output) +{ + private const string JokerName = "Joker"; + private const string JokerInstructions = "You are good at telling jokes."; + + [Fact] + public async Task RunWithOpenAIAssistant() + { + // Get the chat client to use for the agent. + using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) + .GetChatClient(TestConfiguration.OpenAI.ChatModelId) + .AsIChatClient(); + + // Define the agent + ChatClientAgent agent = + new(chatClient, new() + { + Name = JokerName, + Instructions = JokerInstructions, + }); + + // Start a new thread for the agent conversation. + AgentThread thread = agent.GetNewThread(); + + // Respond to user input + await InvokeAgentAsync("Tell me a joke about a pirate."); + await InvokeAgentAsync("Now add some emojis to the joke."); + + // Local function to invoke agent and display the conversation messages for the thread. + async Task InvokeAgentAsync(string input) + { + this.WriteUserMessage(input); + + var response = await agent.RunAsync(input, thread); + + this.WriteResponseOutput(response); + } + } +} diff --git a/dotnet/samples/GettingStarted/ChatClientAgent/Step01_Running.cs b/dotnet/samples/GettingStarted/Steps/Step01_Running.cs similarity index 99% rename from dotnet/samples/GettingStarted/ChatClientAgent/Step01_Running.cs rename to dotnet/samples/GettingStarted/Steps/Step01_Running.cs index 29df4658cf..abc27459db 100644 --- a/dotnet/samples/GettingStarted/ChatClientAgent/Step01_Running.cs +++ b/dotnet/samples/GettingStarted/Steps/Step01_Running.cs @@ -2,7 +2,7 @@ using Microsoft.Agents; -namespace ChatCompletionAgent; +namespace Steps; /// /// Provides test methods to demonstrate the usage of chat agents with different interaction models. diff --git a/dotnet/samples/GettingStarted/ChatClientAgent/Step02_UsingTools.cs b/dotnet/samples/GettingStarted/Steps/Step02_UsingTools.cs similarity index 99% rename from dotnet/samples/GettingStarted/ChatClientAgent/Step02_UsingTools.cs rename to dotnet/samples/GettingStarted/Steps/Step02_UsingTools.cs index 56dc8e3d55..861b409349 100644 --- a/dotnet/samples/GettingStarted/ChatClientAgent/Step02_UsingTools.cs +++ b/dotnet/samples/GettingStarted/Steps/Step02_UsingTools.cs @@ -4,7 +4,7 @@ using System.ComponentModel; using Microsoft.Agents; using Microsoft.Extensions.AI; -namespace ChatCompletionAgent; +namespace Steps; public sealed class Step02_UsingTools(ITestOutputHelper output) : AgentSample(output) {