Skip to content

.Net: Update ChatCompletion, AzureAI and Assistant samples to new invocation and thread pattern. #11137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Projects;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Resources;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand Down Expand Up @@ -35,7 +35,7 @@ public async Task UseTemplateForAzureAgentAsync()
};

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

try
{
Expand All @@ -52,14 +52,14 @@ await InvokeAgentAsync(
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}

// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id, arguments))
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Projects;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
using Plugins;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand All @@ -24,19 +24,19 @@ public async Task UseAzureAgentWithPluginAsync()
name: "Host");

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
{
await InvokeAgentAsync(agent, thread.Id, "Hello");
await InvokeAgentAsync(agent, thread.Id, "What is the special soup and its price?");
await InvokeAgentAsync(agent, thread.Id, "What is the special drink and its price?");
await InvokeAgentAsync(agent, thread.Id, "Thank you");
await InvokeAgentAsync(agent, thread, "Hello");
await InvokeAgentAsync(agent, thread, "What is the special soup and its price?");
await InvokeAgentAsync(agent, thread, "What is the special drink and its price?");
await InvokeAgentAsync(agent, thread, "Thank you");
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}
}
Expand All @@ -48,16 +48,16 @@ public async Task UseAzureAgentWithPluginEnumParameterAsync()
AzureAIAgent agent = await CreateAzureAgentAsync(plugin: KernelPluginFactory.CreateFromType<WidgetFactory>());

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
{
await InvokeAgentAsync(agent, thread.Id, "Create a beautiful red colored widget for me.");
await InvokeAgentAsync(agent, thread, "Create a beautiful red colored widget for me.");
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}
}
Expand All @@ -83,13 +83,12 @@ private async Task<AzureAIAgent> CreateAzureAgentAsync(KernelPlugin plugin, stri
}

// Local function to invoke agent and display the conversation messages.
private async Task InvokeAgentAsync(AzureAIAgent agent, string threadId, string input)
private async Task InvokeAgentAsync(AzureAIAgent agent, AgentThread thread, string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(threadId, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand All @@ -22,7 +23,7 @@ public async Task UseCodeInterpreterToolWithAgentAsync()
AzureAIAgent agent = new(definition, this.AgentsClient);

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
Expand All @@ -31,18 +32,17 @@ public async Task UseCodeInterpreterToolWithAgentAsync()
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}

// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(thread.Id, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand Down Expand Up @@ -38,7 +39,7 @@ await this.AgentsClient.CreateVectorStoreAsync(
AzureAIAgent agent = new(agentModel, this.AgentsClient);

// Create a thread associated for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
Expand All @@ -49,7 +50,7 @@ await this.AgentsClient.CreateVectorStoreAsync(
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
await this.AgentsClient.DeleteVectorStoreAsync(fileStore.Id);
await this.AgentsClient.DeleteFileAsync(fileInfo.Id);
Expand All @@ -59,10 +60,9 @@ await this.AgentsClient.CreateVectorStoreAsync(
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(thread.Id, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand Down Expand Up @@ -35,7 +36,7 @@ public async Task UseOpenAPIToolWithAgentAsync()
AzureAIAgent agent = new(definition, this.AgentsClient);

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
Expand All @@ -45,18 +46,17 @@ public async Task UseOpenAPIToolWithAgentAsync()
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}

// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(thread.Id, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.SemanticKernel.ChatCompletion;
using Plugins;
using Agent = Azure.AI.Projects.Agent;
using AgentThread = Microsoft.SemanticKernel.Agents.AgentThread;

namespace GettingStarted.AzureAgents;

Expand Down Expand Up @@ -40,7 +41,7 @@ public async Task UseSingleAgentWithFunctionToolsAsync()
agent.Kernel.Plugins.Add(plugin);

// Create a thread for the agent conversation.
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new AzureAIAgentThread(this.AgentsClient, metadata: SampleMetadata);

// Respond to user input
try
Expand All @@ -52,18 +53,17 @@ public async Task UseSingleAgentWithFunctionToolsAsync()
}
finally
{
await this.AgentsClient.DeleteThreadAsync(thread.Id);
await thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
}

// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(thread.Id, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(thread.Id))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using OpenAI.Assistants;
using Resources;
Expand Down Expand Up @@ -33,7 +34,7 @@ public async Task UseTemplateForAssistantAgentAsync()
};

// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient, metadata: SampleMetadata);

try
{
Expand All @@ -50,14 +51,14 @@ await InvokeAgentAsync(
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}

// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
await foreach (ChatMessageContent response in agent.InvokeAsync(threadId, arguments))
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, options: new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
Expand All @@ -23,19 +24,19 @@ public async Task UseAssistantWithPluginAsync()
name: "Host");

// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient);

// Respond to user input
try
{
await InvokeAgentAsync(agent, threadId, "Hello");
await InvokeAgentAsync(agent, threadId, "What is the special soup and its price?");
await InvokeAgentAsync(agent, threadId, "What is the special drink and its price?");
await InvokeAgentAsync(agent, threadId, "Thank you");
await InvokeAgentAsync(agent, thread, "Hello");
await InvokeAgentAsync(agent, thread, "What is the special soup and its price?");
await InvokeAgentAsync(agent, thread, "What is the special drink and its price?");
await InvokeAgentAsync(agent, thread, "Thank you");
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
Expand All @@ -47,16 +48,16 @@ public async Task UseAssistantWithPluginEnumParameterAsync()
OpenAIAssistantAgent agent = await CreateAssistantAgentAsync(plugin: KernelPluginFactory.CreateFromType<WidgetFactory>());

// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient);

// Respond to user input
try
{
await InvokeAgentAsync(agent, threadId, "Create a beautiful red colored widget for me.");
await InvokeAgentAsync(agent, thread, "Create a beautiful red colored widget for me.");
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
Expand All @@ -78,13 +79,12 @@ await this.AssistantClient.CreateAssistantAsync(
}

// Local function to invoke agent and display the conversation messages.
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId, string input)
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, AgentThread thread, string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(threadId, message);
this.WriteAgentChatMessage(message);

await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
Expand Down
Loading
Loading