forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Simplify example 52 to showcase using a custom OpenAIClient in …
…genera… (microsoft#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 microsoft#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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
58 additions
and
106 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
dotnet/samples/KernelSyntaxExamples/Example52_CustomOpenAIClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters