Prompt Templates
Prompt template classes.
class langchain.prompts.AIMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) – 
additional_kwargs (dict) – 
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) – 
Return type
langchain.schema.BaseMessage
class langchain.prompts.BaseChatPromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.prompts.base.BasePromptTemplate, abc.ABC
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
Return type
None
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
abstract format_messages(**kwargs)[source]
Format kwargs into a list of messages.
Parameters
kwargs (Any) – 
Return type
List[langchain.schema.BaseMessage]
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) – 
Return type
langchain.schema.PromptValue
class langchain.prompts.BasePromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.load.serializable.Serializable, abc.ABC
Base class for all prompt templates, returning a prompt.
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute output_parser: Optional[langchain.schema.BaseOutputParser] = None
How to parse the output of calling an LLM on this formatted prompt.
attribute partial_variables: Mapping[str, Union[str, Callable[[], str]]] [Optional]
dict(**kwargs)[source]
Return dictionary representation of prompt.
Parameters
kwargs (Any) – 
Return type
Dict
abstract format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
abstract format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) – 
Return type
langchain.schema.PromptValue
partial(**kwargs)[source]
Return a partial of the prompt template.
Parameters
kwargs (Union[str, Callable[[], str]]) – 
Return type
langchain.prompts.base.BasePromptTemplate
save(file_path)[source]
Save the prompt.
Parameters
file_path (Union[pathlib.Path, str]) – Path to directory to save prompt to.
Return type
None
Example:
.. code-block:: python
prompt.save(file_path=”path/prompt.yaml”)
property lc_serializable: bool
Return whether or not the class is serializable.
class langchain.prompts.ChatMessagePromptTemplate(*, prompt, additional_kwargs=None, role)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) – 
additional_kwargs (dict) – 
role (str) – 
Return type
None
attribute role: str [Required]
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) – 
Return type
langchain.schema.BaseMessage
class langchain.prompts.ChatPromptTemplate(*, input_variables, output_parser=None, partial_variables=None, messages)[source]
Bases: langchain.prompts.chat.BaseChatPromptTemplate, abc.ABC
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
messages (List[Union[langchain.prompts.chat.BaseMessagePromptTemplate, langchain.schema.BaseMessage]]) – 
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute messages: List[Union[BaseMessagePromptTemplate, BaseMessage]] [Required]
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
format_messages(**kwargs)[source]
Format kwargs into a list of messages.
Parameters
kwargs (Any) – 
Return type
List[langchain.schema.BaseMessage]
classmethod from_messages(messages)[source]
Parameters
messages (Sequence[Union[langchain.prompts.chat.BaseMessagePromptTemplate, langchain.schema.BaseMessage]]) – 
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_role_strings(string_messages)[source]
Parameters
string_messages (List[Tuple[str, str]]) – 
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_strings(string_messages)[source]
Parameters
string_messages (List[Tuple[Type[langchain.prompts.chat.BaseMessagePromptTemplate], str]]) – 
Return type
langchain.prompts.chat.ChatPromptTemplate
classmethod from_template(template, **kwargs)[source]
Parameters
template (str) – 
kwargs (Any) – 
Return type
langchain.prompts.chat.ChatPromptTemplate
partial(**kwargs)[source]
Return a partial of the prompt template.
Parameters
kwargs (Union[str, Callable[[], str]]) – 
Return type
langchain.prompts.base.BasePromptTemplate
save(file_path)[source]
Save the prompt.
Parameters
file_path (Union[pathlib.Path, str]) – Path to directory to save prompt to.
Return type
None
Example:
.. code-block:: python
prompt.save(file_path=”path/prompt.yaml”)
class langchain.prompts.FewShotPromptTemplate(*, input_variables, output_parser=None, partial_variables=None, examples=None, example_selector=None, example_prompt, suffix, example_separator='\n\n', prefix='', template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Prompt template that contains few shot examples.
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
examples (Optional[List[dict]]) – 
example_selector (Optional[langchain.prompts.example_selector.base.BaseExampleSelector]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
suffix (str) – 
example_separator (str) – 
prefix (str) – 
template_format (str) – 
validate_template (bool) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
PromptTemplate used to format an individual example.
attribute example_selector: Optional[langchain.prompts.example_selector.base.BaseExampleSelector] = None
ExampleSelector to choose the examples to format into the prompt.
Either this or examples should be provided.
attribute example_separator: str = '\n\n'
String separator used to join the prefix, the examples, and suffix.
attribute examples: Optional[List[dict]] = None
Examples to format into the prompt.
Either this or example_selector should be provided.
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute prefix: str = ''
A prompt template string to put before the examples.
attribute suffix: str [Required]
A prompt template string to put after the examples.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.
attribute validate_template: bool = True
Whether or not to try validating the template.
dict(**kwargs)[source]
Return a dictionary of the prompt.
Parameters
kwargs (Any) – 
Return type
Dict
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
property lc_serializable: bool
Return whether or not the class is serializable.
class langchain.prompts.FewShotPromptWithTemplates(*, input_variables, output_parser=None, partial_variables=None, examples=None, example_selector=None, example_prompt, suffix, example_separator='\n\n', prefix=None, template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Prompt template that contains few shot examples.
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
examples (Optional[List[dict]]) – 
example_selector (Optional[langchain.prompts.example_selector.base.BaseExampleSelector]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
suffix (langchain.prompts.base.StringPromptTemplate) – 
example_separator (str) – 
prefix (Optional[langchain.prompts.base.StringPromptTemplate]) – 
template_format (str) – 
validate_template (bool) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
PromptTemplate used to format an individual example.
attribute example_selector: Optional[langchain.prompts.example_selector.base.BaseExampleSelector] = None
ExampleSelector to choose the examples to format into the prompt.
Either this or examples should be provided.
attribute example_separator: str = '\n\n'
String separator used to join the prefix, the examples, and suffix.
attribute examples: Optional[List[dict]] = None
Examples to format into the prompt.
Either this or example_selector should be provided.
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute prefix: Optional[langchain.prompts.base.StringPromptTemplate] = None
A PromptTemplate to put before the examples.
attribute suffix: langchain.prompts.base.StringPromptTemplate [Required]
A PromptTemplate to put after the examples.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.
attribute validate_template: bool = True
Whether or not to try validating the template.
dict(**kwargs)[source]
Return a dictionary of the prompt.
Parameters
kwargs (Any) – 
Return type
Dict
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
class langchain.prompts.HumanMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) – 
additional_kwargs (dict) – 
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) – 
Return type
langchain.schema.BaseMessage
class langchain.prompts.LengthBasedExampleSelector(*, examples, example_prompt, get_text_length=<function _get_length_based>, max_length=2048, example_text_lengths=[])[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select examples based on length.
Parameters
examples (List[dict]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
get_text_length (Callable[[str], int]) – 
max_length (int) – 
example_text_lengths (List[int]) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.
attribute get_text_length: Callable[[str], int] = <function _get_length_based>
Function to measure prompt length. Defaults to word count.
attribute max_length: int = 2048
Max length for the prompt, beyond which examples are cut.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) – 
Return type
None
select_examples(input_variables)[source]
Select which examples to use based on the input lengths.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.MaxMarginalRelevanceExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None, fetch_k=20)[source]
Bases: langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
ExampleSelector that selects examples based on Max Marginal Relevance.
This was shown to improve performance in this paper:
https://arxiv.org/pdf/2211.13892.pdf
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) – 
k (int) – 
example_keys (Optional[List[str]]) – 
input_keys (Optional[List[str]]) – 
fetch_k (int) – 
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute fetch_k: int = 20
Number of examples to fetch to rerank.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.
attribute k: int = 4
Number of examples to select.
attribute vectorstore: langchain.vectorstores.base.VectorStore [Required]
VectorStore than contains information about examples.
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, fetch_k=20, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An iniialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
fetch_k (int) – 
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.MessagesPlaceholder(*, variable_name)[source]
Bases: langchain.prompts.chat.BaseMessagePromptTemplate
Prompt template that assumes variable is already list of messages.
Parameters
variable_name (str) – 
Return type
None
attribute variable_name: str [Required]
format_messages(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) – 
Return type
List[langchain.schema.BaseMessage]
property input_variables: List[str]
Input variables for this prompt template.
class langchain.prompts.NGramOverlapExampleSelector(*, examples, example_prompt, threshold=- 1.0)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select and order examples based on ngram overlap score (sentence_bleu score).
https://www.nltk.org/_modules/nltk/translate/bleu_score.html
https://aclanthology.org/P02-1040.pdf
Parameters
examples (List[dict]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
threshold (float) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.
attribute threshold: float = -1.0
Threshold at which algorithm stops. Set to -1.0 by default.
For negative threshold:
select_examples sorts examples by ngram_overlap_score, but excludes none.
For threshold greater than 1.0:
select_examples excludes all examples, and returns an empty list.
For threshold equal to 0.0:
select_examples sorts examples by ngram_overlap_score,
and excludes examples with no ngram overlap with input.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) – 
Return type
None
select_examples(input_variables)[source]
Return list of examples sorted by ngram_overlap_score with input.
Descending order.
Excludes any examples with ngram_overlap_score less than or equal to threshold.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.PipelinePromptTemplate(*, input_variables, output_parser=None, partial_variables=None, final_prompt, pipeline_prompts)[source]
Bases: langchain.prompts.base.BasePromptTemplate
A prompt template for composing multiple prompts together.
This can be useful when you want to reuse parts of prompts.
A PipelinePrompt consists of two main parts:
final_prompt: This is the final prompt that is returned
pipeline_prompts: This is a list of tuples, consistingof a string (name) and a Prompt Template.
Each PromptTemplate will be formatted and then passed
to future prompt templates as a variable with
the same name as name
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
final_prompt (langchain.prompts.base.BasePromptTemplate) – 
pipeline_prompts (List[Tuple[str, langchain.prompts.base.BasePromptTemplate]]) – 
Return type
None
attribute final_prompt: langchain.prompts.base.BasePromptTemplate [Required]
attribute pipeline_prompts: List[Tuple[str, langchain.prompts.base.BasePromptTemplate]] [Required]
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) – 
Return type
langchain.schema.PromptValue
langchain.prompts.Prompt
alias of langchain.prompts.prompt.PromptTemplate
class langchain.prompts.PromptTemplate(*, input_variables, output_parser=None, partial_variables=None, template, template_format='f-string', validate_template=True)[source]
Bases: langchain.prompts.base.StringPromptTemplate
Schema to represent a prompt for an LLM.
Example
from langchain import PromptTemplate
prompt = PromptTemplate(input_variables=["foo"], template="Say {foo}")
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
template (str) – 
template_format (str) – 
validate_template (bool) – 
Return type
None
attribute input_variables: List[str] [Required]
A list of the names of the variables the prompt template expects.
attribute template: str [Required]
The prompt template.
attribute template_format: str = 'f-string'
The format of the prompt template. Options are: ‘f-string’, ‘jinja2’.
attribute validate_template: bool = True
Whether or not to try validating the template.
format(**kwargs)[source]
Format the prompt with the inputs.
Parameters
kwargs (Any) – Any arguments to be passed to the prompt template.
Returns
A formatted string.
Return type
str
Example:
prompt.format(variable1="foo")
classmethod from_examples(examples, suffix, input_variables, example_separator='\n\n', prefix='', **kwargs)[source]
Take examples in list format with prefix and suffix to create a prompt.
Intended to be used as a way to dynamically create a prompt from examples.
Parameters
examples (List[str]) – List of examples to use in the prompt.
suffix (str) – String to go after the list of examples. Should generally
set up the user’s input.
input_variables (List[str]) – A list of variable names the final prompt template
will expect.
example_separator (str) – The separator to use in between examples. Defaults
to two new line characters.
prefix (str) – String that should go before any examples. Generally includes
examples. Default to an empty string.
kwargs (Any) – 
Returns
The final prompt generated.
Return type
langchain.prompts.prompt.PromptTemplate
classmethod from_file(template_file, input_variables, **kwargs)[source]
Load a prompt from a file.
Parameters
template_file (Union[str, pathlib.Path]) – The path to the file containing the prompt template.
input_variables (List[str]) – A list of variable names the final prompt template
will expect.
kwargs (Any) – 
Returns
The prompt loaded from the file.
Return type
langchain.prompts.prompt.PromptTemplate
classmethod from_template(template, **kwargs)[source]
Load a prompt template from a template.
Parameters
template (str) – 
kwargs (Any) – 
Return type
langchain.prompts.prompt.PromptTemplate
property lc_attributes: Dict[str, Any]
Return a list of attribute names that should be included in the
serialized kwargs. These attributes must be accepted by the
constructor.
class langchain.prompts.SemanticSimilarityExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Example selector that selects examples based on SemanticSimilarity.
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) – 
k (int) – 
example_keys (Optional[List[str]]) – 
input_keys (Optional[List[str]]) – 
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.
attribute k: int = 4
Number of examples to select.
attribute vectorstore: langchain.vectorstores.base.VectorStore [Required]
VectorStore than contains information about examples.
add_example(example)[source]
Add new example to vectorstore.
Parameters
example (Dict[str, str]) – 
Return type
str
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An initialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.StringPromptTemplate(*, input_variables, output_parser=None, partial_variables=None)[source]
Bases: langchain.prompts.base.BasePromptTemplate, abc.ABC
String prompt should expose the format method, returning a prompt.
Parameters
input_variables (List[str]) – 
output_parser (Optional[langchain.schema.BaseOutputParser]) – 
partial_variables (Mapping[str, Union[str, Callable[[], str]]]) – 
Return type
None
format_prompt(**kwargs)[source]
Create Chat Messages.
Parameters
kwargs (Any) – 
Return type
langchain.schema.PromptValue
class langchain.prompts.SystemMessagePromptTemplate(*, prompt, additional_kwargs=None)[source]
Bases: langchain.prompts.chat.BaseStringMessagePromptTemplate
Parameters
prompt (langchain.prompts.base.StringPromptTemplate) – 
additional_kwargs (dict) – 
Return type
None
format(**kwargs)[source]
To a BaseMessage.
Parameters
kwargs (Any) – 
Return type
langchain.schema.BaseMessage
langchain.prompts.load_prompt(path)[source]
Unified method for loading a prompt from LangChainHub or local fs.
Parameters
path (Union[str, pathlib.Path]) – 
Return type
langchain.prompts.base.BasePromptTemplate