chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using OpenAI.Assistants;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// This example demonstrates using <see cref="OpenAIAssistantAgent"/> with templatized instructions.
/// </summary>
public class Step01_Assistant(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseTemplateForAssistantAgent()
{
// Define the agent
string generateStoryYaml = EmbeddedResource.Read("GenerateStory.yaml");
PromptTemplateConfig templateConfig = KernelFunctionYaml.ToPromptTemplateConfig(generateStoryYaml);
// Instructions, Name and Description properties defined via the PromptTemplateConfig.
Assistant definition = await this.AssistantClient.CreateAssistantFromTemplateAsync(this.Model, templateConfig, metadata: SampleMetadata);
OpenAIAssistantAgent agent = new(
definition,
this.AssistantClient,
templateFactory: new KernelPromptTemplateFactory(),
templateFormat: PromptTemplateConfig.SemanticKernelTemplateFormat)
{
Arguments = new()
{
{ "topic", "Dog" },
{ "length", "3" }
}
};
// Create a thread for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient, metadata: SampleMetadata);
try
{
// Invoke the agent with the default arguments.
await InvokeAgentAsync();
// Invoke the agent with the override arguments.
await InvokeAgentAsync(
new()
{
{ "topic", "Cat" },
{ "length", "3" },
});
}
finally
{
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(thread, options: new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(response);
}
}
}
}
@@ -0,0 +1,92 @@
// 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;
using Plugins;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate creation of <see cref="OpenAIAssistantAgent"/> with a <see cref="KernelPlugin"/>,
/// and then eliciting its response to explicit user messages.
/// </summary>
public class Step02_Assistant_Plugins(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseAssistantWithPlugin()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAgentAsync(
plugin: KernelPluginFactory.CreateFromType<MenuPlugin>(),
instructions: "Answer questions about the menu.",
name: "Host");
// Create a thread for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient);
// Respond to user input
try
{
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 thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
[Fact]
public async Task UseAssistantWithPluginEnumParameter()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAgentAsync(plugin: KernelPluginFactory.CreateFromType<WidgetFactory>());
// Create a thread for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient);
// Respond to user input
try
{
await InvokeAgentAsync(agent, thread, "Create a beautiful red colored widget for me.");
}
finally
{
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
private async Task<OpenAIAssistantAgent> CreateAssistantAgentAsync(KernelPlugin plugin, string? instructions = null, string? name = null)
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
name,
instructions: instructions,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient, [plugin]);
return agent;
}
// Local function to invoke agent and display the conversation messages.
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, AgentThread thread, string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate providing image input to <see cref="OpenAIAssistantAgent"/> .
/// </summary>
public class Step03_Assistant_Vision(ITestOutputHelper output) : BaseAssistantTest(output)
{
/// <summary>
/// Azure currently only supports message of type=text.
/// </summary>
protected override bool ForceOpenAI => true;
[Fact]
public async Task UseImageContentWithAssistant()
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
// Upload an image
await using Stream imageStream = EmbeddedResource.ReadStream("cat.jpg")!;
string fileId = await this.Client.UploadAssistantFileAsync(imageStream, "cat.jpg");
// Create a thread for the agent conversation.
OpenAIAssistantAgentThread thread = new(this.AssistantClient, metadata: SampleMetadata);
// Respond to user input
try
{
// Refer to public image by url
await InvokeAgentAsync(CreateMessageWithImageUrl("Describe this image.", "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/New_york_times_square-terabass.jpg/1200px-New_york_times_square-terabass.jpg"));
await InvokeAgentAsync(CreateMessageWithImageUrl("What are is the main color in this image?", "https://upload.wikimedia.org/wikipedia/commons/5/56/White_shark.jpg"));
// Refer to uploaded image by file-id.
await InvokeAgentAsync(CreateMessageWithImageReference("Is there an animal in this image?", fileId));
}
finally
{
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
await this.Client.DeleteFileAsync(fileId);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(ChatMessageContent message)
{
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
private ChatMessageContent CreateMessageWithImageUrl(string input, string url)
=> new(AuthorRole.User, [new TextContent(input), new ImageContent(new Uri(url))]);
private ChatMessageContent CreateMessageWithImageReference(string input, string fileId)
=> new(AuthorRole.User, [new TextContent(input), new FileReferenceContent(fileId)]);
}
@@ -0,0 +1,54 @@
// 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;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate using code-interpreter on <see cref="OpenAIAssistantAgent"/> .
/// </summary>
public class Step04_AssistantTool_CodeInterpreter(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseCodeInterpreterToolWithAssistantAgent()
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
enableCodeInterpreter: true,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
// Create a thread for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient, metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync("Use code to determine the values in the Fibonacci sequence that that are less then the value of 101?");
}
finally
{
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
}
@@ -0,0 +1,73 @@
// 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;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate using <see cref="OpenAIAssistantAgent"/> with file search.
/// </summary>
public class Step05_AssistantTool_FileSearch(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseFileSearchToolWithAssistantAgent()
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
enableFileSearch: true,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
// Upload file - Using a table of fictional employees.
await using Stream stream = EmbeddedResource.ReadStream("employees.pdf")!;
string fileId = await this.Client.UploadAssistantFileAsync(stream, "employees.pdf");
// Create a vector-store
string vectorStoreId =
await this.Client.CreateVectorStoreAsync(
[fileId],
metadata: SampleMetadata);
// Create a thread associated with a vector-store for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(
this.AssistantClient,
vectorStoreId: vectorStoreId,
metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync("Who is the youngest employee?");
await InvokeAgentAsync("Who works in sales?");
await InvokeAgentAsync("I have a customer request, who can help me?");
}
finally
{
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
await this.Client.DeleteVectorStoreAsync(vectorStoreId);
await this.Client.DeleteFileAsync(fileId);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
}
@@ -0,0 +1,77 @@
// 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;
using Plugins;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// This example demonstrates how to define function tools for an <see cref="OpenAIAssistantAgent"/>
/// when the assistant is created. This is useful if you want to retrieve the assistant later and
/// then dynamically check what function tools it requires.
/// </summary>
public class Step06_AssistantTool_Function(ITestOutputHelper output) : BaseAssistantTest(output)
{
private const string HostName = "Host";
private const string HostInstructions = "Answer questions about the menu.";
[Fact]
public async Task UseSingleAssistantWithFunctionTools()
{
// Define the agent
AssistantCreationOptions creationOptions =
new()
{
Name = HostName,
Instructions = HostInstructions,
Metadata =
{
{ SampleMetadataKey, bool.TrueString }
},
};
// In this sample the function tools are added to the assistant this is
// important if you want to retrieve the assistant later and then dynamically check
// what function tools it requires.
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>();
plugin.Select(f => f.ToToolDefinition(plugin.Name)).ToList().ForEach(td => creationOptions.Tools.Add(td));
Assistant definition = await this.AssistantClient.CreateAssistantAsync(this.Model, creationOptions);
OpenAIAssistantAgent agent = new(definition, this.AssistantClient);
// Add plugin to the agent's Kernel (same as direct Kernel usage).
agent.Kernel.Plugins.Add(plugin);
// Create a thread for the agent conversation.
AgentThread thread = new OpenAIAssistantAgentThread(this.AssistantClient, metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("What is the special soup and its price?");
await InvokeAgentAsync("What is the special drink and its price?");
await InvokeAgentAsync("Thank you");
}
finally
{
await thread.DeleteAsync();
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
}
@@ -0,0 +1,223 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure.Core;
using Azure.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// This example demonstrates how to declaratively create instances of <see cref="OpenAIAssistantAgent"/>.
/// </summary>
public class Step07_Assistant_Declarative : BaseAssistantTest
{
/// <summary>
/// Demonstrates creating and using a OpenAI Assistant using configuration.
/// </summary>
[Fact]
public async Task OpenAIAssistantAgentWithConfigurationForOpenAI()
{
var text =
"""
type: openai_assistant
name: MyAgent
description: My helpful agent.
instructions: You are helpful agent.
model:
id: ${OpenAI:ChatModelId}
connection:
type: openai
api_key: ${OpenAI:ApiKey}
""";
OpenAIAssistantAgentFactory factory = new();
var agent = await factory.CreateAgentFromYamlAsync(text, configuration: TestConfiguration.ConfigurationRoot);
await InvokeAgentAsync(agent!, "Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million");
}
/// <summary>
/// Demonstrates creating and using a OpenAI Assistant using configuration for Azure OpenAI.
/// </summary>
[Fact]
public async Task OpenAIAssistantAgentWithConfigurationForAzureOpenAI()
{
var text =
"""
type: openai_assistant
name: MyAgent
description: My helpful agent.
instructions: You are helpful agent.
model:
id: ${AzureOpenAI:ChatModelId}
connection:
type: azure_openai
endpoint: ${AzureOpenAI:Endpoint}
""";
OpenAIAssistantAgentFactory factory = new();
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<TokenCredential>(new AzureCliCredential());
var kernel = builder.Build();
var agent = await factory.CreateAgentFromYamlAsync(text, new() { Kernel = kernel }, TestConfiguration.ConfigurationRoot);
await InvokeAgentAsync(agent!, "Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million");
}
/// <summary>
/// Demonstrates creating and using a OpenAI Assistant using a Kernel.
/// </summary>
[Fact]
public async Task OpenAIAssistantAgentWithKernel()
{
var text =
"""
type: openai_assistant
name: StoryAgent
description: Story Telling Agent
instructions: Tell a story suitable for children about the topic provided by the user.
model:
id: ${AzureOpenAI:ChatModelId}
""";
OpenAIAssistantAgentFactory factory = new();
var agent = await factory.CreateAgentFromYamlAsync(text, new() { Kernel = this._kernel }, configuration: TestConfiguration.ConfigurationRoot);
await InvokeAgentAsync(agent!, "Cats and Dogs");
}
/// <summary>
/// Demonstrates loading an existing OpenAI Assistant.
/// </summary>
[Fact]
public async Task OpenAIAssistantAgentWithId()
{
var text =
"""
id: ${AzureOpenAI:AgentId}
type: openai_assistant
name: StoryAgent
instructions: Tell a story suitable for children about the topic provided by the user. You always respond in French.
""";
OpenAIAssistantAgentFactory factory = new();
var agent = await factory.CreateAgentFromYamlAsync(text, new() { Kernel = this._kernel }, configuration: TestConfiguration.ConfigurationRoot);
await InvokeAgentAsync(agent!, "Cats and Dogs", deleteAgent: false);
}
/// <summary>
/// Demonstrates creating and using a OpenAI Assistant with templated instructions.
/// </summary>
[Fact]
public async Task OpenAIAssistantAgentWithTemplate()
{
var text =
"""
type: openai_assistant
name: StoryAgent
description: A agent that generates a story about a topic.
instructions: Tell a story about {{$topic}} that is {{$length}} sentences long.
model:
id: ${AzureOpenAI:ChatModelId}
inputs:
topic:
description: The topic of the story.
required: true
default: Cats
length:
description: The number of sentences in the story.
required: true
default: 2
outputs:
output1:
description: output1 description
template:
format: semantic-kernel
""";
OpenAIAssistantAgentFactory factory = new();
var promptTemplateFactory = new KernelPromptTemplateFactory();
var agent = await factory.CreateAgentFromYamlAsync(text, new() { Kernel = this._kernel, PromptTemplateFactory = promptTemplateFactory }, TestConfiguration.ConfigurationRoot);
Assert.NotNull(agent);
var options = new AgentInvokeOptions()
{
KernelArguments = new()
{
{ "topic", "Dogs" },
{ "length", "3" },
}
};
AgentThread? agentThread = null;
try
{
await foreach (var response in agent.InvokeAsync(Array.Empty<ChatMessageContent>(), agentThread, options))
{
agentThread = response.Thread;
this.WriteAgentChatMessage(response);
}
}
finally
{
var openaiAgent = agent as OpenAIAssistantAgent;
Assert.NotNull(openaiAgent);
await openaiAgent.Client.DeleteAssistantAsync(openaiAgent.Id);
if (agentThread is not null)
{
await agentThread.DeleteAsync();
}
}
}
public Step07_Assistant_Declarative(ITestOutputHelper output) : base(output)
{
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<OpenAIClient>(this.Client);
this._kernel = builder.Build();
}
#region private
private readonly Kernel _kernel;
/// <summary>
/// Invoke the agent with the user input.
/// </summary>
private async Task InvokeAgentAsync(Agent agent, string input, bool deleteAgent = true)
{
AgentThread? agentThread = null;
try
{
await foreach (AgentResponseItem<ChatMessageContent> response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, input)))
{
agentThread = response.Thread;
WriteAgentChatMessage(response);
}
}
catch (Exception e)
{
Console.WriteLine($"Error invoking agent: {e.Message}");
}
finally
{
if (deleteAgent)
{
var openaiAgent = (OpenAIAssistantAgent)agent;
await openaiAgent.Client.DeleteAssistantAsync(openaiAgent.Id);
}
if (agentThread is not null)
{
await agentThread.DeleteAsync();
}
}
}
#endregion
}