mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context Following best practices for libraries outlined at https://learn.microsoft.com/en-us/dotnet/core/extensions/logging-library-authors, SK should generally be taking an ILoggerFactory rather than an ILogger. This allows individual components to each get a logger with its own category applied. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Shawn Callegari <[email protected]> Co-authored-by: Lee Miller <[email protected]>
64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Azure.Identity;
|
|
using Microsoft.SemanticKernel;
|
|
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
|
using RepoUtils;
|
|
|
|
/**
|
|
* This example shows how to connect your app to Azure OpenAI using
|
|
* Azure Active Directory (AAD) authentication, as opposed to API keys.
|
|
*
|
|
* The example uses DefaultAzureCredential, which you can configure to support
|
|
* multiple authentication strategies:
|
|
*
|
|
* - Env vars present in Azure VMs
|
|
* - Azure Managed Identities
|
|
* - Shared tokens
|
|
* - etc.
|
|
*/
|
|
|
|
// ReSharper disable once InconsistentNaming
|
|
public static class Example26_AADAuth
|
|
{
|
|
public static async Task RunAsync()
|
|
{
|
|
Console.WriteLine("======== SK with AAD Auth ========");
|
|
|
|
// Optional: choose which authentication to support
|
|
var authOptions = new DefaultAzureCredentialOptions
|
|
{
|
|
ExcludeEnvironmentCredential = false,
|
|
ExcludeManagedIdentityCredential = false,
|
|
ExcludeSharedTokenCacheCredential = false,
|
|
ExcludeAzureCliCredential = true,
|
|
ExcludeVisualStudioCredential = true,
|
|
ExcludeVisualStudioCodeCredential = true,
|
|
ExcludeInteractiveBrowserCredential = true,
|
|
};
|
|
|
|
IKernel kernel = new KernelBuilder()
|
|
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
|
|
// Add Azure chat completion service using DefaultAzureCredential AAD auth
|
|
.WithAzureChatCompletionService(
|
|
TestConfiguration.AzureOpenAI.ChatDeploymentName,
|
|
"https://....openai.azure.com/",
|
|
new DefaultAzureCredential(authOptions))
|
|
.Build();
|
|
|
|
IChatCompletion chatGPT = kernel.GetService<IChatCompletion>();
|
|
var chatHistory = chatGPT.CreateNewChat();
|
|
|
|
// User message
|
|
chatHistory.AddUserMessage("Tell me a joke about hourglasses");
|
|
|
|
// Bot reply
|
|
string reply = await chatGPT.GenerateMessageAsync(chatHistory);
|
|
Console.WriteLine(reply);
|
|
|
|
/* Output: Why did the hourglass go to the doctor? Because it was feeling a little run down! */
|
|
}
|
|
}
|