Let s go ahead and talk about another core component of Langchain called prompt
templates which are like recipes for generating prompts for LLMs.
A prompt refers to the input to the model.
Prompt templates are a way to create
dynamic prompts for LLMs that are more flexible and easier to use than
traditional static prompts.
A prompt template takes a piece of text
and injects a user s input into that piece of text.
Note that in Langchain, there are prompt templates and chat prompt templates.
Prompt templates are used for tasks that involve generating text such as answering
questions or completing sentences, while chat prompt templates are specifically
designed for tasks that involve engaging in conversations.
Let s see how to create and use a prompt template that can be used with any type
of LLM.
From Langchain prompts import prompt
template and from Langchain OpenAI, I am importing the ChatOpenAI class.
Even though it was probably already imported in a cell above and it s not
necessary here, I will import it again just in case you want to run only this
cell and the ones below and not the entire notebook.
I am creating a multiline string called template.
This is similar to an F string and uses
curly braces for the dynamic part of the template.
You are an experienced virologist, write
a few sentences about the following virus and the virus in curly braces in and the
language in curly braces.
Now let s define a prompt object of the
class prompt template.
Prompt template equals the prompt
template class constructor from template of and the template equals template.
This piece of code creates a new prompt template object from the provided
template string.
Next, we ll format the prompt template by
replacing the placeholders virus and language in curly braces with the
provided values prompt equals prompt -template .format and the arguments virus
equals HIV and the language equals German.
In this case, the value HIV is used to
replace the placeholder virus and the value German is used to replace the
placeholder language.
This will result in the following prompt.
You are an experienced virologist, write a few sentences about the following virus
in German.
I can write curly braces virus in double quotes.
I am creating the LLM object.
LLM equals chatOpenAi, modelName equals
gpt3 .5 turbo and the temperature equals 0.
Next, I will invoke the LLM with the
prompt string as an argument output equals LLM .invoke of prompt.
Finally, I am printing the response output .content.
I am running it.
Ok, gpt3 .5 turbo in single quotes.
I ve got a short response about HIV in German.
That s it with prompt templates.
We ll take a break and in the next video, we ll go over the other type of prompt
called chat prompt template.