Prompt template for chat models.
Use to create flexible templated prompts for chat models.
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate(
[
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
]
)
prompt_value = template.invoke(
{
"name": "Bob",
"user_input": "What is your name?",
}
)
# Output:
# ChatPromptValue(
# messages=[
# SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),
# HumanMessage(content='Hello, how are you doing?'),
# AIMessage(content="I'm doing well, thanks!"),
# HumanMessage(content='What is your name?')
# ]
# )# In addition to Human/AI/Tool/Function messages,
# you can initialize the template with a MessagesPlaceholder
# either using the class directly or with the shorthand tuple syntax:
template = ChatPromptTemplate(
[
("system", "You are a helpful AI bot."),
# Means the template will receive an optional list of messages under
# the "conversation" key
("placeholder", "{conversation}"),
# Equivalently:
# MessagesPlaceholder(variable_name="conversation", optional=True)
]
)
prompt_value = template.invoke(
{
"conversation": [
("human", "Hi!"),
("ai", "How can I assist you today?"),
("human", "Can you make me an ice cream sundae?"),
("ai", "No."),
]
}
)
# Output:
# ChatPromptValue(
# messages=[
# SystemMessage(content='You are a helpful AI bot.'),
# HumanMessage(content='Hi!'),
# AIMessage(content='How can I assist you today?'),
# HumanMessage(content='Can you make me an ice cream sundae?'),
# AIMessage(content='No.'),
# ]
# )If your prompt has only a single input variable (i.e., one instance of
'{variable_nams}'), and you invoke the template with a non-dict object, the
prompt template will inject the provided argument into that variable location.
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate(
[
("system", "You are a helpful AI bot. Your name is Carl."),
("human", "{user_input}"),
]
)
prompt_value = template.invoke("Hello, there!")
# Equivalent to
# prompt_value = template.invoke({"user_input": "Hello, there!"})
# Output:
# ChatPromptValue(
# messages=[
# SystemMessage(content='You are a helpful AI bot. Your name is Carl.'),
# HumanMessage(content='Hello, there!'),
# ]
# )ChatPromptTemplate(
self,
messages: Sequence[MessageLikeRepresentation],
*,
template_format: PromptTemplateFormat = 'f-string',
**kwargs: Any = {}
)| Name | Type | Description |
|---|---|---|
messages* | Sequence[MessageLikeRepresentation] | Sequence of message representations. A message can be represented using the following formats:
|
template_format | PromptTemplateFormat | Default: 'f-string'Format of the template. |
**kwargs | Any | Default: {}Additional keyword arguments passed to
|
| Name | Type |
|---|---|
| messages | Sequence[MessageLikeRepresentation] |
| template_format | PromptTemplateFormat |
Get the namespace of the LangChain object.
Validate input variables.
If input_variables is not set, it will be set to the union of all input
variables in the messages.
Create a chat prompt template from a template string.
Creates a chat template consisting of a single message assumed to be from the human.
Create a chat prompt template from a variety of message formats.
Format the chat template into a list of finalized messages.
Async format the chat template into a list of finalized messages.
Get a new ChatPromptTemplate with some input variables already filled in.
Append a message to the end of the chat template.
Extend the chat template with a sequence of messages.
Human-readable representation.
Save prompt to file.
A list of the names of the variables whose values are required as inputs to the
A list of the names of the variables for placeholder or MessagePlaceholder that
A dictionary of the types of the variables the prompt template expects.
How to parse the output of calling an LLM on this formatted prompt.
A dictionary of the partial variables the prompt template carries.
Metadata to be used for tracing.
Tags to be used for tracing.
Return the output type of the prompt.
Validate variable names do not include restricted names.
Return True as this class is serializable.
Get the input schema for the prompt.
Invoke the prompt.
Async invoke the prompt.
Create PromptValue.
Async create PromptValue.
Format the prompt with the inputs.
Async format the prompt with the inputs.
Return dictionary representation of prompt.
The name of the Runnable. Used for debugging and tracing.
Input type.
Output Type.
The type of input this Runnable accepts specified as a Pydantic model.
Output schema.
List configurable fields for this Runnable.
Get the name of the Runnable.
Get a Pydantic model that can be used to validate input to the Runnable.
Get a JSON schema that represents the input to the Runnable.
Get a Pydantic model that can be used to validate output to the Runnable.
Get a JSON schema that represents the output of the Runnable.
The type of config this Runnable accepts specified as a Pydantic model.
Get a JSON schema that represents the config of the Runnable.
Return a graph representation of this Runnable.
Return a list of prompts used by this Runnable.
Pipe Runnable objects.
Pick keys from the output dict of this Runnable.
Assigns new fields to the dict output of this Runnable.
Transform a single input into an output.
Transform a single input into an output.
Default implementation runs invoke in parallel using a thread pool executor.
Run invoke in parallel on a list of inputs.
Default implementation runs ainvoke in parallel using asyncio.gather.
Run ainvoke in parallel on a list of inputs.
Default implementation of stream, which calls invoke.
Default implementation of astream, which calls ainvoke.
Stream all output from a Runnable, as reported to the callback system.
Generate a stream of events.
Transform inputs to outputs.
Transform inputs to outputs.
Bind arguments to a Runnable, returning a new Runnable.
Bind config to a Runnable, returning a new Runnable.
Bind lifecycle listeners to a Runnable, returning a new Runnable.
Bind async lifecycle listeners to a Runnable.
Bind input and output types to a Runnable, returning a new Runnable.
Create a new Runnable that retries the original Runnable on exceptions.
Return a new Runnable that maps a list of inputs to a list of outputs.
Add fallbacks to a Runnable, returning a new Runnable.
Create a BaseTool from a Runnable.