Allow extra variables when invoking prompt templates (#10765)

Makes chaining easier as many maps have extra properties.

@baskaryan @hwchase17
pull/10802/head
Jacob Lee 11 months ago committed by GitHub
parent 8515e27d82
commit babf46692d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,7 +37,9 @@ class BasePromptTemplate(Serializable, Runnable[Dict, PromptValue], ABC):
def invoke(self, input: Dict, config: RunnableConfig | None = None) -> PromptValue:
return self._call_with_config(
lambda inner_input: self.format_prompt(**inner_input),
lambda inner_input: self.format_prompt(
**{key: inner_input[key] for key in self.input_variables}
),
input,
config,
run_type="prompt",

@ -369,6 +369,24 @@ async def test_prompt() -> None:
] == [expected]
def test_prompt_template_params() -> None:
prompt = ChatPromptTemplate.from_template(
"Respond to the following question: {question}"
)
result = prompt.invoke(
{
"question": "test",
"topic": "test",
}
)
assert result == ChatPromptValue(
messages=[HumanMessage(content="Respond to the following question: test")]
)
with pytest.raises(KeyError):
prompt.invoke({})
@pytest.mark.asyncio
@freeze_time("2023-01-01")
async def test_prompt_with_chat_model(

Loading…
Cancel
Save