练习 - 在提示中使用角色
向提示分配角色可提高大型语言模型 (LLM) 生成的响应质量。 角色为 LLM 提供上下文,使它能够一致地生成更符合用户意图的响应。 让我们尝试一下吧!
打开在上一练习中创建的 Visual Studio Code 项目。
使用以下文本更新上一练习中的提示:
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(); string language = "French"; string history = @"I'm traveling with my kids and one of them has a peanut allergy."; // Assign a persona to the prompt string prompt = @$" You are a travel assistant. You are helpful, creative, and very friendly. Consider the traveler's background: ${history} Create a list of helpful phrases and words in ${language} a traveler would find useful. Group phrases by category. Include common direction words. Display the phrases in the following format: Hello - Ciao [chow] Begin with: 'Here are some phrases in ${language} you may find helpful:' and end with: 'I hope this helps you on your trip!'"; var result = await kernel.InvokePromptAsync(prompt); Console.WriteLine(result);
通过在终端中输入
dotnet run
来运行代码。如果运行代码,你可能会注意到响应比以前的结果更一致。 LLM 更有可能生成与所分配角色和任务上下文匹配的响应。
响应可能类似于以下输出:
Here are some phrases in French you may find helpful: Greetings: - Hello - Bonjour [bon-zhur] - Goodbye - Au revoir [oh ruh-vwar] - Thank you - Merci [mehr-see] Directions: - Go straight ahead - Allez tout droit [ah-lay too dwa] - Turn left/right - Tournez à gauche/droite [toor-nay ah gohsh/dwaht] - It's on the left/right - C'est à gauche/droite [say ah gohsh/dwaht] Food: - Does this contain peanuts? - Est-ce que cela contient des cacahuètes? [ess-kuh suh suh-la kohn-tee-eh day kah-kah-weht?] - My child has a peanut allergy - Mon enfant est allergique aux cacahuètes [mohn ahn-fahn ay ah-lair-gee-k oh kah-kah-weht] ... I hope this helps you on your trip!
还可向 LLM 提供在生成响应时扮演角色的说明,并提供示例请求和响应。 在语义内核中,特殊语法用于定义消息角色。 若要定义消息角色,可将消息包装在 <message>
标记中,并将角色名称作为属性。 支持的角色是“用户”、“系统”、“助手”和“机器人”。 让我们尝试一下吧!
使用以下文本更新提示:
string prompt = @$" 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>";
接下来,更新输入,为 AI 提供行程的一些详细信息。
将
input
字符串更新为以下文本:string input = @"I'm planning an anniversary trip with my spouse. We like hiking, mountains, and beaches. Our travel budget is $15000";
接下来,运行代码并观察 LLM 的响应方式。
在终端中输入
dotnet run
。That sounds like a great trip ahead! Here are a few suggestions: 1. New Zealand - With stunning mountain ranges, iconic hiking trails, and beautiful beaches, New Zealand is a popular destination for outdoor enthusiasts. Some must-visit spots include the Milford Track, Fox Glacier, and Abel Tasman National Park. 2. Hawaii - Known for its picturesque beaches, Hawaii is also home to several stunning hiking trails. The Kalalau Trail on Kauai is a popular trail that offers breathtaking views of the Na Pali Coast. 3. Costa Rica - Costa Rica boasts beautiful beaches and breathtaking mountains. Hike through the Monteverde Cloud Forest Reserve and catch a glimpse of exotic wildlife like the resplendent quetzal, or take a dip in the turquoise waters of Playa Manuel Antonio. 4. Banff National Park, Canada - Located in the Canadian Rockies, Banff National Park offers some of the most stunning mountain scenery in the world. Explore the park's many hiking trails, relax in hot springs, and take in the beauty of the Canadian wilderness. 5. Amalfi Coast, Italy - The Amalfi Coast is a picturesque stretch of coastline in Southern Italy that offers stunning views of the Mediterranean Sea. Take a hike along the famous Path of the Gods or enjoy a romantic stroll through one of the Amalfi Coast's charming towns like Positano or Ravello. These are just a few of many options, but with a budget of $15000, you should be able to have a fantastic trip to any of these destinations!
注意,向 LLM 分配角色如何让你创建更自然和个性化的对话。
还可优化提示,使其不那么详细,并且仅输出特定信息。 例如,假设用户想要获取从一个目的地到另一个目的地的航班列表。 可要求 LLM 分析其输入,并以你可在代码中使用的格式仅返回相关信息。 让我们尝试一下吧!
将提示更新为以下文本:
string prompt = @$" <message role=""system"">Instructions: Identify the from and to destinations and dates from the user's request</message> <message role=""user"">Can you give me a list of flights from Seattle to Tokyo? I want to travel from March 11 to March 18.</message> <message role=""assistant"">Seattle|Tokyo|03/11/2024|03/18/2024</message> <message role=""user"">${input}</message>";
在此提示中,使用
<message>
,还提供了一个 LLM 的示例。 我们希望以一种可分析的方式格式化输出,因此我们在示例中提供了这种格式。 接下来,更新input
,为 AI 提供行程的一些详细信息。将
input
修改为以下文本:string input = @"I have a vacation from June 1 to July 22. I want to go to Greece. I live in Chicago.";
通过在终端中输入
dotnet run
来运行代码。Chicago|Greece|06/01/2024|07/22/2024
注意 LLM 如何分析输入并仅返回相关信息。 提示 LLM 分析数据是快速从用户处获取所需信息的绝佳方法。
重要
务必不要删除目前为止编写的任何代码,你在下一个练习中需要它。