Fix for #6431 - chatprompt template with partial variables giing validation 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>
This commit is contained in:
Avinash Raj 2023-06-20 10:38:15 +05:30 committed by GitHub
parent 02c0a1e77e
commit 6efd5fa2b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -168,6 +168,8 @@ class ChatPromptTemplate(BaseChatPromptTemplate, ABC):
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(

View File

@ -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"}