mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Add ChatClientAgent constructor overload (#188)
* Add ChatClientAgent constructor overload * Add unit tests for new constructor * Up code coverage with extra tests * Address PR comments. * Address PR comment * Add additional test to increase code coverage.
This commit is contained in:
committed by
GitHub
Unverified
parent
e6d611abe4
commit
95a3264c8b
@@ -9,7 +9,7 @@
|
||||
<RootNamespace>GettingStarted</RootNamespace>
|
||||
<OutputType>Library</OutputType>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
<NoWarn>$(NoWarn);CA1707;CA1716;IDE0009;IDE1006;</NoWarn>
|
||||
<NoWarn>$(NoWarn);CA1707;CA1716;IDE0009;IDE1006;OPENAI001;</NoWarn>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InjectSharedSamples>true</InjectSharedSamples>
|
||||
</PropertyGroup>
|
||||
|
||||
+1
-6
@@ -29,12 +29,7 @@ public sealed class ChatClientAgent_With_AzureOpenAIChatCompletion(ITestOutputHe
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent =
|
||||
new(chatClient, new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
});
|
||||
ChatClientAgent agent = new(chatClient, JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
+1
-6
@@ -24,12 +24,7 @@ public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent =
|
||||
new(chatClient, new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
});
|
||||
ChatClientAgent agent = new(chatClient, JokerInstructions, JokerName);
|
||||
|
||||
// Start a new thread for the agent conversation.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
+42
-10
@@ -6,8 +6,6 @@ using Microsoft.Shared.Samples;
|
||||
using OpenAI;
|
||||
using OpenAI.Responses;
|
||||
|
||||
#pragma warning disable CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value.
|
||||
|
||||
namespace Providers;
|
||||
|
||||
/// <summary>
|
||||
@@ -18,29 +16,63 @@ public sealed class ChatClientAgent_With_OpenAIResponsesChatCompletion(ITestOutp
|
||||
private const string JokerName = "Joker";
|
||||
private const string JokerInstructions = "You are good at telling jokes.";
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)] // This will use in-memory messages to store the thread state.
|
||||
[InlineData(true)] // This will use the conversation id to reference the thread state on the server side.
|
||||
public async Task RunWithChatCompletion(bool useConversationIdThread)
|
||||
/// <summary>
|
||||
/// This will use the conversation id to reference the thread state on the server side.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionServiceManagedThread()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent = new(chatClient, 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)
|
||||
{
|
||||
this.WriteUserMessage(input);
|
||||
|
||||
var response = await agent.RunAsync(input, thread);
|
||||
|
||||
this.WriteResponseOutput(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This will use in-memory messages to store the thread state.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task RunWithChatCompletionInMemoryThread()
|
||||
{
|
||||
// Get the chat client to use for the agent.
|
||||
#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.
|
||||
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
|
||||
.GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent
|
||||
ChatClientAgent agent =
|
||||
new(chatClient, new()
|
||||
new(chatClient, options: new()
|
||||
{
|
||||
Name = JokerName,
|
||||
Instructions = JokerInstructions,
|
||||
ChatOptions = new ChatOptions
|
||||
{
|
||||
RawRepresentationFactory = (_) => new ResponseCreationOptions() { StoredOutputEnabled = useConversationIdThread }
|
||||
// We can use the RawRepresentationFactory to provide Response service specific
|
||||
// options. Here we can indicate that we do not want the service to store the
|
||||
// conversation in a service managed thread.
|
||||
RawRepresentationFactory = (_) => new ResponseCreationOptions() { StoredOutputEnabled = false }
|
||||
}
|
||||
});
|
||||
#pragma warning restore OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
|
||||
|
||||
// Start a new thread for the agent conversation based on the type.
|
||||
AgentThread thread = agent.GetNewThread();
|
||||
|
||||
@@ -33,11 +33,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
|
||||
IChatClient chatClient = base.GetChatClient(provider);
|
||||
|
||||
// Define the agent
|
||||
Agent agent = new ChatClientAgent(chatClient, options: new()
|
||||
{
|
||||
Name = ParrotName,
|
||||
Instructions = ParrotInstructions,
|
||||
});
|
||||
Agent agent = new ChatClientAgent(chatClient, ParrotInstructions, ParrotName);
|
||||
|
||||
// Invoke the agent and output the text result.
|
||||
Console.WriteLine(await agent.RunAsync("Fortune favors the bold."));
|
||||
|
||||
@@ -12,12 +12,33 @@ namespace Steps;
|
||||
/// </summary>
|
||||
public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper output) : AgentSample(output)
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(ChatClientProviders.AzureOpenAI)]
|
||||
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
|
||||
public async Task RunningWithToolsBasic(ChatClientProviders provider)
|
||||
{
|
||||
// Creating a MenuTools instance to be used by the agent.
|
||||
var menuTools = new MenuTools();
|
||||
|
||||
// Get the chat client to use for the agent.
|
||||
var chatClient = base.GetChatClient(provider);
|
||||
|
||||
// Define the agent and add the GetSpecials tool.
|
||||
var agent = new ChatClientAgent(
|
||||
chatClient,
|
||||
instructions: "Answer questions about the menu.",
|
||||
tools: [AIFunctionFactory.Create(menuTools.GetSpecials)]);
|
||||
|
||||
// Respond to user input, invoking functions where appropriate.
|
||||
Console.WriteLine(await agent.RunAsync("What is the special soup and its price?"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ChatClientProviders.AzureOpenAI)]
|
||||
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
|
||||
public async Task RunningWithTools(ChatClientProviders provider)
|
||||
{
|
||||
// Creating a Menu Tools to be used by the agent.
|
||||
// Creating a MenuTools instance to be used by the agent.
|
||||
var menuTools = new MenuTools();
|
||||
|
||||
// Define the options for the chat client agent.
|
||||
@@ -65,7 +86,7 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
|
||||
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
|
||||
public async Task StreamingRunWithTools(ChatClientProviders provider)
|
||||
{
|
||||
// Creating a Menu Tools to be used by the agent.
|
||||
// Creating a MenuTools instance to be used by the agent.
|
||||
var menuTools = new MenuTools();
|
||||
|
||||
// Define the options for the chat client agent.
|
||||
|
||||
-2
@@ -105,7 +105,6 @@ public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutput
|
||||
/// <returns>The code interpreter output as a string.</returns>
|
||||
private static string? GetCodeInterpreterOutput(object rawRepresentation, ChatClientProviders provider)
|
||||
{
|
||||
#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.
|
||||
switch (provider)
|
||||
{
|
||||
case ChatClientProviders.OpenAIAssistant
|
||||
@@ -114,7 +113,6 @@ public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutput
|
||||
string.Empty,
|
||||
stepDetails.CodeInterpreterOutputs.SelectMany(l => l.Logs)
|
||||
)}";
|
||||
#pragma warning restore OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
|
||||
|
||||
case ChatClientProviders.AzureAIAgentsPersistent
|
||||
when rawRepresentation is Azure.AI.Agents.Persistent.RunStepDetailsUpdate stepDetails:
|
||||
|
||||
Reference in New Issue
Block a user