Get Started With Langchain Prompt Templates

Practical examples of Langchain prompt templates in Python

George Pipis
4 min readJul 26, 2023
Image generated by author using DALLE-2

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 =…

--

--

George Pipis

Sr. Director, Data Scientist @ Persado | Co-founder of the Data Science blog: https://predictivehacks.com/