Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example41_HttpClientUsage.cs
T
Shawn Callegariandname f8a8d884ff Moving KernelSyntaxExamples into the dotnet/ folder (#1616)
"KernelSyntaxExamples" is the only outstanding project under /samples
that still uses project references to access the kernel libraries.

This change consolidates all the building, linked dotnet SDK code in
SK-dotnet.sln into a single top-level folder. This will simplify build
jobs, automatic code owner assignment, and other dev ops tasks.

Coming next: other projects in samples will get removed from the
dotnet/SK-dotnet.sln solution, and a new solution with dedicated builds
created for the /samples folder.

TBD:
- Language-specific code "samples" will move into a similar subfolder
for each language.
- The current "/samples" folder (at the root) will likely become
something like "apps", with the demo apps promoted to the top-level
folder for better discoverability.
- notebooks may also move to this folder in the future.

Co-authored-by: name <email>
2023-06-21 01:37:54 +00:00

99 lines
3.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
/**
* These examples show how to use HttpClient and HttpClientFactory within SK SDK.
*/
public static class Example41_HttpClientUsage
{
public static void Run()
{
//Examples showing how to use HttpClient.
UseDefaultHttpClientAsync();
UseCustomHttpClientAsync();
//Examples showing how to use HttpClientFactory.
UseBasicRegistrationWithHttpClientFactoryAsync();
UseNamedRegistrationWitHttpClientFactoryAsync();
}
/// <summary>
/// Demonstrates the usage of the default HttpClient provided by the SK SDK.
/// </summary>
private static void UseDefaultHttpClientAsync()
{
var kernel = Kernel.Builder
.WithOpenAITextCompletionService("<model-id>", "<api-key>") // If you need to use the default HttpClient from the SK SDK, simply omit the argument for the httpMessageInvoker parameter.
.Build();
}
/// <summary>
/// Demonstrates the usage of a custom HttpClient.
/// </summary>
private static void UseCustomHttpClientAsync()
{
using var httpClient = new HttpClient();
// If you need to use a custom HttpClient, simply pass it as an argument for the httpClient parameter.
var kernel = Kernel.Builder
.WithOpenAITextCompletionService("<model-id>", "<api-key>", httpClient: httpClient)
.Build();
}
/// <summary>
/// Demonstrates the "basic usage" approach for HttpClientFactory.
/// </summary>
private static void UseBasicRegistrationWithHttpClientFactoryAsync()
{
//More details - https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#basic-usage
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
var kernel = serviceCollection.AddTransient<IKernel>((sp) =>
{
var factory = sp.GetRequiredService<IHttpClientFactory>();
var kernel = Kernel.Builder
.WithOpenAITextCompletionService("<model-id>", "<api-key>", httpClient: factory.CreateClient())
.Build();
return kernel;
});
}
/// <summary>
/// Demonstrates the "named clients" approach for HttpClientFactory.
/// </summary>
private static void UseNamedRegistrationWitHttpClientFactoryAsync()
{
// More details https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#named-clients
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
//Registration of a named HttpClient.
serviceCollection.AddHttpClient("test-client", (client) =>
{
client.BaseAddress = new Uri("https://api.openai.com/v1/", UriKind.Absolute);
});
var kernel = serviceCollection.AddTransient<IKernel>((sp) =>
{
var factory = sp.GetRequiredService<IHttpClientFactory>();
var kernel = Kernel.Builder
.WithOpenAITextCompletionService("<model-id>", "<api-key>", httpClient: factory.CreateClient("test-client"))
.Build();
return kernel;
});
}
}