langchain.agents.schema.AgentScratchPadChatPromptTemplate¶
- class langchain.agents.schema.AgentScratchPadChatPromptTemplate(*, input_variables: List[str], output_parser: Optional[BaseOutputParser] = None, partial_variables: Mapping[str, Union[str, Callable[[], str]]] = None, messages: List[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate]])[source]¶
Bases:
ChatPromptTemplateChat prompt template for the agent scratchpad.
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- param input_variables: List[str] [Required]¶
List of input variables in template messages. Used for validation.
- param messages: List[Union[BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate]] [Required]¶
List of messages consisting of either message prompt templates or messages.
- param output_parser: Optional[BaseOutputParser] = None¶
How to parse the output of calling an LLM on this formatted prompt.
- param partial_variables: Mapping[str, Union[str, Callable[[], str]]] [Optional]¶
- dict(**kwargs: Any) Dict¶
Return dictionary representation of prompt.
- format(**kwargs: Any) str¶
Format the chat template into a string.
- Parameters
**kwargs – keyword arguments to use for filling in template variables in all the template messages in this chat template.
- Returns
formatted string
- format_messages(**kwargs: Any) List[BaseMessage]¶
Format the chat template into a list of finalized messages.
- Parameters
**kwargs – keyword arguments to use for filling in template variables in all the template messages in this chat template.
- Returns
list of formatted messages
- format_prompt(**kwargs: Any) PromptValue¶
Format prompt. Should return a PromptValue. :param **kwargs: Keyword arguments to use for formatting.
- Returns
PromptValue.
- classmethod from_messages(messages: Sequence[Union[BaseMessagePromptTemplate, BaseChatPromptTemplate, BaseMessage, Tuple[str, str], Tuple[Type, str], str]]) ChatPromptTemplate¶
Create a chat prompt template from a variety of message formats.
Examples
Instantiation from a list of message templates:
template = ChatPromptTemplate.from_messages([ ("human", "Hello, how are you?"), ("ai", "I'm doing well, thanks!"), ("human", "That's good to hear."), ])
Instantiation from mixed message formats:
template = ChatPromptTemplate.from_messages([ SystemMessage(content="hello"), ("human", "Hello, how are you?"), ])
- Parameters
messages – sequence of message representations. A message can be represented using the following formats: (1) BaseMessagePromptTemplate, (2) BaseMessage, (3) 2-tuple of (message type, template); e.g., (“human”, “{user_input}”), (4) 2-tuple of (message class, template), (4) a string which is shorthand for (“human”, template); e.g., “{user_input}”
- Returns
a chat prompt template
- classmethod from_role_strings(string_messages: List[Tuple[str, str]]) ChatPromptTemplate¶
Create a chat prompt template from a list of (role, template) tuples.
- Parameters
string_messages – list of (role, template) tuples.
- Returns
a chat prompt template
- classmethod from_strings(string_messages: List[Tuple[Type[BaseMessagePromptTemplate], str]]) ChatPromptTemplate¶
Create a chat prompt template from a list of (role class, template) tuples.
- Parameters
string_messages – list of (role class, template) tuples.
- Returns
a chat prompt template
- classmethod from_template(template: str, **kwargs: Any) ChatPromptTemplate¶
Create a chat prompt template from a template string.
Creates a chat template consisting of a single message assumed to be from the human.
- Parameters
template – template string
**kwargs – keyword arguments to pass to the constructor.
- Returns
A new instance of this class.
- invoke(input: Dict, config: langchain.schema.runnable.RunnableConfig | None = None) PromptValue¶
- partial(**kwargs: Union[str, Callable[[], str]]) ChatPromptTemplate¶
Return a new ChatPromptTemplate with some of the input variables already filled in.
- Parameters
**kwargs – keyword arguments to use for filling in template variables. Ought to be a subset of the input variables.
- Returns
A new ChatPromptTemplate.
Example
from langchain.prompts import ChatPromptTemplate template = ChatPromptTemplate.from_messages( [ ("system", "You are an AI assistant named {name}."), ("human", "Hi I'm {user}"), ("ai", "Hi there, {user}, I'm {name}."), ("human", "{input}"), ] ) template2 = template.partial(user="Lucy", name="R2D2") template2.format_messages(input="hello")
- save(file_path: Union[Path, str]) None¶
Save prompt to file.
- Parameters
file_path – path to file.
- to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
- to_json_not_implemented() SerializedNotImplemented¶
- validator validate_input_variables » all fields¶
Validate input variables.
If input_variables is not set, it will be set to the union of all input variables in the messages.
- Parameters
values – values to validate.
- Returns
Validated values.
- validator validate_variable_names » all fields¶
Validate variable names do not include restricted names.
- property lc_attributes: Dict¶
Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.
- property lc_namespace: List[str]¶
Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]
- property lc_secrets: Dict[str, str]¶
Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}
- property lc_serializable: bool¶
Return whether or not the class is serializable.