mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-14 05:56:40 +00:00
Support inference of input_variables
from jinja2
template (#3013)
`langchain.prompts.PromptTemplate` is unable to infer `input_variables` from jinja2 template. ```python # Using langchain v0.0.141 template_string = """\ Hello world Your variable: {{ var }} {# This will not get rendered #} {% if verbose %} Congrats! You just turned on verbose mode and got extra messages! {% endif %} """ template = PromptTemplate.from_template(template_string, template_format="jinja2") print(template.input_variables) # Output ['# This will not get rendered #', '% endif %', '% if verbose %'] ``` --------- Co-authored-by: engkheng <ongengkheng929@example.com>
This commit is contained in:
@@ -145,3 +145,70 @@ def test_partial() -> None:
|
||||
assert new_result == "This is a 3 test."
|
||||
result = prompt.format(foo="foo")
|
||||
assert result == "This is a foo test."
|
||||
|
||||
|
||||
def test_prompt_from_jinja2_template() -> None:
|
||||
"""Test prompts can be constructed from a jinja2 template."""
|
||||
# Empty input variable.
|
||||
template = """Hello there
|
||||
There is no variable here {
|
||||
Will it get confused{ }?
|
||||
"""
|
||||
prompt = PromptTemplate.from_template(template, template_format="jinja2")
|
||||
expected_prompt = PromptTemplate(
|
||||
template=template, input_variables=[], template_format="jinja2"
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
||||
# Multiple input variables.
|
||||
template = """\
|
||||
Hello world
|
||||
|
||||
Your variable: {{ foo }}
|
||||
|
||||
{# This will not get rendered #}
|
||||
|
||||
{% if bar %}
|
||||
You just set bar boolean variable to true
|
||||
{% endif %}
|
||||
|
||||
{% for i in foo_list %}
|
||||
{{ i }}
|
||||
{% endfor %}
|
||||
"""
|
||||
prompt = PromptTemplate.from_template(template, template_format="jinja2")
|
||||
expected_prompt = PromptTemplate(
|
||||
template=template,
|
||||
input_variables=["bar", "foo", "foo_list"],
|
||||
template_format="jinja2",
|
||||
)
|
||||
|
||||
assert prompt == expected_prompt
|
||||
|
||||
# Multiple input variables with repeats.
|
||||
template = """\
|
||||
Hello world
|
||||
|
||||
Your variable: {{ foo }}
|
||||
|
||||
{# This will not get rendered #}
|
||||
|
||||
{% if bar %}
|
||||
You just set bar boolean variable to true
|
||||
{% endif %}
|
||||
|
||||
{% for i in foo_list %}
|
||||
{{ i }}
|
||||
{% endfor %}
|
||||
|
||||
{% if bar %}
|
||||
Your variable again: {{ foo }}
|
||||
{% endif %}
|
||||
"""
|
||||
prompt = PromptTemplate.from_template(template, template_format="jinja2")
|
||||
expected_prompt = PromptTemplate(
|
||||
template=template,
|
||||
input_variables=["bar", "foo", "foo_list"],
|
||||
template_format="jinja2",
|
||||
)
|
||||
assert prompt == expected_prompt
|
||||
|
Reference in New Issue
Block a user