天天看點

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

作者:程式設計樂趣
我是程式設計樂趣,一個10年.Net開發經驗老程式員,點選右上方“關注”,每天為你分享開源項目和程式設計知識。

前言

通義千問:是阿裡推出的一個超大規模的語言模型,其中參數模型Qwen-72B已經宣布開源,同時還開源了18億參數模型Qwen-1.8B和音頻大模型Qwen-Audio,至此已經開源了18億、70億、140億、720億參數的4款大語言模型,以及視覺了解、音頻了解兩款多模态大模型。

Semantic Kernel:是微軟推出的開源項目,旨在将大型語言模型與應用程式內建,友善開發者建構一個更加智能、更加高效的應用。更多介紹可以看我之前分享《微軟官方出品:GPT大模型編排工具,支援C#、Python等多個語言版本》。

01

搭建通義千問

1、部署要求

python 3.8及以上版本 ;

pytorch 1.12及以上版本,推薦2.0及以上版本 ;

建議使用CUDA 11.4及以上(GPU使用者需考慮此選項);

使用CPU推理,記憶體建議32G以上;

使用GPU推理,顯存存建議24G以上;

建議使用Liunx伺服器。

2、下載下傳源碼

git clone https://github.com/QwenLM/Qwen-VL           
利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

下載下傳後的源碼如下:

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

其中源碼中openai_api.py,就是基于FastAPI模仿OpenAI接口的源碼。

3、安裝大模型依賴庫

進入源碼目錄,執行以下指令,安裝通義千問的依賴庫。

pip install -r requirements.txt           
利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

4、安裝fastapi相關依賴

執行以下指令,安裝依賴庫。

pip install fastapi uvicorn openai pydantic sse_starlette           
利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

5、啟動FastAPI

執行以下指令,啟動項目。

python3 openai_api.py           
利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

安裝成功後,我們在浏覽器輸入:http://127.0.0.1:8000,就可以檢視API文檔。

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

02

內建Semantic Kernel

1、建立一個控制台項目

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

2、安裝依賴

從NuGet安裝依賴庫:Microsoft.SemanticKernel。

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

3、添加通義千問擴充

為SemanticKernel添加通義千問擴充方法,代碼如下:

using Azure.AI.OpenAI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.TextGeneration;


namespace Chat2KnowL.Console
{
    public static class QwenServiceCollectionExtensions
    {
        /// <summary>
        /// 添加通義千問聊天服務
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="modelId"></param>
        /// <param name="apiKey"></param>
        /// <param name="url"></param>
        /// <param name="orgId"></param>
        /// <param name="serviceId"></param>
        /// <returns></returns>
        public static IKernelBuilder AddQwenChatCompletion(this IKernelBuilder builder, string modelId, string apiKey, string url, string? orgId = null, string? serviceId = null)
        {
            string modelId2 = modelId;
            string apiKey2 = apiKey;
            string orgId2 = orgId;


            Func<IServiceProvider, object, OpenAIChatCompletionService> implementationFactory = (IServiceProvider serviceProvider, object? _) => new OpenAIChatCompletionService(modelId, new OpenAIClient(new Uri(url), new Azure.AzureKeyCredential(apiKey)), serviceProvider.GetService<ILoggerFactory>());
            builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, IChatCompletionService>)implementationFactory);
            builder.Services.AddKeyedSingleton((object?)serviceId, (Func<IServiceProvider, object?, ITextGenerationService>)implementationFactory);
            return builder;
        }
    }
}           

4、SemanticKernel內建通義千問

SemanticKernel與通義千問內建,實作簡單的知識助手!

using Chat2KnowL.Console;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;


var builder = Kernel.CreateBuilder();


//通義千問
builder.AddQwenChatCompletion(
         "Qwen", "none", "http://127.0.0.1:8000/v1"
         );


var kernel = builder.Build();


//等待使用者輸入
Console.Write("使用者:");
var input = Console.ReadLine();


//循環對話
while (input != "quit")
{
    var prompt = @#34;<message role=""user"">{input}</message>";
    var summarize = kernel.CreateFunctionFromPrompt(prompt, executionSettings: new OpenAIPromptExecutionSettings { MaxTokens = 100, Temperature = 0.5 });
    Console.Write("知識助理:");
    var result =   kernel.InvokeStreamingAsync(summarize);
    await foreach (var item in result)
    {
        Console.Write(item.ToString());
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.Write("使用者:");
    input = Console.ReadLine();
}           

注意:

A、我們接口是模拟OpenAI的,是以我們修改符合OpenAI的接口位址;

B、開啟支援流請求。

修改openai_api.py中的接口:create_chat_completion,修改内容見紅色框。

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

另外如果是通義千問是部署在伺服器的,需要外部電腦通路的,還需要把--server-name參數的值改為0.0.0.0,或者在啟動項目的時候指定。

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

5、測試效果

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

03

最後

以上代碼我已經放在Github,開源項目位址:

https://github.com/bianchenglequ/chat2KnowL

也歡迎大家點點收藏!

利用阿裡通義千問和Semantic Kernel,10分鐘搭建知識助手!

該項目簡介:知識文檔問答工具,用大模型與文檔對話,提供Al分析、閱讀、問答工具,助你快速了解文檔内容。

大家有什麼問題,歡迎給我留言!

我是程式設計樂趣,一個10年.Net開發經驗老程式員,專注開源項目和程式設計知識分享。

私信回複:【888】,領取.Net視訊教程。

- End -

繼續閱讀