mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
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.
This commit is contained in:
committed by
GitHub
Unverified
parent
d6dc360215
commit
4d3b17eb27
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Shows how to use <see cref="ChatClientAgent"/> with Open AI Assistants.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Shows how to use <see cref="ChatClientAgent"/> with Open AI Chat Completion.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
using Microsoft.Agents;
|
||||
|
||||
namespace ChatCompletionAgent;
|
||||
namespace Steps;
|
||||
|
||||
/// <summary>
|
||||
/// Provides test methods to demonstrate the usage of chat agents with different interaction models.
|
||||
+1
-1
@@ -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)
|
||||
{
|
||||
Reference in New Issue
Block a user