Skip to content

Commit

Permalink
.Net: Simplify example 52 to showcase using a custom OpenAIClient in …
Browse files Browse the repository at this point in the history
…genera… (#4502)

…l instead of using one specifically for APIM

### Motivation and Context
Setting up APIM and AAD is tedious and is not directly related to what
we want to showcase: that we can customize OpenAIClient instances used
with SK.

resolves #3844 

### Description
Simplify examples and its requirements.

### Contribution Checklist
- [ ] The code builds clean without any errors or warnings
- [ ] 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
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
  • Loading branch information
glahaye and crickman authored Jan 16, 2024
1 parent b9c1adc commit 7d47e90
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 106 deletions.
99 changes: 0 additions & 99 deletions dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Azure;
using Azure.AI.OpenAI;
using Azure.Core.Pipeline;
using Microsoft.SemanticKernel;
using RepoUtils;

public static class Example52_CustomOpenAIClient
{
public static async Task RunAsync()
{
Console.WriteLine("======== Using a custom OpenAI client ========");

string endpoint = TestConfiguration.AzureOpenAI.Endpoint;
string deploymentName = TestConfiguration.AzureOpenAI.ChatDeploymentName;
string apiKey = TestConfiguration.AzureOpenAI.ApiKey;

if (endpoint is null || deploymentName is null || apiKey is null)
{
Console.WriteLine("Azure OpenAI credentials not found. Skipping example.");
return;
}

// Create an HttpClient and include your custom header(s)
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("x-my-custom-header", "My custom value");

// Configure OpenAIClient to use the customized HttpClient
var clientOptions = new OpenAIClientOptions
{
Transport = new HttpClientTransport(httpClient),
};
var openAIClient = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey), clientOptions);

IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(deploymentName, openAIClient);
Kernel kernel = builder.Build();

// Load semantic plugin defined with prompt templates
string folder = RepoFiles.SamplePluginsPath();

kernel.ImportPluginFromPromptDirectory(Path.Combine(folder, "FunPlugin"));

// Run
var result = await kernel.InvokeAsync(
kernel.Plugins["FunPlugin"]["Excuses"],
new() { ["input"] = "I have no homework" }
);
Console.WriteLine(result.GetValue<string>());

httpClient.Dispose();
}
}
7 changes: 0 additions & 7 deletions dotnet/samples/KernelSyntaxExamples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ dotnet user-secrets set "Google:SearchEngineId" "..."
dotnet user-secrets set "Github:PAT" "github_pat_..."
dotnet user-secrets set "Apim:Endpoint" "https://apim...azure-api.net/"
dotnet user-secrets set "Apim:SubscriptionKey" "..."
dotnet user-secrets set "Postgres:ConnectionString" "..."
dotnet user-secrets set "Redis:Configuration" "..."
dotnet user-secrets set "Kusto:ConnectionString" "..."
Expand Down Expand Up @@ -173,10 +170,6 @@ Google__SearchEngineId
# Github
Github__PAT
# Azure API Management (APIM)
Apim__Endpoint
Apim__SubscriptionKey
# Other
Postgres__ConnectionString
Redis__Configuration
Expand Down

0 comments on commit 7d47e90

Please sign in to comment.