core: fix "template" not allowed as prompt param (#26060)

- **Description:**  fix "template" not allowed as prompt param
- **Issue:** #26058
- **Dependencies:** none


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Alejandro Rodríguez 2024-09-20 19:33:06 -04:00 committed by GitHub
parent 58f339a67c
commit 4ac9a6f52c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -17,7 +17,7 @@ from langchain_core.utils.formatting import formatter
from langchain_core.utils.interactive_env import is_interactive_env
def jinja2_formatter(template: str, **kwargs: Any) -> str:
def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
"""Format a template using jinja2.
*Security warning*:
@ -99,7 +99,7 @@ def _get_jinja2_variables_from_template(template: str) -> set[str]:
return variables
def mustache_formatter(template: str, **kwargs: Any) -> str:
def mustache_formatter(template: str, /, **kwargs: Any) -> str:
"""Format a template using mustache.
Args:

View File

@ -640,3 +640,22 @@ def test_prompt_missing_vars_error() -> None:
# Check helper text has right number of braces
assert "'{{goingtobemissing}}'" in str(e.value.args[0])
def test_prompt_with_template_variable_name_fstring() -> None:
template = "This is a {template} test."
prompt = PromptTemplate.from_template(template, template_format="f-string")
assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."
def test_prompt_with_template_variable_name_mustache() -> None:
template = "This is a {{template}} test."
prompt = PromptTemplate.from_template(template, template_format="mustache")
assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."
@pytest.mark.requires("jinja2")
def test_prompt_with_template_variable_name_jinja2() -> None:
template = "This is a {{template}} test."
prompt = PromptTemplate.from_template(template, template_format="jinja2")
assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."