Files
Roger Barreto dac17a8f48 .NET: Adding Semantic Kernel - Migration Samples (#499)
* Guidance

* Guidance

* WIP Migration Preps

* Move to single file projects

* Update guidance code and final adjustments to ensure all feature compatibility

* Move from demos to samples

* Address format

* Address chat client pipeline order

* Update project naming

* Revisition on README

* Remove unused ctor

* Address feedback

* Address feedback

* Address merge conflict fix

* Address SK versioning

* Address folder naming

* Address feedback
2025-08-29 14:29:03 +00:00

98 lines
3.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
#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 Microsoft.Extensions.AI.Agents;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using OpenAI;
using OpenAI.Assistants;
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set.");
var modelId = System.Environment.GetEnvironmentVariable("OPENAI_MODELID") ?? "gpt-4o";
var userInput = "Tell me a joke about a pirate.";
Console.WriteLine($"User Input: {userInput}");
await SKAgent();
await AFAgent();
async Task SKAgent()
{
Console.WriteLine("\n=== SK Agent ===\n");
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton((sp) => new AssistantClient(apiKey));
serviceCollection.AddKernel().AddOpenAIChatClient(modelId, apiKey);
serviceCollection.AddTransient((sp) =>
{
var assistantsClient = sp.GetRequiredService<AssistantClient>();
Assistant assistant = assistantsClient.CreateAssistant(modelId, new() { Name = "Joker", Instructions = "You are good at telling jokes." });
return new OpenAIAssistantAgent(assistant, assistantsClient);
});
await using ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
var agent = serviceProvider.GetRequiredService<OpenAIAssistantAgent>();
// Create a thread for the agent conversation.
var assistantsClient = serviceProvider.GetRequiredService<AssistantClient>();
var thread = new OpenAIAssistantAgentThread(assistantsClient);
var settings = new OpenAIPromptExecutionSettings() { MaxTokens = 1000 };
var agentOptions = new OpenAIAssistantAgentInvokeOptions() { KernelArguments = new(settings) };
await foreach (var result in agent.InvokeAsync(userInput, thread, agentOptions))
{
Console.WriteLine(result.Message);
}
Console.WriteLine("---");
await foreach (var update in agent.InvokeStreamingAsync(userInput, thread, agentOptions))
{
Console.Write(update.Message);
}
// Clean up
await thread.DeleteAsync();
await assistantsClient.DeleteAssistantAsync(agent.Id);
}
async Task AFAgent()
{
Console.WriteLine("\n=== AF Agent ===\n");
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton((sp) => new AssistantClient(apiKey));
serviceCollection.AddTransient((sp) =>
{
var assistantClient = sp.GetRequiredService<AssistantClient>();
var agent = assistantClient.CreateAIAgent(modelId, name: "Joker", instructions: "You are good at telling jokes.");
return agent;
});
await using ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
var agent = serviceProvider.GetRequiredService<AIAgent>();
var thread = agent.GetNewThread();
var agentOptions = new ChatClientAgentRunOptions(new() { MaxOutputTokens = 1000 });
var result = await agent.RunAsync(userInput, thread, agentOptions);
Console.WriteLine(result);
Console.WriteLine("---");
await foreach (var update in agent.RunStreamingAsync(userInput, thread, agentOptions))
{
Console.Write(update);
}
// Clean up
var assistantClient = serviceProvider.GetRequiredService<AssistantClient>();
await assistantClient.DeleteThreadAsync(thread.ConversationId);
await assistantClient.DeleteAssistantAsync(agent.Id);
}