Semantic Kernel 没有提供通义千问 Qwen 的内置支持,通义千问也没有提供对 Semantic Kernel 的支持,但通义提供了兼容 OpenAI api 的 api,已经成功部署通义千问,详见博文 以容器方式部署通义千问 Qwen
Semantic Kernel 内置支持 OpenAI,如果能修改 Semantic Kernel 所请求的 OpenAI API url 就能以最简单的方式实现对通义千问的支持,请问如何实现?
参考 github 上 Allow Modifying the OpenAI endpoint 中的一个评论解决了
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("gpt-3.5-turbo", "***");
builder.Services.ConfigureHttpClientDefaults(b =>
b.ConfigurePrimaryHttpMessageHandler(() => new MyRedirectingHandler()));
var kernel = builder.Build();
var result = await kernel.InvokePromptAsync("博客园是什么网站");
Console.WriteLine(result);
sealed class MyRedirectingHandler() : DelegatingHandler(new HttpClientHandler())
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
request.RequestUri = new UriBuilder(request.RequestUri!) { Scheme = "http", Host = "localhost", Port = 8901 }.Uri;
return base.SendAsync(request, cancellationToken);
}
}