Predictive Hacks

Get Started with Langchain Prompt Templates

In the majority of cases, LLM applications don’t directly input user input into an LLM. Instead, they utilize a larger piece of text known as a “prompt template” to include the user input along with additional context related to the specific task. They encapsulate all the necessary logic to transform user input into a fully formatted prompt. Let’s start with some prompt templates:

Single Input Prompt

from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate

llm = OpenAI(model_name='text-davinci-003')
chat = ChatOpenAI()


single_input_prompt = PromptTemplate(input_variables = ['product'],
                                template = 'What is a good name for a company that makes {product}?')


single_input_prompt.format(product='colorful socks')
 
'What is a good name for a company that makes colorful socks?'
print(llm(single_input_prompt.format(product='colorful socks')))
Socktastic!

Multi-Input Prompt

We can easily add more parameters as follows:

multi_input_prompt = PromptTemplate(input_variables = ['entity', 'product'],
                                template = 'What is a good name for a {entity} that is about {product}?')


multi_input_prompt.format(entity='blog', product='data science')
 
'What is a good name for a blog that is about data science?'
llm(multi_input_prompt.format(entity='blog', product='data science'))
 
'Data Science Insight'

Chat Open AI Templates

The previous templates were suitable for large language models. When we have to work with the Chat Open AI, we need to create separate templates by role.

Let’s assume that we would like to get a training program for running. Of course, our virtual coach must know our level, the running distance that we want to race and the duration of the program.

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
 

Let’s build a function called running_coach:

def running_coach(level, distance, duration):

    '''
    INPUTS:
        level: The running level, beginner, intermediate, or advanced
        distance: The target running distance
        duration: The duration of the training program
    '''
    
    system_template = "You are an experienced running coach"
    system_message_prompt =  SystemMessagePromptTemplate.from_template(system_template)


    human_template = "I would like to participate in a {distance} race. My level is {level} and I would like a training program for {duration}"
    human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)


    chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

    request = chat_prompt.format_prompt(level=level, distance=distance, duration=duration).to_messages()

    chat = ChatOpenAI()
    result = chat(request)
    return result.content
 

Now, we can call the function by passing the required parameters. In this example, we stated that our level is “intermediate” and we would like to train for “8 weeks” participating in a “5K” race.

print(running_coach(level='intermediate', distance='5K', duration='8 weeks'))
 

Output:

Great! I can definitely help you with that. Here's an 8-week training program to prepare you for a 5K race:

Week 1:
- Day 1: Easy run/walk for 20 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 2 minutes and walking for 1 minute, repeat 5 times
- Day 4: Rest
- Day 5: Easy run/walk for 25 minutes
- Day 6: Rest
- Day 7: Long run/walk for 30 minutes

Week 2:
- Day 1: Easy run/walk for 25 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 3 minutes and walking for 1 minute, repeat 5 times
- Day 4: Rest
- Day 5: Easy run/walk for 30 minutes
- Day 6: Rest
- Day 7: Long run/walk for 35 minutes

Week 3:
- Day 1: Easy run/walk for 30 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 4 minutes and walking for 1 minute, repeat 5 times
- Day 4: Rest
- Day 5: Easy run/walk for 35 minutes
- Day 6: Rest
- Day 7: Long run/walk for 40 minutes

Week 4:
- Day 1: Easy run/walk for 35 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 5 minutes and walking for 1 minute, repeat 5 times
- Day 4: Rest
- Day 5: Easy run/walk for 40 minutes
- Day 6: Rest
- Day 7: Long run/walk for 45 minutes

Week 5:
- Day 1: Easy run/walk for 40 minutes
- Day 2: Rest
- Day 3: Tempo run - run at a comfortably hard pace for 20 minutes
- Day 4: Rest
- Day 5: Easy run/walk for 45 minutes
- Day 6: Rest
- Day 7: Long run/walk for 50 minutes

Week 6:
- Day 1: Easy run for 45 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 6 minutes and walking for 1 minute, repeat 4 times
- Day 4: Rest
- Day 5: Easy run for 50 minutes
- Day 6: Rest
- Day 7: Long run for 55 minutes

Week 7:
- Day 1: Easy run for 50 minutes
- Day 2: Rest
- Day 3: Tempo run - run at a comfortably hard pace for 25 minutes
- Day 4: Rest
- Day 5: Easy run for 55 minutes
- Day 6: Rest
- Day 7: Long run for 60 minutes

Week 8:
- Day 1: Easy run for 45 minutes
- Day 2: Rest
- Day 3: Interval training - alternate between running at a moderate pace for 4 minutes and walking for 1 minute, repeat 6 times
- Day 4: Rest
- Day 5: Easy run for 30 minutes
- Day 6: Rest
- Day 7: Race day! Participate in a 5K race and give it your best effort.

Remember to warm up and cool down with a few minutes of walking or light jogging before and after each workout. Additionally, listen to your body and adjust the intensity or duration of your runs if needed. Good luck with your training and the race!

Perfect! We managed to generate our training program. Look carefully at how we passed the Human and System templates, and start experimenting with your own examples.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Document Splitting with LangChain

In this tutorial, we will talk about different ways of how to split the loaded documents into smaller chunks using