Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions src/quickstarts/azure-openai/01-HikeBenefitsSummary/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = $"""
Expand All @@ -27,5 +27,5 @@
Console.WriteLine($"user >>> {prompt}");

// Submit the prompt and print out the response
string response = await kernel.InvokePromptAsync<string>(prompt, new(new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
ChatCompletion response = await client.CompleteAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
Console.WriteLine($"assistant >>> {response}");
3 changes: 2 additions & 1 deletion src/quickstarts/azure-openai/02-HikerAI/02-HikerAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

</Project>
29 changes: 18 additions & 11 deletions src/quickstarts/azure-openai/02-HikerAI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<ChatMessage> 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:
Expand All @@ -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()}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
28 changes: 16 additions & 12 deletions src/quickstarts/azure-openai/03-ChattingAboutMyHikes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<ChatMessage> 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()}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.24531.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

</Project>
55 changes: 27 additions & 28 deletions src/quickstarts/azure-openai/04-HikerAIPro/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<ChatMessage> 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<IChatCompletionService>().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()}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0-beta.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

</Project>
16 changes: 10 additions & 6 deletions src/quickstarts/azure-openai/05-HikeImages/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}");
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 6 additions & 7 deletions src/quickstarts/openai/01-HikeBenefitsSummary/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = $"""
Expand All @@ -26,5 +25,5 @@
Console.WriteLine($"user >>> {prompt}");

// Submit the prompt and print out the response
string response = await kernel.InvokePromptAsync<string>(prompt, new(new OpenAIPromptExecutionSettings() { MaxTokens = 400 }));
ChatCompletion response = await client.CompleteAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
Console.WriteLine($"assistant >>> {response}");
1 change: 1 addition & 0 deletions src/quickstarts/openai/02-HikerAI/02-HikerAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24525.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
Expand Down
Loading