Exercise - Use personas in prompts

Completed

Assigning personas to your prompts can improve the quality of the responses generated by the large language model (LLM). Let's try it out!

  1. Open the Visual Studio Code project you created in the previous exercise.

  2. Update your prompt from the previous exercise with the following text:

    using Microsoft.SemanticKernel;
    using Microsoft.SemanticKernel.Plugins.Core;
    
    var builder = Kernel.CreateBuilder();
    builder.Services.AddAzureOpenAIChatCompletion(
        "your-resource-name",
        "your-endpoint",
        "your-resource-key",
        "deployment-model");
    
    var kernel = builder.Build();
    
    string language = "French";
    
    // 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. Run the code by entering dotnet run in the terminal.

    If you run the code, you might notice the responses are more consistent than your previous results. The LLM is more likely to generate a response that matches the persona you assigned and the context of the task.

    Your response may look similar to the following output:

    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!
    

You can also provide instructions to the LLM to assume a role when generating a response, and provide example requests and responses. In Semantic Kernel, a special syntax is used to define message roles. To define a message role, you can wrap the message in <message> tag with the role name as an attribute. The supported roles are 'user', 'system', 'assistant', and 'bot'. Let's try it out!

  1. Update your prompt with the following text:

    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>";
    

    Next, let's update the input to provide the AI with some details for the trip.

  2. Update the input string to the following text:

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

    Next, run the code and observe how the LLM responds.

  3. Enter dotnet run in the terminal.

    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!
    

    Notice how assigning a persona to the LLM allows you to create a more natural and personalized conversation.

You can also tune prompts to be less verbose and only output specific information. For example, suppose the user wants to get a list of flights from one destination to another. You can ask the LLM to parse their input and return only the relevant information in a format you can use in your code. Let's try it out!

  1. Update your prompt to the following text:

    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>";
    

    In this prompt, we use the <message> and also provide an example for the LLM. We want to format the output in a way that we can parse, so we provide that format in the example. Next, let's update the input to provide the AI with some details for the trip.

  2. Modify the input to the following text:

    string input = @"I have a vacation from June 1 to July 
    22. I want to go to Greece. I live in Chicago.";
    
  3. Run the code by entering dotnet run in the terminal.

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

    Notice how the LLM was able to parse the input and return only the relevant information. Prompting the LLM to parse data is a great way to quickly get the information you need from the user.

Important

Be sure not to delete any of the code you wrote so far, you need it for the next exercise.