mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Switch minimal console and a few samples to use latest patterns (#348)
* Switch minimal console and a few samples to use latest patterns * Use interpolation for sample output.
This commit is contained in:
committed by
GitHub
Unverified
parent
ff3e13c2aa
commit
ef16774878
@@ -13,6 +13,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.Extensions.AI.Agents.OpenAI\Microsoft.Extensions.AI.Agents.OpenAI.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.Extensions.AI.Agents\Microsoft.Extensions.AI.Agents.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -6,22 +6,18 @@ using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using OpenAI;
|
||||
|
||||
[Description("Get the weather for a given location.")]
|
||||
static string GetWeather([Description("The location to get the weather for.")] string location)
|
||||
{
|
||||
return $"The weather in {location} is cloudy with a high of 15°C.";
|
||||
}
|
||||
=> $"The weather in {location} is cloudy with a high of 15°C.";
|
||||
|
||||
IChatClient chatClient = new AzureOpenAIClient(
|
||||
AIAgent agent = new AzureOpenAIClient(
|
||||
new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!),
|
||||
new AzureCliCredential())
|
||||
.GetChatClient("gpt-4o-mini")
|
||||
.AsIChatClient();
|
||||
|
||||
AIAgent agent = new ChatClientAgent(
|
||||
chatClient,
|
||||
instructions: "You are a helpful assistant, you can help the user with weather information.",
|
||||
tools: [AIFunctionFactory.Create(GetWeather)]);
|
||||
.CreateAIAgent(
|
||||
instructions: "You are a helpful assistant, you can help the user with weather information.",
|
||||
tools: [AIFunctionFactory.Create(GetWeather)]);
|
||||
|
||||
Console.WriteLine(await agent.RunAsync("What's the weather in Amsterdam?"));
|
||||
|
||||
@@ -43,11 +43,13 @@ public sealed class AIAgent_With_AzureAIAgentsPersistent(ITestOutputHelper outpu
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response);
|
||||
""");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
@@ -77,11 +79,13 @@ public sealed class AIAgent_With_AzureAIAgentsPersistent(ITestOutputHelper outpu
|
||||
// Local function to run agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response);
|
||||
""");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
|
||||
@@ -39,11 +39,13 @@ public sealed class AIAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper out
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +43,13 @@ public sealed class AIAgent_With_OpenAIAssistant(ITestOutputHelper output) : Age
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
""");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
|
||||
@@ -33,11 +33,13 @@ public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentS
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
Console.WriteLine(response.Messages.Last().Text);
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,12 +61,17 @@ public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentS
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
Console.WriteLine($"User: {input}");
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
var chatCompletion = response.AsChatCompletion();
|
||||
|
||||
Console.WriteLine(chatCompletion.Content.Last().Text);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Assistant:
|
||||
{chatCompletion.Content.Last().Text}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,13 +93,18 @@ public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentS
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
Console.WriteLine(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(chatCompletion.Content.Last().Text);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
Assistant:
|
||||
{chatCompletion.Content.Last().Text}
|
||||
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +37,13 @@ public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output)
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,11 +79,13 @@ public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output)
|
||||
// Local function to invoke agent and display the conversation messages for the thread.
|
||||
async Task RunAgentAsync(string input)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
Console.WriteLine(
|
||||
$"""
|
||||
User: {input}
|
||||
Assistant:
|
||||
{await agent.RunAsync(input, thread)}
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
|
||||
private const string ParrotName = "Parrot";
|
||||
private const string ParrotInstructions = "Repeat the user message in the voice of a pirate and then end with a parrot sound.";
|
||||
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
/// <summary>
|
||||
/// Demonstrate the most basic Agent case, where we do not have a server-side agent
|
||||
/// but just an in-memory agent, backed by an inference service,
|
||||
@@ -33,7 +36,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
|
||||
IChatClient chatClient = base.GetChatClient(provider);
|
||||
|
||||
// Define the agent
|
||||
AIAgent agent = new ChatClientAgent(chatClient, name: ParrotName, instructions: ParrotInstructions);
|
||||
AIAgent agent = new ChatClientAgent(chatClient, ParrotInstructions);
|
||||
|
||||
// Invoke the agent and output the text result.
|
||||
Console.WriteLine(await agent.RunAsync("Fortune favors the bold."));
|
||||
@@ -95,8 +98,8 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
|
||||
// Define the options for the chat client agent.
|
||||
var agentOptions = new ChatClientAgentOptions
|
||||
{
|
||||
Name = ParrotName,
|
||||
Instructions = ParrotInstructions,
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
|
||||
// Get chat options based on the store type, if needed.
|
||||
ChatOptions = base.GetChatOptions(provider),
|
||||
@@ -145,7 +148,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
|
||||
public async Task RunStreamingWithThread(ChatClientProviders provider)
|
||||
{
|
||||
// Define the options for the chat client agent.
|
||||
var agentOptions = new ChatClientAgentOptions(name: ParrotName, instructions: ParrotInstructions)
|
||||
var agentOptions = new ChatClientAgentOptions(name: JokerName, instructions: JokerInstructions)
|
||||
{
|
||||
// Get chat options based on the store type, if needed.
|
||||
ChatOptions = base.GetChatOptions(provider),
|
||||
|
||||
@@ -9,6 +9,9 @@ namespace Steps;
|
||||
|
||||
public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Theory]
|
||||
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
|
||||
[InlineData(ChatClientProviders.AzureOpenAI)]
|
||||
@@ -20,9 +23,7 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper
|
||||
// Adding multiple chat clients to the service collection.
|
||||
var services = new ServiceCollection();
|
||||
|
||||
var agentOptions = new ChatClientAgentOptions(
|
||||
name: "Parrot",
|
||||
instructions: "Repeat the user message in the voice of a pirate and then end with a parrot sound.");
|
||||
var agentOptions = new ChatClientAgentOptions(JokerInstructions, JokerName);
|
||||
|
||||
services.AddLogging();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user