創建kernel
// Create a kernel with OpenAI chat completionKernel kernel = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId:"使用的模型id" ,apiKey: "APIKey").Build();
使用訊飛星火認知大模型的話,可以參考我這一篇文章:
記一次WPF集成SemanticKernel+OneAPI+訊飛星火認知大模型實踐 (qq.com)
使用gpt-3.5-turbo的效果
代碼:
// Example 1. Invoke the kernel with a prompt and display the resultDebug.WriteLine(await kernel.InvokePromptAsync("天空是什么顏色?"));Debug.WriteLine("-------------------------------------------------------");// Example 2. Invoke the kernel with a templated prompt and display the resultKernelArguments arguments = new() { { "topic", "sea" } };Debug.WriteLine(await kernel.InvokePromptAsync("這是什么顏色{{$topic}}?", arguments));Debug.WriteLine("-------------------------------------------------------");// Example 3. Invoke the kernel with a templated prompt and stream the results to the displayawait foreach (var update in kernel.InvokePromptStreamingAsync("這是什么顏色 {{$topic}}? 請提供一個詳細的解釋", arguments)){Debug.Write(update);}Debug.WriteLine("-------------------------------------------------------");// Example 4. Invoke the kernel with a templated prompt and execution settingsarguments = new(new OpenAIPromptExecutionSettings { MaxTokens = 500, Temperature = 0.5 }) { { "topic", "dogs" } };Debug.WriteLine(await kernel.InvokePromptAsync("告訴我關于這個主題的故事 {{$topic}}", arguments));// Example 5. Invoke the kernel with a templated prompt and execution settings configured to return JSON
#pragma warning disable SKEXP0013arguments = new(new OpenAIPromptExecutionSettings { ResponseFormat = "json_object" }) { { "topic", "chocolate" } };Debug.WriteLine(await kernel.InvokePromptAsync("給這種 {{$topic}} 的蛋糕用json格式制作一個食譜 ", arguments));
效果:
一個一個看:
// Example 1. Invoke the kernel with a prompt and display the resultDebug.WriteLine(await kernel.InvokePromptAsync("天空是什么顏色?"));Debug.WriteLine("-------------------------------------------------------");
回答:
天空的顏色通常是藍色的。這是因為大氣對太陽光的散射作用。在白天,空氣中的氣體和微粒會散射太陽光中的短波藍光,使我們看到天空是藍色的。在日落和日出時,由于光線穿過更多的大氣層,散射更多的波長,使得天空呈現出橙紅色。
// Example 2. Invoke the kernel with a templated prompt and display the resultKernelArguments arguments = new() { { "topic", "sea" } };Debug.WriteLine(await kernel.InvokePromptAsync("這是什么顏色{{$topic}}?", arguments));Debug.WriteLine("-------------------------------------------------------");
回答:
"Sea" 不是一種具體的顏色,而是指海洋。海洋的顏色通常取決于許多因素,例如水深、海底的顏色、水質等等,因此海洋的顏色可能是藍色、綠色、灰色等等不同的色調。如果你需要描述海洋的顏色,你可以根據具體情況選擇最貼切的顏色描述。
// Example 3. Invoke the kernel with a templated prompt and stream the results to the displayawait foreach (var update in kernel.InvokePromptStreamingAsync("這是什么顏色 {{$topic}}? 請提供一個詳細的解釋", arguments)){Debug.Write(update);}Debug.WriteLine("-------------------------------------------------------");
回答:
"Sea" 不是一種具體的顏色,因為海洋的顏色會隨著不同的地點和時間而變化。一般來說,海水的顏色可以是深藍色、淺藍色、綠色或者灰色等。這些顏色是由海水中的水質、深度、天空的顏色、陽光的角度等因素影響而形成的。深藍色的海水通常表示著水深和透明度很高,這種顏色在深海處很普遍。淺藍色的海水通常會出現在沿海地區,這可能是由于波浪的攪動導致的。而綠色的海水可能是由于海洋中浮游植物的存在所導致的。另外,灰色的海水可能是由于天氣陰沉或者海水中的懸浮物質造成的。綜上所述,海洋的顏色是多種因素綜合作用的結果,具有多樣性和變化性。
// Example 4. Invoke the kernel with a templated prompt and execution settingsarguments = new(new OpenAIPromptExecutionSettings { MaxTokens = 500, Temperature = 0.5 }) { { "topic", "dogs" } };Debug.WriteLine(await kernel.InvokePromptAsync("告訴我關于這個主題的故事 {{$topic}}", arguments));
回答:
《忠誠的狗》從前,有一個農夫,他有一只忠誠的狗,名叫小白。小白是農夫的得力助手,他每天都跟著農夫一起工作,保護莊稼,驅趕野獸。有一天,農夫生病了,病得很重,不能下地工作了。家里的存糧也所剩無幾。小白看著主人的病情一天比一天嚴重,他心里很著急,卻無計可施。一天,小白突然想到了一個主意。他決定去附近的城鎮尋找幫助。于是,小白離開了農場,開始了他的旅程。在城鎮上,小白遇到了一個慈祥的老人,老人看到小白的眼神中充滿了忠誠和堅定,便決定跟著小白回到了農場。小白回到農場后,老人開始幫助農夫種地、照顧莊稼,而小白則繼續保護莊稼,驅趕野獸。經過一段時間的努力,農夫的病情逐漸好轉,莊稼也長得茁壯。農夫感激不盡,他對小白和老人說:“你們是我最忠誠的朋友,我永遠不會忘記你們的幫助。”從此以后,小白、老人和
由于我們設置了MaxTokens = 500
,所以這個故事由于太長了而無法完整呈現。
// Example 5. Invoke the kernel with a templated prompt and execution settings configured to return JSON
#pragma warning disable SKEXP0013arguments = new(new OpenAIPromptExecutionSettings { ResponseFormat = "json_object" }) { { "topic", "chocolate" } };Debug.WriteLine(await kernel.InvokePromptAsync("給這種 {{$topic}} 的蛋糕用json格式制作一個食譜 ", arguments));
回答:
{"食譜名稱": "巧克力蛋糕","食材": {"巧克力": "200克","黃油": "150克","糖": "150克","雞蛋": "4個","面粉": "150克","泡打粉": "1茶匙","可可粉": "30克","牛奶": "100毫升"},"步驟": ["1. 將巧克力和黃油融化在鍋中,待涼。","2. 打蛋和糖,直到顏色變淺。","3. 加入巧克力混合物,攪拌均勻。","4. 篩入面粉、泡打粉和可可粉,攪拌均勻。","5. 慢慢倒入牛奶,攪拌成濃稠的面糊。","6. 將面糊倒入預熱至180°C的烤箱中,烤約30-35分鐘至熟。","7. 取出蛋糕,待涼后即可享用。"]
}
返回的是json格式。
使用訊飛星火認知大模型的效果
代碼與上面那個一樣。
效果:
一個一個看:
// Example 1. Invoke the kernel with a prompt and display the resultDebug.WriteLine(await kernel.InvokePromptAsync("天空是什么顏色?"));Debug.WriteLine("-------------------------------------------------------");
回答:
天空的顏色通常是藍色或灰色,但在日出和日落時會呈現出橙色、紅色或紫色等不同的顏色。這是因為太陽的光線在穿過大氣層時被散射,使得天空呈現出不同的色彩。
// Example 2. Invoke the kernel with a templated prompt and display the resultKernelArguments arguments = new() { { "topic", "sea" } };Debug.WriteLine(await kernel.InvokePromptAsync("這是什么顏色{{$topic}}?", arguments));Debug.WriteLine("-------------------------------------------------------");
回答:
由于您沒有提供足夠的信息,我無法確定您所提到的顏色。
// Example 3. Invoke the kernel with a templated prompt and stream the results to the displayawait foreach (var update in kernel.InvokePromptStreamingAsync("這是什么顏色 {{$topic}}? 請提供一個詳細的解釋", arguments)){Debug.Write(update);}Debug.WriteLine("-------------------------------------------------------");
回答:
海的顏色通常是藍色或綠色。這是因為海水吸收了太陽光中的紅色、黃色和橙色波長,而反射出藍色和綠色的波長。當太陽在天空中較低時,海水會呈現出更深的藍色,因為更多的紅色和黃色波長被散射掉了。此外,海洋的顏色還受到天氣、水質和深度等因素的影響。例如,當海水中含有大量的浮游生物或污染物時,它可能會呈現出混濁的綠色或棕色。
// Example 4. Invoke the kernel with a templated prompt and execution settingsarguments = new(new OpenAIPromptExecutionSettings { MaxTokens = 500, Temperature = 0.5 }) { { "topic", "dogs" } };Debug.WriteLine(await kernel.InvokePromptAsync("告訴我關于這個主題的故事 {{$topic}}", arguments));
回答:
從前,有一個小鎮,這個小鎮里住著許多善良的人們。他們非常熱愛動物,尤其是狗。在這個小鎮上,有一個特殊的節日,叫做“狗狗節”。每年的這個時候,小鎮的居民們都會為他們心愛的狗狗舉辦一場盛大的慶祝活動。在小鎮的東邊,有一戶人家,他們養了一只名叫旺財的金毛犬。旺財非常聰明,而且非常忠誠。它的主人小明和小紅非常喜歡旺財,把它當作家庭的一員。今年的狗狗節即將來臨,小明和小紅決定為旺財準備一份特別的禮物。他們想了很久,最后決定為旺財制作一頂漂亮的帽子。小明和小紅開始四處尋找材料,他們找到了一些彩色的絲帶、布料和紐扣。經過一番努力,他們終于為旺財制作了一頂獨一無二的帽子。狗狗節那天,整個小鎮都沉浸在歡樂的氛圍中。狗狗們都穿上了最漂亮的衣服,戴上了最時尚的配飾。小明和小紅帶著旺財來到了鎮上的廣場,那里已經聚集了許多人和狗狗。在廣場的一角,有一個舞臺,上面擺放著各種狗狗表演的道具。小明和小紅帶著旺財走上了舞臺,他們為旺財戴上了那頂漂亮的帽子。旺財立刻成為了眾人矚目的焦點,它昂首挺胸,神氣十足。接下來,旺財要參加一個狗狗才藝表演比賽。比賽開始了,旺財經過了一系列的考驗,展示了它的聰明才智和出色的表演能力。最后,旺財成功地完成了所有任務,贏得了比賽的冠軍。比賽結束后,主持人宣布了一個重要的消息:今年的狗狗節將評選出一位“最佳狗狗主人”,以表彰那些對狗狗無微不至的照顧和關愛。經過激烈的角逐,小明和小紅憑借他們對旺財的精心照料和無私奉獻,成功當選為“最佳狗狗主人”。頒獎典禮上,鎮長親自為小明和小紅頒發了獎杯和證書。他說:“你們是所有狗狗主人的榜樣,你們的愛心和付出讓我們看到了人與動物之間最美好的感情。”從那以后,小明和小紅更加珍惜與旺財相處的時光。他們知道,狗狗不僅僅是寵物,更是家庭的一部分,是他們生活中不可或缺的伙伴。而那個小鎮上的人們也更加關愛動物,讓每一個狗狗都能過上幸福快樂的生活。
// Example 5. Invoke the kernel with a templated prompt and execution settings configured to return JSON
#pragma warning disable SKEXP0013arguments = new(new OpenAIPromptExecutionSettings { ResponseFormat = "json_object" }) { { "topic", "chocolate" } };Debug.WriteLine(await kernel.InvokePromptAsync("給這種 {{$topic}} 的蛋糕用json格式制作一個食譜 ", arguments));
回答:
{"recipe": {"name": "Chocolate Cake","ingredients": [{"item": "flour","quantity": "2 cups"},{"item": "cocoa powder","quantity": "1 cup"},{"item": "baking powder","quantity": "1.5 teaspoons"},{"item": "salt","quantity": "1/2 teaspoon"},{"item": "sugar","quantity": "1.5 cups"},{"item": "butter","quantity": "1 cup"},{"item": "eggs","quantity": "2"},{"item": "vanilla extract","quantity": "2 teaspoons"},{"item": "boiling water","quantity": "1 cup"}],"instructions": ["Preheat the oven to 350°F (175°C).","Grease and flour two 9-inch round cake pans.","In a large bowl, mix together the flour, cocoa powder, baking powder, and salt.","In another bowl, cream together the butter and sugar until light and fluffy. Beat in the eggs one at a time, then stir in the vanilla.","Gradually blend the dry ingredients into the wet ingredients. Beat in the boiling water last. The batter will be thin.","Pour the batter evenly into the prepared pans.","Bake for 30 to 35 minutes, or until a toothpick inserted into the center of the cake comes out clean.","Allow the cakes to cool in the pans for 10 minutes, then remove from the pans and cool completely on wire racks."]}
}
返回的是json格式。
參考
semantic-kernel/dotnet/samples/KernelSyntaxExamples at main · microsoft/semantic-kernel (github.com)
推薦閱讀:
記一次WPF集成SemanticKernel+OneAPI+訊飛星火認知大模型實踐 (qq.com)