練習 - 在提示中使用角色

已完成

將角色指派給您的提示可以改善大型語言模型 (LLM) 所產生的回應品質。 角色能為 LLM 提供內容,使其能夠一致地產生更符合使用者意圖的回應。 讓我們試試看!

  1. 開啟您在上一個練習中建立的 Visual Studio Code 專案。

  2. 使用下列文字更新上一個練習中的提示:

    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);
    
  3. 在終端中輸入 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 在產生回應時扮演某種角色,並提供要求和回應範例。 在 Semantic Kernel 中,會使用特殊語法來定義訊息角色。 若要定義訊息角色,您可以將訊息包裝在標籤中 <message>,並將角色名稱當做屬性。 支援的角色為「使用者」、「系統」、「助理」和「聊天機器人」。 讓我們試試看!

  1. 使用下列文字更新您的提示:

    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 提供行程的一些詳細資料。

  2. input 字串更新為下列文字:

    string input = @"I'm planning an anniversary trip with my spouse. We like hiking, mountains, 
        and beaches. Our travel budget is $15000";
    

    接下來,執行程式碼並觀察 LLM 的回應方式。

  3. 在終端機中輸入 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 剖析其輸入,並只傳回可在程式碼中使用的格式相關資訊。 讓我們試試看!

  1. 將提示更新為下列文字:

    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 提供一些旅行詳細資料。

  2. input 修改為下列文字:

    string input = @"I have a vacation from June 1 to July 22. I want to go to Greece. 
        I live in Chicago.";
    
  3. 在終端中輸入 dotnet run,以執行程式碼。

    Chicago|Greece|06/01/2024|07/22/2024
    

    請注意 LLM 如何能夠剖析輸入,並只傳回相關資訊。 提示 LLM 剖析資料是快速從使用者取得所需資訊的絕佳方式。

重要

請勿刪除您到目前為止撰寫的任何程式碼,因為下一個練習需要這些程式碼。