mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Basic agent invocation sample (#175)
* Basic agent invocation sample * Simplify sample further. * Add additional comment. * Address code review comments. * Suppress CA2000 and remove using from most basic sample, since the IChatClient instances involved do not do any disposal.
This commit is contained in:
committed by
GitHub
Unverified
parent
407ed6de70
commit
55045b5c1e
@@ -4,4 +4,5 @@ dotnet_diagnostic.CA2007.severity = none # Do not directly await a Task
|
||||
dotnet_diagnostic.CS1591.severity = none # Missing XML comment for publicly visible type or member
|
||||
dotnet_diagnostic.IDE1006.severity = warning # Naming rule violations
|
||||
dotnet_diagnostic.VSTHRD111.severity = none # Use .ConfigureAwait(bool) is hidden by default, set to none to prevent IDE from changing on autosave
|
||||
dotnet_diagnostic.CA1716.severity = none # Add summary to documentation comment.
|
||||
dotnet_diagnostic.CA1716.severity = none # Add summary to documentation comment.
|
||||
dotnet_diagnostic.CA2000.severity = none # Call System.IDisposable.Dispose on object before all references to it are out of scope
|
||||
|
||||
@@ -6,6 +6,7 @@ using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
using Microsoft.Shared.Samples;
|
||||
using OpenAI.Assistants;
|
||||
using OpenAI.Chat;
|
||||
@@ -31,13 +32,13 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
|
||||
AzureAIAgentsPersistent
|
||||
}
|
||||
|
||||
protected IChatClient GetChatClient(ChatClientProviders provider, ChatClientAgentOptions options)
|
||||
protected IChatClient GetChatClient(ChatClientProviders provider, ChatClientAgentOptions? options = null)
|
||||
=> provider switch
|
||||
{
|
||||
ChatClientProviders.OpenAIChatCompletion => GetOpenAIChatClient(),
|
||||
ChatClientProviders.OpenAIAssistant => GetOpenAIAssistantChatClient(options),
|
||||
ChatClientProviders.OpenAIAssistant => GetOpenAIAssistantChatClient(Throw.IfNull(options)),
|
||||
ChatClientProviders.AzureOpenAI => GetAzureOpenAIChatClient(),
|
||||
ChatClientProviders.AzureAIAgentsPersistent => GetAzureAIAgentPersistentClient(options),
|
||||
ChatClientProviders.AzureAIAgentsPersistent => GetAzureAIAgentPersistentClient(Throw.IfNull(options)),
|
||||
ChatClientProviders.OpenAIResponses or
|
||||
ChatClientProviders.OpenAIResponses_InMemoryMessageThread or
|
||||
ChatClientProviders.OpenAIResponses_ConversationIdThread
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.AI.Agents;
|
||||
|
||||
namespace Steps;
|
||||
@@ -15,6 +16,33 @@ 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.";
|
||||
|
||||
/// <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,
|
||||
/// and we are invoking with text input, and getting back a text response.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(ChatClientProviders.AzureOpenAI)]
|
||||
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
|
||||
[InlineData(ChatClientProviders.OpenAIResponses)]
|
||||
public async Task RunBasic(ChatClientProviders provider)
|
||||
{
|
||||
// Get the chat client to communicate with the inference service backing our agent.
|
||||
// Any implementation of Microsoft.Extensions.AI.Agents.IChatClient can be used with the ChatClientAgent.
|
||||
// See the Providers folder for examples on how to create chat clients for some sample providers.
|
||||
IChatClient chatClient = base.GetChatClient(provider);
|
||||
|
||||
// Define the agent
|
||||
Agent agent = new ChatClientAgent(chatClient, options: new()
|
||||
{
|
||||
Name = ParrotName,
|
||||
Instructions = ParrotInstructions,
|
||||
});
|
||||
|
||||
// Invoke the agent and output the text result.
|
||||
Console.WriteLine(await agent.RunAsync("Fortune favors the bold."));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Demonstrate the usage of <see cref="ChatClientAgent"/> where each invocation is
|
||||
/// a unique interaction with no conversation history between them.
|
||||
|
||||
Reference in New Issue
Block a user