diff --git a/src/quickstarts/azure-openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj b/src/quickstarts/azure-openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
index fe7afb94..3fd65575 100644
--- a/src/quickstarts/azure-openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
+++ b/src/quickstarts/azure-openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
@@ -8,9 +8,10 @@
+
+
-
diff --git a/src/quickstarts/azure-openai/01-HikeBenefitsSummary/Program.cs b/src/quickstarts/azure-openai/01-HikeBenefitsSummary/Program.cs
index 772ae559..1320a9cd 100644
--- a/src/quickstarts/azure-openai/01-HikeBenefitsSummary/Program.cs
+++ b/src/quickstarts/azure-openai/01-HikeBenefitsSummary/Program.cs
@@ -2,9 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.ClientModel;
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using Microsoft.Extensions.AI;
+using Azure.AI.OpenAI;
// Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
// because you already have an Azure OpenAI available, edit the following lines to use your information,
@@ -14,10 +15,9 @@
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];
-// Create a Kernel containing the Azure OpenAI Chat Completion Service
-Kernel kernel = Kernel.CreateBuilder()
- .AddAzureOpenAIChatCompletion(deployment, endpoint, key)
- .Build();
+IChatClient client =
+ new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
+ .AsChatClient(deployment);
// Create and print out the prompt
string prompt = $"""
@@ -27,5 +27,5 @@
Console.WriteLine($"user >>> {prompt}");
// Submit the prompt and print out the response
-string response = await kernel.InvokePromptAsync(prompt, new(new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
+ChatCompletion response = await client.CompleteAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
Console.WriteLine($"assistant >>> {response}");
diff --git a/src/quickstarts/azure-openai/02-HikerAI/02-HikerAI.csproj b/src/quickstarts/azure-openai/02-HikerAI/02-HikerAI.csproj
index 5329a31b..78d29d1d 100644
--- a/src/quickstarts/azure-openai/02-HikerAI/02-HikerAI.csproj
+++ b/src/quickstarts/azure-openai/02-HikerAI/02-HikerAI.csproj
@@ -8,10 +8,11 @@
+
+
-
diff --git a/src/quickstarts/azure-openai/02-HikerAI/Program.cs b/src/quickstarts/azure-openai/02-HikerAI/Program.cs
index 0bced587..22aca818 100644
--- a/src/quickstarts/azure-openai/02-HikerAI/Program.cs
+++ b/src/quickstarts/azure-openai/02-HikerAI/Program.cs
@@ -2,9 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.ClientModel;
+using Azure.AI.OpenAI;
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel.ChatCompletion;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
// Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
// because you already have an Azure OpenAI available, edit the following lines to use your information,
@@ -15,10 +16,12 @@
string key = config["AZURE_OPENAI_KEY"];
// Create the Azure OpenAI Chat Completion Service
-AzureOpenAIChatCompletionService service = new(deployment, endpoint, key);
+IChatClient client =
+ new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
+ .AsChatClient(deployment);
// Start the conversation with context for the AI model
-ChatHistory chatHistory = new("""
+List chatHistory = [new(ChatRole.System, """
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
You introduce yourself when first saying hello. When helping people out, you always ask them
for this information to inform the hiking recommendation you provide:
@@ -28,25 +31,29 @@ 1. Where they are located
You will then provide three suggestions for nearby hikes that vary in length after you get that information.
You will also share an interesting fact about the local nature on the hikes when making a recommendation.
- """);
+ """)];
+
await PrintAndSendAsync();
// Continue the conversation with a question
-chatHistory.AddUserMessage("Hi! Apparently you can help me find a hike that I will like?");
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi! Apparently you can help me find a hike that I will like?"));
+
await PrintAndSendAsync();
// Continue the conversation with another question
-chatHistory.AddUserMessage("""
+chatHistory.Add(new ChatMessage(ChatRole.User, """
I live in the greater Montreal area and would like an easy hike. I don't mind driving a bit to get there.
I don't want the hike to be over 10 miles round trip. I'd consider a point-to-point hike.
I want the hike to be as isolated as possible. I don't want to see many people.
I would like it to be as bug free as possible.
- """);
+ """));
+
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
- chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, new ChatOptions { MaxOutputTokens = 400 });
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Text));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
}
diff --git a/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj b/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
index a7c2724a..2c3cb017 100644
--- a/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
+++ b/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
@@ -8,9 +8,10 @@
+
+
-
diff --git a/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/Program.cs b/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/Program.cs
index 965c8e12..3285d1a7 100644
--- a/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/Program.cs
+++ b/src/quickstarts/azure-openai/03-ChattingAboutMyHikes/Program.cs
@@ -3,8 +3,9 @@
// See the LICENSE file in the project root for more information.
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel.ChatCompletion;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using Microsoft.Extensions.AI;
+using Azure.AI.OpenAI;
+using System.ClientModel;
// Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
// because you already have an Azure OpenAI available, edit the following lines to use your information,
@@ -14,29 +15,32 @@
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];
-// Create the Azure OpenAI Chat Completion Service
-AzureOpenAIChatCompletionService service = new(deployment, endpoint, key);
+// Create the IChatClient
+IChatClient client =
+ new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
+ .AsChatClient(deployment);
// Provide context for the AI model
-ChatHistory chatHistory = new($"""
+List chatHistory = [new(ChatRole.System, $"""
You are upbeat and friendly. You introduce yourself when first saying hello.
Provide a short answer only based on the user hiking records below:
{File.ReadAllText("hikes.md")}
- """);
-Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ """)];
+Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
// Start the conversation
-chatHistory.AddUserMessage("Hi!");
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi!"));
await PrintAndSendAsync();
// Continue the conversation with a question.
-chatHistory.AddUserMessage("I would like to know the ratio of the hikes I've done in Canada compared to other countries.");
+chatHistory.Add(new ChatMessage(ChatRole.User, "I would like to know the ratio of the hikes I've done in Canada compared to other countries."));
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
- chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, new ChatOptions { MaxOutputTokens = 400 });
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Text));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
}
diff --git a/src/quickstarts/azure-openai/04-HikerAIPro/04-HikerAIPro.csproj b/src/quickstarts/azure-openai/04-HikerAIPro/04-HikerAIPro.csproj
index 1bb57189..f58f15a6 100644
--- a/src/quickstarts/azure-openai/04-HikerAIPro/04-HikerAIPro.csproj
+++ b/src/quickstarts/azure-openai/04-HikerAIPro/04-HikerAIPro.csproj
@@ -8,12 +8,11 @@
+
+
+
-
-
-
-
diff --git a/src/quickstarts/azure-openai/04-HikerAIPro/Program.cs b/src/quickstarts/azure-openai/04-HikerAIPro/Program.cs
index 6987a1e3..c71ac010 100644
--- a/src/quickstarts/azure-openai/04-HikerAIPro/Program.cs
+++ b/src/quickstarts/azure-openai/04-HikerAIPro/Program.cs
@@ -2,13 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System.ComponentModel;
+using System.ClientModel;
+using Azure.AI.OpenAI;
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Microsoft.SemanticKernel;
-using Microsoft.SemanticKernel.ChatCompletion;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
// Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
// because you already have an Azure OpenAI available, edit the following lines to use your information,
@@ -18,41 +15,43 @@
string deployment = config["AZURE_OPENAI_GPT_NAME"];
string key = config["AZURE_OPENAI_KEY"];
-// Create a Kernel containing the Azure OpenAI Chat Completion Service
-IKernelBuilder b = Kernel.CreateBuilder();
-//b.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace)); // uncomment to see all interactions with the model logged
-Kernel kernel = b
- .AddAzureOpenAIChatCompletion(deployment, endpoint, key)
- .Build();
+// Create the IChatClient
+IChatClient client =
+ new ChatClientBuilder()
+ .UseFunctionInvocation()
+ .Use(
+ new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
+ .AsChatClient(deployment));
// Add a new plugin with a local .NET function that should be available to the AI model
-kernel.ImportPluginFromFunctions("WeatherPlugin",
-[
- KernelFunctionFactory.CreateFromMethod(([Description("The city, e.g. Montreal, Sidney")] string location, string unit = null) =>
+var chatOptions = new ChatOptions
+{
+ Tools = [AIFunctionFactory.Create((string location, string unit) =>
{
// Here you would call a weather API to get the weather for the location
return "Periods of rain or drizzle, 15 C";
- }, "get_current_weather", "Get the current weather in a given location")
-]);
+ },
+ "get_current_weather",
+ "Get the current weather in a given location")]
+};
// Start the conversation
-ChatHistory chatHistory = new("""
+List chatHistory = [new(ChatRole.System, """
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
You introduce yourself when first saying hello.
- """);
-chatHistory.AddUserMessage("Hi!");
+ """)];
+
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi!"));
await PrintAndSendAsync();
// Continue the conversation
-chatHistory.AddUserMessage("I live in Montreal and I'm looking for a moderate intensity hike. Is the weather good today for a hike? ");
+chatHistory.Add(new ChatMessage(ChatRole.User, "I live in Montreal and I'm looking for a moderate intensity hike. Is the weather good today for a hike? "));
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
-
- OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
- chatHistory.Add(await kernel.GetRequiredService().GetChatMessageContentAsync(chatHistory, settings, kernel));
-
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
-}
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, chatOptions);
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Contents));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+}
\ No newline at end of file
diff --git a/src/quickstarts/azure-openai/05-HikeImages/05-HikeImages.csproj b/src/quickstarts/azure-openai/05-HikeImages/05-HikeImages.csproj
index 32ef996d..2a130735 100644
--- a/src/quickstarts/azure-openai/05-HikeImages/05-HikeImages.csproj
+++ b/src/quickstarts/azure-openai/05-HikeImages/05-HikeImages.csproj
@@ -9,9 +9,9 @@
+
-
diff --git a/src/quickstarts/azure-openai/05-HikeImages/Program.cs b/src/quickstarts/azure-openai/05-HikeImages/Program.cs
index 1cd9102b..cffc6afd 100644
--- a/src/quickstarts/azure-openai/05-HikeImages/Program.cs
+++ b/src/quickstarts/azure-openai/05-HikeImages/Program.cs
@@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using OpenAI.Images;
+using System.ClientModel;
+using Azure.AI.OpenAI;
// Retrieve the local secrets saved during the Azure deployment. If you skipped the deployment
// because you already have an Azure OpenAI available, edit the following lines to use your information,
@@ -13,13 +15,15 @@
string deployment = config["AZURE_OPENAI_DALLE_NAME"];
string key = config["AZURE_OPENAI_KEY"];
-// Create the Azure OpeAI Text to Image Service
-AzureOpenAITextToImageService textToImageService = new(deployment, endpoint, key, null);
+// Create the Azure OpenAI ImageClient
+ImageClient client =
+ new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
+ .GetImageClient(deployment);
// Generate the image
-string imageUrl = await textToImageService.GenerateImageAsync("""
+GeneratedImage generatedImage = await client.GenerateImageAsync("""
A postal card with an happy hiker waving and a beautiful mountain in the background.
There is a trail visible in the foreground.
The postal card has text in red saying: 'You are invited for a hike!'
- """, 1024, 1024);
-Console.WriteLine($"The generated image is ready at:\n{imageUrl}");
+ """, new ImageGenerationOptions { Size = GeneratedImageSize.W1024xH1024 });
+Console.WriteLine($"The generated image is ready at:\n{generatedImage.ImageUri}");
diff --git a/src/quickstarts/openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj b/src/quickstarts/openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
index fe7afb94..42ab31fe 100644
--- a/src/quickstarts/openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
+++ b/src/quickstarts/openai/01-HikeBenefitsSummary/01-HikeBenefitsSummary.csproj
@@ -8,9 +8,9 @@
+
-
diff --git a/src/quickstarts/openai/01-HikeBenefitsSummary/Program.cs b/src/quickstarts/openai/01-HikeBenefitsSummary/Program.cs
index cbf5ca41..9bf0b629 100644
--- a/src/quickstarts/openai/01-HikeBenefitsSummary/Program.cs
+++ b/src/quickstarts/openai/01-HikeBenefitsSummary/Program.cs
@@ -2,9 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using OpenAI;
// Retrieve the local secrets that were set from the command line, using:
// dotnet user-secrets init
@@ -13,10 +13,9 @@
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];
-// Create a Kernel containing the OpenAI Chat Completion Service
-Kernel kernel = Kernel.CreateBuilder()
- .AddOpenAIChatCompletion(model, key)
- .Build();
+// Create the IChatClient
+IChatClient client =
+ new OpenAIClient(key).AsChatClient(model);
// Create and print out the prompt
string prompt = $"""
@@ -26,5 +25,5 @@
Console.WriteLine($"user >>> {prompt}");
// Submit the prompt and print out the response
-string response = await kernel.InvokePromptAsync(prompt, new(new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
+ChatCompletion response = await client.CompleteAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
Console.WriteLine($"assistant >>> {response}");
diff --git a/src/quickstarts/openai/02-HikerAI/02-HikerAI.csproj b/src/quickstarts/openai/02-HikerAI/02-HikerAI.csproj
index 5329a31b..96178465 100644
--- a/src/quickstarts/openai/02-HikerAI/02-HikerAI.csproj
+++ b/src/quickstarts/openai/02-HikerAI/02-HikerAI.csproj
@@ -8,6 +8,7 @@
+
diff --git a/src/quickstarts/openai/02-HikerAI/Program.cs b/src/quickstarts/openai/02-HikerAI/Program.cs
index 127c88e1..33de1645 100644
--- a/src/quickstarts/openai/02-HikerAI/Program.cs
+++ b/src/quickstarts/openai/02-HikerAI/Program.cs
@@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
+using OpenAI;
// Retrieve the local secrets that were set from the command line, using:
// dotnet user-secrets init
@@ -13,11 +15,12 @@
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];
-// Create the OpenAI Chat Completion Service
-OpenAIChatCompletionService service = new(model, key);
+// Create the IChatClient
+IChatClient client =
+ new OpenAIClient(key).AsChatClient(model);
// Start the conversation with context for the AI model
-ChatHistory chatHistory = new("""
+List chatHistory = [new(ChatRole.System, """
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
You introduce yourself when first saying hello. When helping people out, you always ask them
for this information to inform the hiking recommendation you provide:
@@ -27,25 +30,28 @@ 1. Where they are located
You will then provide three suggestions for nearby hikes that vary in length after you get that information.
You will also share an interesting fact about the local nature on the hikes when making a recommendation.
- """);
+ """)];
await PrintAndSendAsync();
// Continue the conversation with a question
-chatHistory.AddUserMessage("Hi! Apparently you can help me find a hike that I will like?");
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi! Apparently you can help me find a hike that I will like?"));
+
await PrintAndSendAsync();
// Continue the conversation with another question
-chatHistory.AddUserMessage("""
+chatHistory.Add(new ChatMessage(ChatRole.User, """
I live in the greater Montreal area and would like an easy hike. I don't mind driving a bit to get there.
I don't want the hike to be over 10 miles round trip. I'd consider a point-to-point hike.
I want the hike to be as isolated as possible. I don't want to see many people.
I would like it to be as bug free as possible.
- """);
+ """));
+
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
- chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, new ChatOptions { MaxOutputTokens = 400 });
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Text));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
}
diff --git a/src/quickstarts/openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj b/src/quickstarts/openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
index a7c2724a..63683df2 100644
--- a/src/quickstarts/openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
+++ b/src/quickstarts/openai/03-ChattingAboutMyHikes/03-ChattingAboutMyHikes.csproj
@@ -8,9 +8,9 @@
+
-
diff --git a/src/quickstarts/openai/03-ChattingAboutMyHikes/Program.cs b/src/quickstarts/openai/03-ChattingAboutMyHikes/Program.cs
index 377e48c6..a8f218eb 100644
--- a/src/quickstarts/openai/03-ChattingAboutMyHikes/Program.cs
+++ b/src/quickstarts/openai/03-ChattingAboutMyHikes/Program.cs
@@ -2,9 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel.ChatCompletion;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using OpenAI;
// Retrieve the local secrets that were set from the command line, using:
// dotnet user-secrets init
@@ -13,29 +13,32 @@
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];
-// Create the OpenAI Chat Completion Service
-OpenAIChatCompletionService service = new(model, key);
+// Create the IChatClient
+IChatClient client =
+ new OpenAIClient(key)
+ .AsChatClient(model);
// Provide context for the AI model
-ChatHistory chatHistory = new($"""
+List chatHistory = [new(ChatRole.System, $"""
You are upbeat and friendly. You introduce yourself when first saying hello.
Provide a short answer only based on the user hiking records below:
{File.ReadAllText("hikes.md")}
- """);
-Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ """)];
+Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
// Start the conversation
-chatHistory.AddUserMessage("Hi!");
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi!"));
await PrintAndSendAsync();
// Continue the conversation with a question.
-chatHistory.AddUserMessage("I would like to know the ratio of the hikes I've done in Canada compared to other countries.");
+chatHistory.Add(new ChatMessage(ChatRole.User, "I would like to know the ratio of the hikes I've done in Canada compared to other countries."));
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
- chatHistory.Add(await service.GetChatMessageContentAsync(chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, new ChatOptions { MaxOutputTokens = 400 });
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Text));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
}
diff --git a/src/quickstarts/openai/04-HikerAIPro/04-HikerAIPro.csproj b/src/quickstarts/openai/04-HikerAIPro/04-HikerAIPro.csproj
index 1bb57189..299f893c 100644
--- a/src/quickstarts/openai/04-HikerAIPro/04-HikerAIPro.csproj
+++ b/src/quickstarts/openai/04-HikerAIPro/04-HikerAIPro.csproj
@@ -8,12 +8,10 @@
+
+
-
-
-
-
diff --git a/src/quickstarts/openai/04-HikerAIPro/Program.cs b/src/quickstarts/openai/04-HikerAIPro/Program.cs
index 9ec20e4b..980f6366 100644
--- a/src/quickstarts/openai/04-HikerAIPro/Program.cs
+++ b/src/quickstarts/openai/04-HikerAIPro/Program.cs
@@ -3,12 +3,10 @@
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
+using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Microsoft.SemanticKernel;
-using Microsoft.SemanticKernel.ChatCompletion;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
+using OpenAI;
// Retrieve the local secrets that were set from the command line, using:
// dotnet user-secrets init
@@ -17,41 +15,42 @@
string model = "gpt-3.5-turbo";
string key = config["OpenAIKey"];
-// Create a Kernel containing the OpenAI Chat Completion Service
-IKernelBuilder b = Kernel.CreateBuilder();
-//b.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace)); // uncomment to see all interactions with the model logged
-Kernel kernel = b
- .AddOpenAIChatCompletion(model, key)
- .Build();
+// Create the IChatClient
+IChatClient client =
+ new ChatClientBuilder()
+ .UseFunctionInvocation()
+ .Use(new OpenAIClient(key).AsChatClient(model));
// Add a new plugin with a local .NET function that should be available to the AI model
-kernel.ImportPluginFromFunctions("WeatherPlugin",
-[
- KernelFunctionFactory.CreateFromMethod(([Description("The city, e.g. Montreal, Sidney")] string location, string unit = null) =>
+var chatOptions = new ChatOptions
+{
+ Tools = [AIFunctionFactory.Create((string location, string unit) =>
{
// Here you would call a weather API to get the weather for the location
return "Periods of rain or drizzle, 15 C";
- }, "get_current_weather", "Get the current weather in a given location")
-]);
+ },
+ "get_current_weather",
+ "Get the current weather in a given location")]
+};
+
// Start the conversation
-ChatHistory chatHistory = new("""
+List chatHistory = [new(ChatRole.System, """
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
You introduce yourself when first saying hello.
- """);
-chatHistory.AddUserMessage("Hi!");
+ """)];
+
+chatHistory.Add(new ChatMessage(ChatRole.User, "Hi!"));
await PrintAndSendAsync();
// Continue the conversation
-chatHistory.AddUserMessage("I live in Montreal and I'm looking for a moderate intensity hike. Is the weather good today for a hike? ");
+chatHistory.Add(new ChatMessage(ChatRole.User, "I live in Montreal and I'm looking for a moderate intensity hike. Is the weather good today for a hike? "));
await PrintAndSendAsync();
async Task PrintAndSendAsync()
{
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
-
- OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
- chatHistory.Add(await kernel.GetRequiredService().GetChatMessageContentAsync(chatHistory, settings, kernel));
-
- Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
+ var response = await client.CompleteAsync(chatHistory, chatOptions);
+ chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Contents));
+ Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
}
diff --git a/src/quickstarts/openai/05-HikeImages/05-HikeImages.csproj b/src/quickstarts/openai/05-HikeImages/05-HikeImages.csproj
index 32ef996d..089ff7db 100644
--- a/src/quickstarts/openai/05-HikeImages/05-HikeImages.csproj
+++ b/src/quickstarts/openai/05-HikeImages/05-HikeImages.csproj
@@ -11,7 +11,9 @@
-
+
+
+
diff --git a/src/quickstarts/openai/05-HikeImages/Program.cs b/src/quickstarts/openai/05-HikeImages/Program.cs
index b2225905..bd023f05 100644
--- a/src/quickstarts/openai/05-HikeImages/Program.cs
+++ b/src/quickstarts/openai/05-HikeImages/Program.cs
@@ -3,21 +3,20 @@
// See the LICENSE file in the project root for more information.
using Microsoft.Extensions.Configuration;
-using Microsoft.SemanticKernel.Connectors.OpenAI;
-
+using OpenAI.Images;
// Retrieve the local secrets that were set from the command line, using:
// dotnet user-secrets init
// dotnet user-secrets set OpenAIKey
var config = new ConfigurationBuilder().AddUserSecrets().Build();
string key = config["OpenAIKey"];
-// Create the OpeAI Text to Image Service
-OpenAITextToImageService textToImageService = new(key, null);
+// Create the OpenAI ImageClient
+ImageClient client = new("dall-e-3", key);
// Generate the image
-string imageUrl = await textToImageService.GenerateImageAsync("""
+GeneratedImage generatedImage = await client.GenerateImageAsync("""
A postal card with an happy hiker waving and a beautiful mountain in the background.
There is a trail visible in the foreground.
The postal card has text in red saying: 'You are invited for a hike!'
- """, 1024, 1024);
-Console.WriteLine($"The generated image is ready at:\n{imageUrl}");
+ """, new ImageGenerationOptions { Size = GeneratedImageSize.W1024xH1024 });
+Console.WriteLine($"The generated image is ready at:\n{generatedImage.ImageUri}");