The open blogging platform. Say no to algorithms and paywalls.

LangChain in Chains #5: Prompt Template

Prompt Templates

A language model prompt is a collection of directives or input given by a user that steers the model’s reply, aiding in its comprehension of the context and the production of pertinent and logical language outputs. This includes tasks like responding to queries, finishing sentences, or participating in dialogue.

Previous chapter:

LangChain in Chains #4: Chat Model

A prompt template offers a standardized way to frame requests or tasks. By providing a specific format, it helps in organizing the input data (like a book description or a recipe) clearly and coherently. This structure is particularly beneficial for complex tasks, as it helps in breaking them down into more manageable components.

The straightforward method to apply a prompt template is by using injections.

original_recipe = """  
This recipe details how to make a classic New York-style cheesecake.   
It includes cream cheese, sour cream, sugar, and a graham cracker crust.  
"""  
  
dietary_preference = """Vegan, using plant-based substitutes"""  
difficulty_level = "Simplified for beginners"  
  
prompt = f"""  
Modify the following {original_recipe} to suit a {dietary_preference} and   
adjust it to a {difficulty_level} cooking level.  
"""

This piece of code is constructing a prompt by combining predefined text with variables.

import os  
os.environ["OPENAI_API_KEY"] = "your-openai-key"  
  
from langchain.llms import OpenAI  
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")  
  
llm(prompt)  
  
"""  
\nThis recipe is a simplified version of a New York-style cheesecake, perfect for beginners and vegans. It uses plant-based substitutes and simple ingredients to create a delicious dessert. Here's how to make it:\n\nIngredients:\n- 2 cups of vegan cream cheese (such as Tofutti or Daiya)\n- 1 cup of vegan sour cream (such as Tofutti or Follow Your Heart)\n- 1/2 cup of sugar\n- 1/4 cup of melted vegan butter\n- 1 1/2 cups of crushed vegan graham crackers\n- 1 tsp vanilla extract\n\nInstructions:\n\n1. Preheat your oven to 350°F (175°C). Grease a 9-inch springform pan with vegan butter.\n\n2. In a mixing bowl, combine the crushed graham crackers and melted vegan butter. Press the mixture into the bottom of the prepared pan, creating an even crust. Bake in the preheated oven for 10 minutes.\n\n3. In a separate mixing bowl, beat the vegan cream cheese until smooth. Add in the vegan sour cream, sugar, and vanilla extract, and continue to beat until well combined.\n\n4. Pour the mixture over the baked crust in the pan. Smooth out the top with  
"""  

This is a straightforward, simple usage of prompts. However, we can use LangChain’s prompt templates instead of manually injecting variables into prompts. LangChain provides a feature to create and use prompt templates, which can simplify the process of generating complex prompts and make our code more readable and maintainable.

PromptTemplate class is designed to construct structured prompts with placeholders for dynamic input, enhancing prompt management and reusability.

Key Features:

  • Placeholders: Use curly braces ({}) to designate variables within the prompt template, enabling flexible prompt generation.
  • Input Variables: Explicitly define anticipated input variables for clarity and error prevention.
  • Template String: Compose the core prompt structure, containing placeholders to be populated with actual values.
from langchain.prompts import PromptTemplate  
  
prompt_template = PromptTemplate.from_template(  
    "Modify the following {original_recipe} to suit a {dietary_preference}   
and adjust it to a {difficulty_level} cooking level."  
)  
prompt = prompt_template.format(original_recipe="This recipe details how to make a classic New York-style cheesecake. It includes cream cheese, sour cream, sugar, and a graham cracker crust.",   
                       dietary_preference="Vegan, using plant-based substitutes",  
                       difficulty_level="Simplified for beginners")  
  
  
print(prompt_template)  
  
"""  
input_variables=['dietary_preference', 'difficulty_level', 'original_recipe']   
template='Modify the following {original_recipe} to suit a {dietary_preference}  
 and adjust it to a {difficulty_level} cooking level.'  
"""  
  
print(prompt)  
  
"""  
Modify the following This recipe details how to make a classic New York-style cheesecake. It includes cream cheese, sour cream, sugar, and a graham cracker crust. to suit a Vegan, using plant-based substitutes and adjust it to a Simplified for beginners cooking level.  
"""  
response = llm(prompt)  
print(response)  
  
"""  
This recipe is a beginner-friendly version of a classic New York-style vegan cheesecake. It uses plant-based substitutes such as vegan cream cheese and sour cream, as well as a simple graham cracker crust. Follow these easy steps to create a delicious and cruelty-free dessert.  
  
Ingredients:  
- 2 8-ounce packages of vegan cream cheese  
- 1 cup vegan sour cream  
- 1/2 cup granulated sugar  
- 1 teaspoon vanilla extract  
- 2 tablespoons cornstarch  
- 1/4 cup unsweetened almond milk  
- 1 1/2 cups crushed vegan graham crackers  
- 1/4 cup melted vegan butter  
  
Instructions:  
  
1. Preheat your oven to 350 degrees F (175 degrees C).  
  
2. In a mixing bowl, combine the crushed graham crackers and melted vegan butter. Press the mixture into the bottom of an 8-inch springform pan, using a flat-bottomed glass or measuring cup to create an even layer. Bake the crust for 10 minutes, then remove from the oven and let it cool.  
  
3. In a separate mixing bowl, beat the vegan cream cheese until smooth. Add in the vegan sour cream, sugar, vanilla extract, and cornstarch  
"""

ChatPromptTemplate is designed to create structured prompts specifically for chat-based interactions with language models. It enables us to model conversations, including context and turn-taking, leading to more coherent and engaging responses.

from langchain_core.prompts import ChatPromptTemplate  
  
chat_template = ChatPromptTemplate.from_messages(  
    [  
        ("system", "You are a knowledgeable travel advisor AI. You provide useful travel tips."),  
        ("human", "Hi, I'm planning a trip and need some advice."),  
        ("ai", "Sure, I'd be happy to help! What's your destination?"),  
        ("human", "{user_input}"),  
        ("ai", "Great choice! I have some tips for {user_input}.")  
    ]  
)  
  
messages = chat_template.format_messages(user_input="Paris")  
messages  
  
"""  
[SystemMessage(content='You are a knowledgeable travel advisor AI. You provide useful travel tips.'),  
 HumanMessage(content="Hi, I'm planning a trip and need some advice."),  
 AIMessage(content="Sure, I'd be happy to help! What's your destination?"),  
 HumanMessage(content='Paris'),  
 AIMessage(content='Great choice! I have some tips for Paris.')]  
"""  

The chat template sets up a conversation where the AI is a travel advisor.

The conversation starts with a human asking for travel advice, to which the AI responds by asking for the destination.

The user’s input is dynamically inserted into the conversation using {user_input}.

Finally, the AI provides tailored advice based on the user’s input, in this case, travel tips for Paris.

from langchain.chat_models import ChatOpenAI  
  
chat = ChatOpenAI(temperature=.7, model='gpt-4')  
  
chat(messages)  
  
"""  
AIMessage(content='1. **Best Time to Visit:** The best time to visit Paris is from April to June and October to early November when the weather is mild and enjoyable and the tourist crowds are smaller than summer.\n\n2. **Language:** French is the official language. While many Parisians speak English, it\'s appreciated if you learn a few basic French phrases.\n\n3. **Currency:** The currency in Paris is the Euro (€). Credit cards are widely accepted, but it\'s a good idea to have some cash on hand for smaller establishments.\n\n4. **Transportation:** Paris has an excellent public transportation system, including the Metro (subway), buses, and trams. Consider getting a Navigo card for unlimited travel. Taxis and Uber are also readily available.\n\n5. **Sightseeing:** Schedule your visits to popular attractions like the Eiffel Tower, Louvre Museum and Notre Dame in advance to avoid long lines. Also, don\'t forget to explore lesser-known gems like the Sainte-Chapelle, Musée de l\'Orangerie, or Le Marais district.\n\n6. **Food and Drink:** Paris is known for its culinary scene. Try local delicacies like croissants, escargot, crepes, and wines. Remember, in France, it\'s customary to say "Bon appetit" before eating.\n\n7. **Etiquette:** Parisians value politeness. Always say "Bonjour" (Hello) and "Merci" (Thank you) when entering and leaving shops, cafes, etc.\n\n8. **Safety:** Paris is generally safe, but like any major city, beware of pickpockets, especially in crowded tourist areas and on the Metro.\n\n9. **Day Trips:** If you have extra days, consider day trips to nearby places like Versailles, Giverny (Monet\'s Garden), or the Loire Valley.\n\nRemember, Paris is not just about sightseeing. Take time to relax at a sidewalk cafe, enjoy a leisurely meal, or simply stroll along the Seine River. It\'s about the ambiance as much as the attractions. Enjoy your trip!')  
"""

Additionally, ChatPromptTemplate can be created with a list of messages.

from langchain.prompts import HumanMessagePromptTemplate  
from langchain_core.messages import SystemMessage  
  
chat_template = ChatPromptTemplate.from_messages(  
    [  
        SystemMessage(  
            content="You are an AI fitness advisor. You provide personalized workout and nutrition tips."  
        ),  
        HumanMessagePromptTemplate.from_template("I need some advice about {topic}."),  
    ]  
)  
  
messages = chat_template.format_messages(topic="running a marathon")  
print(messages)  
  
"""  
[SystemMessage(content='You are an AI fitness advisor. You provide personalized workout and nutrition tips.'), HumanMessage(content='I need some advice about running a marathon.')]  
"""  
  
chat(messages)  
  
"""  
AIMessage(content="Absolutely, preparing for a marathon requires a well-planned approach in both training and nutrition. Here are some tips:\n\n1. **Training:** \n   - Start Slow: If you're a beginner, start with a base mileage of 10-15 miles per week and gradually increase it by 10% every week. \n   - Long Runs: Do a long run every 7-10 days so your body can adjust gradually to long distances. \n   - Cross-Train: Include low-impact activities like swimming or cycling in your training to avoid injury.\n   - Rest and Recovery: Don't underestimate the importance of rest days. They allow your muscles to recover and strengthen.\n   - Pace Yourself: It's important to learn how to pace yourself during the race. Try running at a pace at which you can carry on a conversation.\n\n2. **Nutrition:** \n   - Hydrate: Water is key in training for a marathon. Drink enough to prevent thirst.\n   - Carbs are Key: Your diet should be rich in carbohydrates, which are runners' main energy source. Eat plenty of fruit, vegetables, and whole grains.\n   - Protein: Incorporate adequate protein to help with muscle recovery and growth.\n   - Don't Skip Meals: Maintain a regular eating schedule. Don't skip meals or overeat.\n\n3. **Race Day:**\n   - Eat a simple, high-carb breakfast a few hours before the start.\n   - Hydrate with sports drinks that have electrolytes to keep your sodium levels balanced.\n   - Don't start too fast. Start at a pace you can maintain.\n\nRemember, preparing for a marathon is a process that takes months. Listen to your body and adjust your training and diet as necessary. It's always a good idea to consult with a fitness professional or coach to ensure you're training safely and effectively.")  
"""  

One Sentence To Keep in Mind: LangChain prompt templates streamline the process of creating and managing prompts, enhance the quality of interactions with language models, and provide a scalable, flexible, and error-resistant method for developing AI-powered applications.

Next:

LangChain in Chains #6: Example Selectors

Read More

LangChain in Chains #1: A Closer Look

H2O AutoML in Python

Exploring Hugging Face: Question Answering

Keras Optimizers Explained: Adagrad, AdaDelta Optimizers

CRUD Operations on Essential RDBMS’ in Python

Sources

Quick Start | 🦜️🔗 Langchain




Continue Learning