mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Add samples to show how to create an AIAgent for each provider. (#525)
* Add samples to show how to create an AIAgent for each provider. * Update dotnet/samples/HowToCreateAnAIAgentByProvider/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
bbea3c00f8
commit
fe1941d25f
@@ -1,95 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Azure.AI.Agents.Persistent;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Shows how to use <see cref="AIAgent"/> with Azure AI Persistent Agents.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Running "az login" command in terminal is required for authentication with Azure AI service.
|
||||
/// </remarks>
|
||||
public sealed class AIAgent_With_AzureAIAgentsPersistent(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task GetWithAzureAIAgentsPersistent()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a service side persistent agent.
|
||||
var persistentAgent = await persistentAgentsClient.Administration.CreateAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName!,
|
||||
name: JokerName,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Get a server side agent.
|
||||
AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(persistentAgent.Value.Id);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Threads.DeleteThreadAsync(thread.ConversationId);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateWithAzureAIAgentsPersistent()
|
||||
{
|
||||
// Get a client to create server side agents with.
|
||||
var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential());
|
||||
|
||||
// Create a server side persistent agent.
|
||||
AIAgent agent = await persistentAgentsClient.CreateAIAgentAsync(
|
||||
model: TestConfiguration.AzureAI.DeploymentName!,
|
||||
name: JokerName,
|
||||
instructions: JokerInstructions);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
await persistentAgentsClient.Threads.DeleteThreadAsync(thread.ConversationId);
|
||||
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.ClientModel;
|
||||
using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with Azure OpenAI Chat Completion.
|
||||
/// </summary>
|
||||
public sealed class AIAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletion()
|
||||
{
|
||||
// Get the OpenAI client to use for the agent.
|
||||
var openAIClient = (TestConfiguration.AzureOpenAI.ApiKey is null)
|
||||
// Use Azure CLI credentials if API key is not provided.
|
||||
? new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new AzureCliCredential())
|
||||
: new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey));
|
||||
|
||||
// Create the agent
|
||||
AIAgent agent = openAIClient.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName).CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="AIAgent"/> with OpenAI Chat Completion and Responses.
|
||||
/// </summary>
|
||||
public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletion()
|
||||
{
|
||||
// Create the agent using the OpenAI ChatClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionReturnChatCompletion()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine($"User: {input}");
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
var chatCompletion = response.AsChatCompletion();
|
||||
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Assistant:
|
||||
{chatCompletion.Content.Last().Text}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionWithOpenAIChatMessage()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine($"User: {input}");
|
||||
|
||||
// Use the OpenAI.Chat message types directly
|
||||
var chatMessage = new UserChatMessage(input);
|
||||
var chatCompletion = await agent.RunAsync(chatMessage, thread);
|
||||
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Assistant:
|
||||
{chatCompletion.Content.Last().Text}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
|
||||
/// </summary>
|
||||
public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
/// <summary>
|
||||
/// This will use the conversation id to reference the thread state on the server side.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithResponses()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation based on the type.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will use in-memory messages to store the thread state.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithResponsesAndStoreOutputDisabled()
|
||||
{
|
||||
// Get the agent directly from OpenAIClient.
|
||||
AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.CreateAIAgent(options: new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
ChatOptions = new ChatOptions().WithResponseStoredOutputDisabled()
|
||||
});
|
||||
|
||||
// Start a new thread for the agent conversation based on the type.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
// Respond to user input.
|
||||
await RunAgentAsync("Tell me a joke about a pirate.");
|
||||
await RunAgentAsync("Now add some emojis to the joke.");
|
||||
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user