【semantic Kernel】接入其他平台AI(智普、星火大模型)

Semantic Kernel默认主要支持两款模型:OpenAI和AzureOpenAI。对于开发者来说,这显然是不够的,尤其是当我们希望对接国内的一些强大模型,比如百度的文心一言、阿里的通义千问、搜狗的百川、智谱ChatGLM、科大讯飞的星火等。那么,我们应该怎么做呢?
Semantie Kernel中对话请求默认是发送到OpenAI去的

与OpenAI对话请求接口兼容的模型平台,一般只需要修改host、path,可以通过HttpClientHandler修改

接入智普AI

但是智谱AI的对话接口地址是api/paas/v4/chat/completions,和OpenAI不同,所以需要修改host和path
智普官网
安装Nuget:

<PackageReference Include="Microsoft.SemanticKernel" Version="1.25.0" />

代码:

//接入智普AI
var httpClient = new HttpClient(new ZhipuOpenAIHttpClientHandler());
var builder = builder.AddOpenAIChatCompletion(modelId: "glm-4",
    apiKey: "xxxx", httpClient: httpClient);

var kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

var history = new ChatHistory();
string? userInput;
do
{
    // Collect user input
    Console.Write("User > ");
    userInput = Console.ReadLine();

    // Add user input
    history.AddUserMessage(userInput);

    // Get the response from the AI
    var result = await chatCompletionService.GetChatMessageContentAsync(history, kernel: kernel);

    // Print the results
    Console.WriteLine("Assistant > " + result);

    // Add the message from the agent to the chat history
    history.AddMessage(result.Role, result.Content ?? string.Empty);
} while (userInput is not null);

使用HttpClientHandler修改host和path

public class ZhipuOpenAIHttpClientHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        switch (request.RequestUri?.LocalPath)
        {
            case "/v1/chat/completions":
                var uriBuilder = new UriBuilder(request.RequestUri)
                {
                    Scheme = "https",
                    Host = "open.bigmodel.cn",
                    Path = "api/paas/v4/chat/completions",//其他与OpenAI对话请求接口兼容的模型平台,一般只需要修改host即可,不需要修改path
                };
                request.RequestUri = uriBuilder.Uri;
                break;
        }

        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

        return response;
    }
}

运行程序:

接入星火大模型

星火大模型官网

//接入星火AI
var httpClient = new HttpClient(new XinghuoOpenAIHttpClientHandler());
var builder = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId: "generalv3.5",
    apiKey: "xxxx", httpClient: httpClient);
//其余代码同上

使用HttpClientHandler修改host

/// <summary>
/// 星火
/// </summary>
public class XinghuoOpenAIHttpClientHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        switch (request.RequestUri?.LocalPath)
        {
            case "/v1/chat/completions":
                var uriBuilder = new UriBuilder(request.RequestUri)
                {
                    Scheme = "https",
                    Host = "spark-api-open.xf-yun.com",
                };
                request.RequestUri = uriBuilder.Uri;
                break;
        }

        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

        return response;
    }
}
posted @ 2024-10-29 10:03  .Neterr  阅读(73)  评论(0编辑  收藏  举报