Allow extra variables when invoking prompt templates (#10765)

Makes chaining easier as many maps have extra properties.

@baskaryan @hwchase17
This commit is contained in:
Jacob Lee 2023-09-18 20:08:54 -07:00 committed by GitHub
parent 8515e27d82
commit babf46692d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -37,7 +37,9 @@ class BasePromptTemplate(Serializable, Runnable[Dict, PromptValue], ABC):
def invoke(self, input: Dict, config: RunnableConfig | None = None) -> PromptValue: def invoke(self, input: Dict, config: RunnableConfig | None = None) -> PromptValue:
return self._call_with_config( 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, input,
config, config,
run_type="prompt", run_type="prompt",

View File

@ -369,6 +369,24 @@ async def test_prompt() -> None:
] == [expected] ] == [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 @pytest.mark.asyncio
@freeze_time("2023-01-01") @freeze_time("2023-01-01")
async def test_prompt_with_chat_model( async def test_prompt_with_chat_model(