Skip to content

Commit

Permalink
Fix for #6431 - chatprompt template with partial variables giing vali…
Browse files Browse the repository at this point in the history
…dation error (#6456)

W.r.t recent changes, ChatPromptTemplate does not accepting partial
variables. This PR should fix that issue.


Fixes #6431




#### Who can review?



  @hwchase17

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
  • Loading branch information
Avinash-Raj and hwchase17 authored Jun 20, 2023
1 parent 02c0a1e commit 6efd5fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions langchain/prompts/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ def validate_input_variables(cls, values: dict) -> dict:
for message in messages:
if isinstance(message, BaseMessagePromptTemplate):
input_vars.update(message.input_variables)
if "partial_variables" in values:
input_vars = input_vars - set(values["partial_variables"])
if "input_variables" in values:
if input_vars != set(values["input_variables"]):
raise ValueError(
Expand Down
28 changes: 28 additions & 0 deletions tests/unit_tests/prompts/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,31 @@ def test_infer_variables() -> None:
messages = [HumanMessagePromptTemplate.from_template("{foo}")]
prompt = ChatPromptTemplate(messages=messages)
assert prompt.input_variables == ["foo"]


def test_chat_valid_with_partial_variables() -> None:
messages = [
HumanMessagePromptTemplate.from_template(
"Do something with {question} using {context} giving it like {formatins}"
)
]
prompt = ChatPromptTemplate(
messages=messages,
input_variables=["question", "context"],
partial_variables={"formatins": "some structure"},
)
assert set(prompt.input_variables) == set(["question", "context"])
assert prompt.partial_variables == {"formatins": "some structure"}


def test_chat_valid_infer_variables() -> None:
messages = [
HumanMessagePromptTemplate.from_template(
"Do something with {question} using {context} giving it like {formatins}"
)
]
prompt = ChatPromptTemplate(
messages=messages, partial_variables={"formatins": "some structure"}
)
assert set(prompt.input_variables) == set(["question", "context"])
assert prompt.partial_variables == {"formatins": "some structure"}

0 comments on commit 6efd5fa

Please sign in to comment.