练习 - 将提示保存到文件
假设你要为用户推荐旅行目的地和活动。 在本练习中,你将练习创建提示并将其保存到文件。 现在就开始吧!
打开在上一练习中创建的 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 的响应方式。