Monday, November 25, 2024

How few-shot learning with Google’s Prompt Poet can supercharge your LLMs

Must read


Join our daily and weekly newsletters for the latest updates and exclusive content on industry-leading AI coverage. Learn More


Prompt engineering, the discipline of crafting just the right input to a large language model (LLM) to get the desired response, is a critical new skill for the age of AI. It’s helpful for even casual users of conversational AI, but essential for builders of the next generation of AI-powered applications.

Enter Prompt Poet, the brainchild of Character.ai, a conversational LLM startup recently acquired by Google. Prompt Poet simplifies advanced prompt engineering by offering a user-friendly, low-code template system that manages context effectively and seamlessly integrates external data. This allows you to ground LLM-generated responses to a real-world data context, opening up a new horizon of AI interactions.

Prompt Poet shines for its seamless integration of “few-shot learning,” a powerful technique for rapid customization of LLMs without requiring complex and expensive model fine-tuning. This article explores how few-shot learning with Prompt Poet can be leveraged to deliver bespoke AI-driven interactions with ease and efficiency.

Could Prompt Poet be a glimpse into Google’s future approach to prompt engineering across Gemini and other AI products? This exciting potential is worth a closer look.

The Power of Few-Shot Learning

    In few-shot learning, we give the AI a handful of examples that illustrate the kind of responses we want for different possible prompts. In addition to a few ‘shots’ of how it should behave in similar scenarios.

    The beauty of few-shot learning is its efficiency. Model fine-tuning involves retraining a model on a new dataset, which can be computationally intensive, time-consuming, and costly, especially when working with large models. Few-shot learning, on the other hand, provides a small set of examples with the prompt to adjust the model’s behavior to a specific context. Even models that have been fine-tuned can benefit from few-shot learning to tailor their behavior to a more specific context.

    How Prompt Poet Makes Few-Shot Learning Accessible

      Prompt Poet shines in its ability to simplify the implementation of few-shot learning. By using YAML and Jinja2 templates, Prompt Poet allows you to create complex, dynamic prompts that incorporate few-shot examples directly into the prompt structure.

      To explore an example, suppose you want to develop a customer service chatbot for a retail business. Using Prompt Poet, you can easily include customer information such as order history and the status of any current orders, as well as information about current promotions and sales.

      But what about tone? Should it be more friendly and funny, or formal? More concise or informative? By including a “few shots” of successful examples, you can fine-tune the chatbot’s responses to match the distinct voice of each brand.

      Base Instruction

        The base instruction for the chatbot might be:

        - name: system instructions 
          role: system 
          content: | 
            You are a customer service chatbot for a retail site. Your job is to assist customers by answering their questions, providing helpful information, and resolving issues. Below you will be provided some example user inputs paired with responses that are desirable in terms of tone, style, and voice. Emulate these examples in your responses to the user.
        In these examples, placeholders marked with double question marks like '??placeholder??' will be used instead of real user data. After the examples, you'll be provided with real data about the user's current and past orders as a customer, which you must use faithfully in dealing with the user. 

        We can also provide the model with our real customer data, so that it can answer questions accurately about the user’s past and current orders. If appropriate we can also include current sales and promotions.

        - name: customer data
          role: system
          content: |    
            current orders:
            {% for order in current_orders %}
            - {{ order.description }}
            {% endfor %}
            Past orders:
            {% for order in past_orders %}
            - {{ order.description }}
            {% endfor %}
        
        - name: promotions
          role: system
          content: |    
            promotions:
            {% for promotion in promotions %}
            - {{ promotion.description }}
            {% endfor %}

        But first, we need to provide the examples of style, tone and voice that we promised the model in the base instruction.

        Setting the tone: friendly or formal?

        Some brands have a more casual, friendly and jocular tone, whereas others prefer a more refined elegance.

        For a brand that has a friendly and casual tone, the few-shot learning examples could look like this:

        - name: user prompt 
          role: user 
          content: | 
            Hi, I ordered ??product_name?? but haven't received it yet. What’s going on? 
        
        - name: response 
          role: assistant 
          content: | 
            Hey ??user_name??! Sorry for the hassle—let’s get this sorted out ASAP. ? I’m pulling up your order for ??product_name?? placed on ??order_date??. Hang tight while I check where it’s at! 
        
        - name: user prompt 
          role: user 
          content: | 
            I need to update my shipping address for an order I made today, it should be ??new_address?? 
        
        - name: response 
          role: assistant 
          content: | 
            Gotcha, ??user_name??! Let’s make sure your ??product_name?? gets to the right place. I’ll switch your shipping to ??new_address?? right away. All set—anything else I can do for you? 
        
        - name: user prompt 
          role: user 
          content: | 
            What’s your return policy? Not sure I’m going to love ??product_name??. 
        
        - name: response 
          role: assistant 
          content: | 
            No worries, ??user_name??! If ??product_name?? doesn’t knock your socks off, we’ve got a super easy return process. Just let me know, and I’ll help you get it sorted out. ?

        For a brand that emphasizes a more formal and professional tone, different answers can be provided to the very same prompts, guiding the model toward a different style:

        - name: user prompt 
          role: user 
          content: | 
            Hi, I ordered ??product_name?? but haven't received it yet. Can you assist? 
        
        - name: response 
          role: assistant 
          content: | 
            Thank you for reaching out, ??user_name??. I apologize for any inconvenience this may have caused. I will promptly review the status of your order for ??product_name?? placed on ??order_date?? and provide an update shortly. 
        
        - name: user prompt 
          role: user 
          content: | 
            I need to update my shipping address for an order I made today, it should be ??new_address?? 
        
        - name: response 
          role: assistant 
          content: | 
            Certainly, ??user_name??. I will update your shipping information to ??new_address?? for the order placed today. The changes have been successfully applied. Is there anything else I can assist you with? 
        
        - name: user prompt 
          role: user 
          content: | 
            Could you explain your return policy? I’m uncertain about keeping ??product_name??. 
        
        - name: response 
          role: assistant 
          content: | 
            Certainly, ??user_name??. If ??product_name?? does not meet your expectations, you may initiate a return within our specified timeframe. I will guide you through the process to ensure it is handled smoothly and efficiently.

        Bringing it together

        The code below uses Prompt Poet’s `Prompt` class to form a single, coherent prompt from the separate elements of the base instructions, the few-shot learning examples, and the actual user data. This allows us to invoke AI responses that are accurately informed and highly crafted in purpose and style.

        # User data
        user_past_orders = get_past_orders(user)
        user_current_orders = get_current_orders(user)
        promotions = get_promotions(user)
        
        template_data = {
            "past_orders": user_past_orders,
            "current_orders": user_current_orders,
            "promotions": promotions
        }
        
        # Create the prompt using Prompt Poet
        
        combined_template = base_instructions + few_shot_examples + customer_data
        
        prompt = Prompt(
            raw_template=combined_template,
            template_data=template_data
        )
        
        # Get response from OpenAI
        model_response = openai.ChatCompletion.create(
          model="gpt-4",
          messages=prompt.messages
        )

        Elevating AI with Prompt Poet

          Prompt Poet is more than just a tool for managing context in AI prompts—it’s a gateway to advanced prompt engineering techniques like few-shot learning. By making it easy to compose complex prompts with real data and the voice-customizing power of few-shot examples, Prompt Poet empowers you to create sophisticated AI applications that are informative as well as customized to your brand.

          As AI continues to evolve, mastering techniques like few-shot learning will be crucial for staying ahead of the curve. Prompt Poet can help you harness the full potential of LLMs, creating solutions that are powerful and practical.

Latest article