Skip to content

Commit

Permalink
.Net - Complete Ability to use Samples without API Key (Security Wave) (
Browse files Browse the repository at this point in the history
#9671)

### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Integration tests had been converted, but samples remain bound to API
key usage.

Now able to use Azure CLI to run samples:

```powershell
az login
az account get-access-token
```

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

- Facilitates use of Azure resources instead of retargeting samples for
Open AI.
- Eliminates need to utilize M.A.D.E. tenant for API key loophole

> Note: Individual samples may still require refactoring to target
`BaseTest`: `base.AddChatCompletionToKernel(builder);`

### 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
- [X] I didn't break anyone 😄
  • Loading branch information
crickman authored Nov 13, 2024
1 parent 17a0df4 commit 78b6990
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
Expand Down
1 change: 1 addition & 0 deletions dotnet/samples/LearnResources/LearnResources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ClientModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
Expand Down Expand Up @@ -41,8 +42,10 @@ public abstract class BaseAgentsTest(ITestOutputHelper output) : BaseTest(output
protected OpenAIClientProvider GetClientProvider()
=>
this.UseOpenAIConfig ?
OpenAIClientProvider.ForOpenAI(new ApiKeyCredential(this.ApiKey)) :
OpenAIClientProvider.ForAzureOpenAI(new ApiKeyCredential(this.ApiKey), new Uri(this.Endpoint!));
OpenAIClientProvider.ForOpenAI(new ApiKeyCredential(this.ApiKey ?? throw new ConfigurationNotFoundException("OpenAI:ApiKey"))) :
!string.IsNullOrWhiteSpace(this.ApiKey) ?
OpenAIClientProvider.ForAzureOpenAI(new ApiKeyCredential(this.ApiKey), new Uri(this.Endpoint!)) :
OpenAIClientProvider.ForAzureOpenAI(new AzureCliCredential(), new Uri(this.Endpoint!));

/// <summary>
/// Common method to write formatted agent chat content to the console.
Expand Down
12 changes: 10 additions & 2 deletions dotnet/src/InternalUtilities/samples/InternalUtilities/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
Expand All @@ -27,7 +28,7 @@ public abstract class BaseTest : TextWriter

protected bool UseOpenAIConfig => this.ForceOpenAI || string.IsNullOrEmpty(TestConfiguration.AzureOpenAI.Endpoint);

protected string ApiKey =>
protected string? ApiKey =>
this.UseOpenAIConfig ?
TestConfiguration.OpenAI.ApiKey :
TestConfiguration.AzureOpenAI.ApiKey;
Expand Down Expand Up @@ -61,13 +62,20 @@ protected void AddChatCompletionToKernel(IKernelBuilder builder)
TestConfiguration.OpenAI.ChatModelId,
TestConfiguration.OpenAI.ApiKey);
}
else
else if (!string.IsNullOrEmpty(this.ApiKey))
{
builder.AddAzureOpenAIChatCompletion(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
TestConfiguration.AzureOpenAI.Endpoint,
TestConfiguration.AzureOpenAI.ApiKey);
}
else
{
builder.AddAzureOpenAIChatCompletion(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
TestConfiguration.AzureOpenAI.Endpoint,
new AzureCliCredential());
}
}

protected BaseTest(ITestOutputHelper output, bool redirectSystemConsoleOutput = false)
Expand Down

0 comments on commit 78b6990

Please sign in to comment.