Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/GettingStarted.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public async Task RunWithChatCompletion()
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ public async Task RunWithChatCompletion()
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
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>
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public async Task RunBasic(ChatClientProviders provider)
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."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -65,7 +86,7 @@ async Task RunAgentAsync(string input)
[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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private async Task<string> UploadFileAsync(string filePath, ChatClientProviders
/// <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
Expand All @@ -114,7 +113,6 @@ private async Task<string> UploadFileAsync(string filePath, ChatClientProviders
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,35 @@ public sealed class ChatClientAgent : Agent
/// Initializes a new instance of the <see cref="ChatClientAgent"/> class.
/// </summary>
/// <param name="chatClient">The chat client to use for invoking the agent.</param>
/// <param name="options">Optional agent options to configure the agent.</param>
/// <param name="instructions">Optional instructions for the agent.</param>
/// <param name="name">Optional name for the agent.</param>
/// <param name="description">Optional description for the agent.</param>
/// <param name="tools">Optional list of tools that the agent can use during invocation.</param>
/// <param name="loggerFactory">Optional logger factory to use for logging.</param>
public ChatClientAgent(IChatClient chatClient, ChatClientAgentOptions? options = null, ILoggerFactory? loggerFactory = null)
public ChatClientAgent(IChatClient chatClient, string? instructions = null, string? name = null, string? description = null, IList<AITool>? tools = null, ILoggerFactory? loggerFactory = null)
: this(
chatClient,
new ChatClientAgentOptions()
{
Name = name,
Description = description,
Instructions = instructions,
ChatOptions = tools is null ? null : new ChatOptions()
{
Tools = tools,
}
},
loggerFactory)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ChatClientAgent"/> class.
/// </summary>
/// <param name="chatClient">The chat client to use for invoking the agent.</param>
/// <param name="options">Full set of options to configure the agent.</param>
/// <param name="loggerFactory">Optional logger factory to use for logging.</param>
public ChatClientAgent(IChatClient chatClient, ChatClientAgentOptions options, ILoggerFactory? loggerFactory = null)
{
Throw.IfNull(chatClient);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task<ChatClientAgent> CreateChatClientAgentAsync(

return new ChatClientAgent(
this._persistentAgentsClient.AsIChatClient(persistentAgent.Id),
new()
options: new()
{
Id = persistentAgent.Id,
ChatOptions = new() { Tools = aiTools }
Expand Down
Loading
Loading