AI應用開發之路-準備:發起第2個開源小專案 SemanticKernel.DashScope

博客园团队發表於2024-03-13

上週我們釋出了與AI應用開發相關的第1個開源小專案 —— DashScope SDK for .NET,今天我們再釋出一個開源小專案 —— SemanticKernel.DashScope,今天這個專案才是主角,因為我們想基於 Semantic Kernel 開發大模型應用。

首先分享幾個與 Semantic Kernel 與 DashScope 相關的訊息:

  • Java 1.0 Release Candidate for Semantic Kernel 釋出了,SK 不僅是 .NET 版的 AI 應用開發框架,土豪的微軟正在同時開發三個技術平臺的版本 —— .NET, Java, Python ,為了AI,微軟也是夠拼。
  • DashScope 開始支援 function calling,2月22日我們提交工單諮詢阿里雲 DashScope 是否支援 function calling,阿里雲回覆說不支援,這周發現開始支援了,阿里雲的動作夠快。
  • 限時免費的通義千問系列大模型18號要開始收費了,我們現在使用的 qwen-max 就屬於限時免費款。

【阿里雲】靈積大模型服務平臺通義千問系列大模型將於2024年3月18日調整計費。請您檢視最新定價計費說明。檢視地址:https://t.aliyun.com/U/4ij1a1 。感謝您的使用

下面簡單介紹一下今天的主角,github 倉庫地址 https://github.com/cnblogs/semantic-kernel-dashscope

這個開源專案不僅實現了支援 Semantic Kernel 的 DashScope Connector,還實現了支援 Kernel Memory 的 DashScope Extension,所以分成了2個子專案:

  • SemanticKernel.DashScope:實現了3個介面 IChatCompletionServiceITextGenerationService 以及 ITextEmbeddingGenerationService,對應的 nuget 包是 Cnblogs.SemanticKernel.Connectors.DashScope
  • KernelMemory.DashScope:實現了2個介面 ITextEmbeddingGeneratorITextEmbeddingGenerator, 對應的 nuget 包是 Cnblogs.KernelMemory.AI.DashScope

下面透過簡單的示例程式碼快速體驗一下。

先體驗 Cnblogs.SemanticKernel.Connectors.DashScope

安裝 nuget 包

dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope

寫一個簡單的控制檯程式與通義千問 qwn-max 大模型進行對話

var builder = Kernel.CreateBuilder();
builder.Services.AddDashScopeChatCompletion(apiKey, "qwen-max");
var kernel = builder.Build();

var prompt = "部落格園是什麼樣網站,用簡練的語言回答";
var result = await kernel.InvokePromptAsync(prompt);
Console.WriteLine(result);

執行程式,通義千問的回答如下:

部落格園是一個面向軟體開發和技術愛好者的中文技術部落格平臺,使用者可以在該網站上建立、分享和交流程式設計知識、技術文章、行業動態等內容。它致力於為程式設計師及網際網路相關人員提供一個高質量的內容創作與分享社群,促進技術資訊的傳播與交流。

接著體驗 Cnblogs.KernelMemory.AI.DashScope

安裝 nuget 包

dotnet add package Microsoft.KernelMemory.Core
dotnet add package Cnblogs.KernelMemory.AI.DashScope

寫一個簡單的控制檯程式,使用園子的第一款簡陋滑鼠墊,是否是您值得擁有的周邊這篇博文進行 RAG(Retrieval Augmented Generation) 與通義千問大模型進行對話

var memory = new KernelMemoryBuilder()
    .WithDashScopeDefaults(apiKey)
    .Build<MemoryServerless>();

await memory.ImportWebPageAsync("https://www.cnblogs.com/cmt/p/17974346");

var question = "部落格園滑鼠墊在哪買";
var answer = await memory.AskAsync(question);
Console.WriteLine($"{answer.Result}");

上面的程式碼很簡單,但背後比較複雜,Kernel Memory 做了很多事情,比如 memory.ImportWebPageAsync 方法執行時完成了博文內容的抓取、內容分割(partition)、呼叫 DashScope api 生成 embedding 向量並儲存至向量資料庫,從下面的日誌可以看出來

info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Queueing upload of 1 files for further processing [request f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707]
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      File uploaded: content.url, 38 bytes
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Handler 'extract' processed pipeline 'default/f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707' successfully
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Handler 'partition' processed pipeline 'default/f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707' successfully
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Handler 'gen_embeddings' processed pipeline 'default/f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707' successfully
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Handler 'save_records' processed pipeline 'default/f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707' successfully
info: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0]
      Pipeline 'default/f3626f2e67ad451a959d056b4a9e3fbd202403130328152696707' complete

在透過 memory.AskAsync 方法傳送提示詞時,不僅包含使用者輸入的提示詞,而且會根據使用者輸入的提示詞呼叫 DashScope api 生成提示詞的 embedding 向量,並且用這個向量在向量資料庫中進行語義搜尋,將搜尋到的結果與使用者輸入的提示詞一起傳送給大模型(這就是RAG)。

通義千問 qwen-max 大模型訓練時並沒有關於部落格園滑鼠墊的資料,所以如果不進行 RAG,通義千問不會給出想要的答案。

有了 Kernel Memory,使用 RAG 變得很簡單,看看下面採用 RAG 的回答效果

部落格園滑鼠墊可以在淘寶上購買,具體購買方式有兩種:

1. 淘寶搜尋“部落格園”,找到相關店鋪和商品連結進行購買。
2. 直接透過提供的淘寶店購買連結購買:https://item.taobao.com/item.htm?id=761724714914

另外,如果不想在淘寶店購買,還可以選擇新增園子的企業微信進行購買。

對比一下,不採用 RAG,直接問通義千問

很抱歉,我無法提供最新、實時的購物資訊,包括部落格園是否售賣滑鼠墊以及購買途徑。部落格園(Cnblogs)是一個面向軟體開發者的IT技術部落格平臺,並非商品銷售網站,一般不會直接售賣滑鼠墊等實物商品。

如果您想購買滑鼠墊,可以嘗試在各大電商平臺如淘寶、京東、蘇寧易購等搜尋相關商品進行選購。

RAG的效果很明顯。

歡迎大家關注這個開源專案,歡迎反饋,歡迎提交 PR https://github.com/cnblogs/semantic-kernel-dashscope

接下來我們會嘗試實現對 function calling 的支援。

19:34 更新:DashScope SDK for .NET 現已支援 function calling

相關文章