練習 - 將提示儲存至檔案
假設您想要為使用者建議旅遊目的地和活動。 在此練習中,您會練習建立提示並將其儲存至檔案。 現在就開始吧!
開啟您在上一個練習中建立的 Visual Studio Code 專案。
在 Program.cs 檔案中,移除您在上一個練習中建立的
prompt
和input
變數,以便留下下列程式碼:using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Plugins.Core; var builder = Kernel.CreateBuilder(); builder.AddAzureOpenAIChatCompletion( "your-deployment-name", "your-endpoint", "your-api-key", "deployment-model"); var kernel = builder.Build();
確認專案中存在下列資料夾:
- 'Prompts'
- 'Prompts/TravelPlugins'
- 'Prompts/TravelPlugins/SuggestDestinations'
- 'Prompts/TravelPlugins/GetDestination'
- 'Prompts/TravelPlugins/SuggestActivities'
這些目錄將協助您組織提示。 首先,您建立一個提示來識別使用者想要前往的目的地。 若要建立提示,您必須建立 config.json 和 skprompt.txt 檔案。 現在就開始吧!
在 GetDestination 資料夾中,開啟 config.json 檔案,並輸入下列程式碼:
{ "schema": 1, "type": "completion", "description": "Identify the destination of the user's travel plans", "execution_settings": { "default": { "max_tokens": 1200, "temperature": 0 } }, "input_variables": [ { "name": "input", "description": "Text from the user that contains their travel destination", "required": true } ] }
此組態會告知核心您提示的用途,以及要接受哪些輸入變數。 接下來,您會在 skprompt.txt 檔案中提供提示文字。
在 GetDestination 資料夾中,開啟 skprompt.txt 檔案,然後輸入下列文字:
<message role="system"> Instructions: Identify the destination the user wants to travel to. </message> <message role="user"> I am so excited to take time off work! My partner and I are thinking about going to Santorini in Greece! I absolutely LOVE Greek food, I can't wait to be some place warm. </message> <message role="assistant">Santorini, Greece</message> <message role="user">{{$input}}</message>
此提示可協助大型語言模型 (LLM) 篩選使用者的輸入,並只從文字中擷取目的地。
在 SuggestDestinations 資料夾中,開啟 config.json 檔案,然後輸入下列文字:
{ "schema": 1, "type": "completion", "description": "Recommend travel destinations to the user", "execution_settings": { "default": { "max_tokens": 1200, "temperature": 0.3 } }, "input_variables": [ { "name": "input", "description": "Details about the user's travel plans", "required": true } ] }
在此設定中,您可以稍微提高溫度以使輸出更具創意。
在 SuggestDestinations 資料夾中,開啟 skprompt.txt 檔案,然後輸入下列文字:
The following is a conversation with an AI travel assistant. The assistant is helpful, creative, and very friendly. <message role="user">Can you give me some travel destination suggestions?</message> <message role="assistant">Of course! Do you have a budget or any specific activities in mind?</message> <message role="user">${input}</message>
此提示會根據使用者的輸入,向使用者建議旅遊目的地。 現在,讓我們建立外掛程式來建議目的地的活動。
在 SuggestActivities 資料夾中,開啟 config.json 檔案,然後輸入下列文字:
{ "schema": 1, "type": "completion", "description": "Recommend activities at a travel destination to the user", "execution_settings": { "default": { "max_tokens": 4000, "temperature": 0.3 } }, "input_variables": [ { "name": "history", "description": "Background information about the user", "required": true }, { "name": "destination", "description": "The user's travel destination", "required": true } ] }
在此設定中,您會增加
max_tokens
以允許更多文字用於記錄和產生文字。在 SuggestActivities 資料夾中,開啟 skprompt.txt 檔案,然後輸入下列文字:
You are a travel assistant. You are helpful, creative, and very friendly. Consider your previous conversation with the traveler: {{$history}} The traveler would like some activity recommendations, things to do, and points of interest for their trip. They want to go to {{$destination}}. Please provide them with a list of things they might like to do at their chosen destination.
現在讓我們匯入並測試新的提示!
使用下列程式碼來更新您的 Program.cs 檔案:
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Plugins.Core; using Microsoft.SemanticKernel.ChatCompletion; var builder = Kernel.CreateBuilder(); builder.AddAzureOpenAIChatCompletion( "your-deployment-name", "your-endpoint", "your-api-key", "deployment-model"); var kernel = builder.Build(); kernel.ImportPluginFromType<ConversationSummaryPlugin>(); var prompts = kernel.ImportPluginFromPromptDirectory("Prompts/TravelPlugins"); ChatHistory history = []; string input = @"I'm planning an anniversary trip with my spouse. We like hiking, mountains, and beaches. Our travel budget is $15000"; var result = await kernel.InvokeAsync<string>(prompts["SuggestDestinations"], new() {{ "input", input }}); Console.WriteLine(result); history.AddUserMessage(input); history.AddAssistantMessage(result);
在此程式碼中,您會匯入您所建立的外掛程式。 您也可以使用
ChatHistory
物件來儲存使用者的交談。 最後,您會將一些資訊傳遞給SuggestDestinations
提示,並記錄結果。 接下來,讓我們詢問使用者想要前往何處,以便我們可以向他們建議一些活動。新增以下程式碼至您的 Program.cs 檔案:
Console.WriteLine("Where would you like to go?"); input = Console.ReadLine(); result = await kernel.InvokeAsync<string>(prompts["SuggestActivities"], new() { { "history", history }, { "destination", input }, } ); Console.WriteLine(result);
在此程式碼中,您會從使用者獲得一些輸入,以找出他們想要前往的位置。 然後,您會使用目的地和交談記錄呼叫
SuggestActivities
提示。若要測試該程式碼,請在終端機中輸入
dotnet run
。最終輸入看起來可能類似下列內容:
Absolutely! Japan is a wonderful destination with so much to see and do. Here are some recommendations for activities and points of interest: 1. Visit Tokyo Tower: This iconic tower offers stunning views of the city and is a must-visit attraction. 2. Explore the temples of Kyoto: Kyoto is home to many beautiful temples, including the famous Kiyomizu-dera and Fushimi Inari-taisha. 3. Experience traditional Japanese culture: Attend a tea ceremony, try on a kimono, or take a calligraphy class to immerse yourself in Japanese culture.
現在您已建立了 AI 旅遊助理的開頭! 嘗試變更輸入,看看 LLM 會如何回應。