mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
102 lines
3.4 KiB
C#
102 lines
3.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Net.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.SemanticKernel;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Examples;
|
|
|
|
// These examples show how to use HttpClient and HttpClientFactory within SK SDK.
|
|
public class Example41_HttpClientUsage : BaseTest
|
|
{
|
|
/// <summary>
|
|
/// Demonstrates the usage of the default HttpClient provided by the SK SDK.
|
|
/// </summary>
|
|
[Fact]
|
|
public void UseDefaultHttpClient()
|
|
{
|
|
var kernel = Kernel.CreateBuilder()
|
|
.AddOpenAIChatCompletion(
|
|
modelId: TestConfiguration.OpenAI.ChatModelId,
|
|
apiKey: TestConfiguration.OpenAI.ApiKey) // 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>
|
|
[Fact]
|
|
public void UseCustomHttpClient()
|
|
{
|
|
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.CreateBuilder()
|
|
.AddOpenAIChatCompletion(
|
|
modelId: TestConfiguration.OpenAI.ModelId,
|
|
apiKey: TestConfiguration.OpenAI.ApiKey,
|
|
httpClient: httpClient)
|
|
.Build();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demonstrates the "basic usage" approach for HttpClientFactory.
|
|
/// </summary>
|
|
[Fact]
|
|
public void UseBasicRegistrationWithHttpClientFactory()
|
|
{
|
|
//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<Kernel>((sp) =>
|
|
{
|
|
var factory = sp.GetRequiredService<IHttpClientFactory>();
|
|
|
|
return Kernel.CreateBuilder()
|
|
.AddOpenAIChatCompletion(
|
|
modelId: TestConfiguration.OpenAI.ChatModelId,
|
|
apiKey: TestConfiguration.OpenAI.ApiKey,
|
|
httpClient: factory.CreateClient())
|
|
.Build();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demonstrates the "named clients" approach for HttpClientFactory.
|
|
/// </summary>
|
|
[Fact]
|
|
public void UseNamedRegistrationWitHttpClientFactory()
|
|
{
|
|
// 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<Kernel>((sp) =>
|
|
{
|
|
var factory = sp.GetRequiredService<IHttpClientFactory>();
|
|
|
|
return Kernel.CreateBuilder()
|
|
.AddOpenAIChatCompletion(
|
|
modelId: TestConfiguration.OpenAI.ChatModelId,
|
|
apiKey: TestConfiguration.OpenAI.ApiKey,
|
|
httpClient: factory.CreateClient("test-client"))
|
|
.Build();
|
|
});
|
|
}
|
|
|
|
public Example41_HttpClientUsage(ITestOutputHelper output) : base(output)
|
|
{
|
|
}
|
|
}
|