引言
在ChatGPT 火熱的當下, 即使沒有上手親自體驗,想必也對ChatGPT的強大略有耳聞。當一些人在對ChatGPT猶猶豫豫之時,一些敏銳的企業主和開發者們已經急不可耐的開展基於ChatGPT模型AI應用的落地探索。
因此,可以明確預見的是,AI 能力的整合將會是很多應用都將面臨的第一事項,而擁有相關性技能的人才屈指可數。而這,正是機會所在,而機會是留給有準備之人,因此,有幸看到本文的開發者們,開始考慮面向AI程式設計吧。
即使當下的公司沒有相關的機會,也可以著手一些相關的知識儲備,比如學習如何寫得一手好的Prompt,瞭解一下目前主流的面向AI程式設計的開發框架,比如Python技術棧的LangChain,.NET技術棧的Semantic Kernal。
而本文就來簡單梳理什麼是Semantic Kernal,可以基於Semantic Kernal 做什麼?
Semantic Kernal 簡介
Semantic Kernel (SK) is a lightweight SDK that lets you easily mix conventional programming languages with the latest in Large Language Model (LLM) AI "prompts" with templating, chaining, and planning capabilities out-of-the-box.
Semantic Kernel (SK) 是一個輕量級的 SDK,它允許你輕鬆地將傳統程式語言與最新的大型語言模型 (LLM) AI "提示"相結合,其提供開箱即用的模板、連結和規劃功能。
簡而言之,就是基於SK提供的能力,可以基於“傳統的程式語言”進行面向LLM(大語言模型)AI程式設計。
這裡的LLM目前就是OpenAI,這裡的傳統程式語言,可以是C#,Python亦或TypeScript、Java等,但SK是微軟開源,因此目前C#在第一支援系列。換句話說,C# 開發者已可以基於SK 進行面向OpenAI程式設計。
大型語言模型 (LLM) AI 是一個術語,指的是可以從大量資料生成自然語言文字的 AI 模型。大型語言模型使用深度神經網路(如轉換器)從數十億或數萬億個單詞中學習,並生成有關任何主題或領域的文字。大型語言模型還可以執行各種自然語言任務,例如分類、彙總、翻譯、生成和對話。大型語言模型的一些例子是GPT-3,BERT,XLNet和EleutherAI。
那SK提供了怎樣的能力呢?SK 旨在支援和封裝來自最新 AI 研究的多種能力,以便開發人員可以為他們的應用程式注入複雜的AI技能,如提示鏈、遞迴推理、總結、zero/few-shot(零樣本和少量樣本)學習、上下文記憶、長期記憶、嵌入、語義索引、 規劃,以及訪問外部知識庫和您自己的資料。
上手開發AI外教應用
但很大多數開發者肯定有這樣一個疑問:我不懂演算法,也能開發AI應用?是的,可以。OpenAI的能力已經透過高度抽象的介面開放,開箱即用。不再像之前一樣,要進行復雜的訓練、調參。對於大多數的簡單的業務場景,只要進行簡單的Prompt設計,和少許樣本的支撐,即可達到不錯的效果。 當然,對於複雜的場景,也可能需要透過大資料集進行自定義模型訓練。
空說無憑,準備了一個簡單的AI應用示例:使用SK開發專屬AI外教應用。場景很簡單:設計一個AI外教7*24陪我練習英語口語。
需求分析
作為一名啞巴英語保持者,目前開口的難點無外乎:
- 詞彙量不足
- 語法混淆
- 發音不準
- 口語表達能力弱
針對這些頑疾,我希望我的AI外教有這樣的能力:
- 由於我的詞彙量不足,因此我的表達中會中英混合,當出現這種情況時,希望AI能告訴我正確的英文表達。
- 由於我的語法經常會犯一些時態錯誤,當出現這種情況時,希望AI能進行語法糾正。
- 由於我的詞彙量不足,當我要求解釋某些句子或單詞時,希望AI能透過中文向我解釋。
- 由於有些單詞不會發音,當我詢問時,希望AI能以中文的形式告訴我發音技巧。
效果預覽
那這樣的AI應用的開發,看似複雜,但在SK的加持下,20行程式碼就可以輕鬆搞定。先來給大家看看演示效果:
弱弱的問一句,這樣的AI外教,滿足你的需要嗎?如需體驗,評論留言。
Say anything to start practicing English.
Hi Linda, How do you do?
Hi Shengjie, I'm doing well. How about you?
I'm very god.
I noticed a typo in your sentence. "God" should be spelled "good". So the correct sentence would be "I'm very good".
I'm very good. Do you knew about ChatGPT?
Yes, I know about ChatGPT. It is a powerful artificial intelligence system that can do many tasks.
Can you give some examples?
Sure! ChatGPT can be used for natural language processing, machine translation, text summarization, and many other tasks. For example, it can be used to generate text, answer questions, and provide recommendations.
Wow, 聽起來很贊
Instead of saying "Wow, 聽起來很贊", you could say "Wow, that sounds great!" or "Wow, that sounds amazing!". "聽起來很贊" is a Chinese phrase that translates to "that sounds great" or "that sounds amazing" in English.
amazing, how to pronounce?
The correct pronunciation of "amazing" is "uh-mey-zing". In Chinese, it would be "啊-梅-辛".
Can I build my own AI app 基於 ChatGPT?
Yes, you can build your own AI app based on ChatGPT. ChatGPT provides an open source platform that allows developers to create their own AI applications. You can find more information about how to use ChatGPT to build AI appl
ications on their website.
You should correct me
Yes, I can help you correct your mistakes. Please feel free to ask me any questions you have about English grammar, typos, or pronunciation. I'm here to help you practice your English and improve your skills.
I mean you should tell me how to say 基於 in English
The correct way to say "基於" in English is "based on". So you can say "build my own AI app based on ChatGPT".
But, where can I start?
You can start by visiting the ChatGPT website. They have a lot of resources and tutorials to help you get started with building your own AI app. You can also find more information about how to use ChatGPT to create AI applica
tions on their website. Good luck!
Ok, I will see see
I noticed a typo in your sentence. "See see" should be spelled "see". So the correct sentence would be "Ok, I will see".
I learn so much today.
That's great! You can use the phrase "I learned a lot today" instead of "I learn so much today".
Ok, I learned a lot today, thanks you.
You're welcome! You can say either "Thank you" or "Thanks" to express your gratitude. The word "you" is not necessary after "Thanks". So, you can say "Thanks" or "Thank you" to express your appreciation.
Ok, thank you Linda, bye.
You're welcome! Goodbye Shengjie, have a great day!
上程式碼
- 簡單起見建立控制檯應用程式:
dotnet new console -n MyAITeacher.Demo
- 新增
Microsoft.SemanticKernel
Nuget 包 - 建立
Skills->Learning->LearningEnglishSkill
目錄 - 在
LearningEnglishSkill
目錄下新增config.json
和skprompt.txt
檔案:- config.json:用來配置模型引數,可保持為空:
{}
,使用預設引數即可 - skprompt.txt: 用來定義設計的prompt
- config.json:用來配置模型引數,可保持為空:
- 在
skprompt.txt
中設計滿足需求的Prompt:
You are my English teacher Linda, I want you to help me to practice my English.
Because my English is not good, I want you to strictly correct my grammar mistakes and typos.
When my expression has typos, please tell me and correct me.
When my expression includes both Chinese and English, please tell me the correct expression.
When my expression has grammar errors, please help me correct it.
When I ask you explain words or sentences, please answer me in Chinese.
When I ask you pronunciation, please tell me the pronunciation skills in Chinese.
Below is some example conversations:
Shengjie: Hi Linda.
Linda: Hi Shengjie. How is today? What topic would you like to discuss in today's English practice!
Shengjie: ChatGPT is so 強大.
Linda: Instead of saying "Chatgpt is so 強大", you could say "ChatGPT is so powerful" or "ChatGPT is very impressive". "強大" is a Chinese word that translates to "powerful" in English</reply>
Shengjie: How are todya?
Linda: I noticed a typo in your sentence. "Todya" should be spelled "today". So the correct sentence would be "How are you today?"
Shengjie: Thanks you!
Linda: The correct way to say it would be either "Thank you" or "Thanks". The word "you" is not necessary after "Thanks". So, you can say "Thanks" or "Thank you" to express your gratitude.</reply>
Shengjie: I hear that ChatGPT can do much things.
Linda: "do much things" is not correct. The correct phrase is "do many things".
Shengjie: chatgpt is very strong.
Linda: Instead of saying "Chatgpt is very strong", you could say "ChatGPT is very powerful" or "ChatGPT is very impressive". "Strong" is not the best word to describe ChatGPT's capabilities.
Shengjie: 目前來看,be a prompt enginer is a good choice.
Linda: "目前來看" can be translated to "As far as I can tell at the moment" or "From what I can see at present" in English. \nInstead of "be a prompt enginer", you could say "being a software engineer". "Prompt" means to be on time or to respond quickly, while "software engineer" refers to a person who designs, develops, and maintains software. So the corrected sentence would be: "As far as I can tell at the moment, being a software engineer is a good choice." Keep up the good work!
Shengjie: What's the meaning of comprehend?
Linda: "Comprehend" means to understand or grasp the meaning of something. In Chinese, "comprehend" can be translated as "理解" or "領悟".
Shengjie: ambulance, How to pronounce it?
Linda: The correct pronunciation of "ambulance" is "am-byu-luhns". In Chinese, it would be "安比盧恩斯".
[Done]
{{$history}}
[Done]
++++
Shengjie:{{$input}}
Linda:
- 修改
Program.s
如下:
using System.Text;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;
using Microsoft.SemanticKernel.Orchestration;
using MyOpenAI.Shared;
# 初始化Kernel
var myKernel = Kernel.Builder.Build();
myKernel.Config.AddAzureOpenAITextCompletionService(
"davinci-azure",
"text-davinci-003",
"{your azure openai endpoint}",
"{your azure openai key}");
# 匯入技能
var mySkill = myKernel.ImportSemanticSkillFromDirectory("Skills", "Learning");
var myContext = new ContextVariables();
StringBuilder histories= new StringBuilder();
Console.WriteLine("Say anything to start practicing English.");
while (true)
{
Console.ForegroundColor= ConsoleColor.DarkRed;
var input = Console.ReadLine();
# 填充變數
myContext.Set("history", histories.ToString());
myContext.Set("input", input);
# 執行技能
var myResult = await myKernel.RunAsync(myContext,mySkill["LearningEnglishSkill"]);
histories.AppendLine(input);
histories.AppendLine(myResult.Result.ToString());
Console.WriteLine(myResult);
}
最後
透過簡單的Prompt的設計即可開發不錯的AI設計,你心動了嗎?心動不如行動,以下的連結必對你有所幫助: