Semantic Kernel/C#:接入智譜AI的兩種方式

mingupupup發表於2024-08-21

Semantie Kernel中對話請求預設是傳送到OpenAI去的:

image-20240820135810583

其他與OpenAI對話請求介面兼任的模型平臺,一般只需要修改host即可,如下所示:

 default:
     uriBuilder = new UriBuilder(request.RequestUri)
     {
         // 這裡是你要修改的 URL
         Scheme = "https",
         Host = host,
         Path = "v1/chat/completions",
     };
     request.RequestUri = uriBuilder.Uri;
     break;

但是智譜AI的對話介面地址如下:

image-20240820140509016

在python中這樣就可以用,但SemanticKernel中好像還沒有base_url的設定。

有兩種方式可以實現。

一種是想和之前其他模型用相同的方式,把智普平臺作為一種特殊的方式處理。

在appsettings.json中新增一個Platform欄位,請求介面完全相容OpenAI的可以不寫:

image-20240820141319013

建立Kernel是這樣的:

 var builder = Kernel.CreateBuilder()
 .AddOpenAIChatCompletion(
    modelId: OpenAIOption.ChatModel,
    apiKey: OpenAIOption.Key,
    httpClient: new HttpClient(handler));
_kernel = builder.Build();

然後在OpenAIHttpClientHandler為智譜平臺做一下不同的處理:

public class OpenAIHttpClientHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        UriBuilder uriBuilder;
        string url = OpenAIOption.Endpoint;
        string platform = OpenAIOption.Platform;
        Uri uri = new Uri(url);
        string host = uri.Host;
        switch (request.RequestUri?.LocalPath)
        {
            case "/v1/chat/completions":
                switch(platform)
                {
                    case "ZhiPu":
                        uriBuilder = new UriBuilder(request.RequestUri)
                        {
                            // 這裡是你要修改的 URL
                            Scheme = "https",
                            Host = host,
                            Path = "api/paas/v4/chat/completions",
                        };
                        request.RequestUri = uriBuilder.Uri;
                        break;
                    default:
                        uriBuilder = new UriBuilder(request.RequestUri)
                        {
                            // 這裡是你要修改的 URL
                            Scheme = "https",
                            Host = host,
                            Path = "v1/chat/completions",
                        };
                        request.RequestUri = uriBuilder.Uri;
                        break;
                }
                break;
        }
    
        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
      
        return response;
    }
}

嘗試是否成功:

image-20240820141544371

另一種方式更加簡單,只需要在appsettings.json中這樣寫:

image-20240820141816259

使用這種方式建立Kernel即可:

image-20240820141925142

嘗試是否成功:

image-20240820142138323

在AvaloniaChat中為了和其他平臺保持統一的使用方式,我選擇第一種方式。

OpenAIHttpClientHandler可以在此處檢視:https://github.com/Ming-jiayou/AvaloniaChat/blob/main/src/AvaloniaChat/Model/OpenAIHttpClientHandler.cs

建立Kernel的兩種方式可以在此處檢視:https://github.com/Ming-jiayou/AvaloniaChat/blob/main/src/AvaloniaChat/ViewModels/MainViewModel.cs

相關文章