Skip to content

Commit

Permalink
add docstring example
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Mar 20, 2024
1 parent a7494a9 commit c357868
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions libs/core/langchain_core/prompts/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,49 @@ class ChatPromptTemplate(BaseChatPromptTemplate):
("human", "{user_input}"),
])
messages = template.format_messages(
name="Bob",
user_input="What is your name?"
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?')
# ]
#)
If your prompt has only a single input variable (i.e., 1 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.
.. code-block:: python
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
("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!?'),
# ]
# )
""" # noqa: E501

input_variables: List[str]
"""List of input variables in template messages. Used for validation."""
Expand Down

0 comments on commit c357868

Please sign in to comment.