langchain/libs/partners/prompty/tests/unit_tests/test_templating.py
Matthieu 783397eacb
community: avoid double templating in langchain_prompty (#25777)
## Description

In `langchain_prompty`, messages are templated by Prompty. However, a
call to `ChatPromptTemplate` was initiating a second templating. We now
convert parsed messages to `Message` objects before calling
`ChatPromptTemplate`, signifying clearly that they are already
templated.

We also revert #25739 , which applied to this second templating, which
we now avoid, and did not fix the original issue.

## Issue

Closes #25703
2024-08-28 18:18:18 -04:00

24 lines
665 B
Python

from pathlib import Path
import pytest
from langchain_prompty import create_chat_prompt
PROMPT_DIR = Path(__file__).parent / "prompts"
def test_double_templating() -> None:
"""
Assess whether double templating occurs when invoking a chat prompt.
If it does, an error is thrown and the test fails.
"""
prompt_path = PROMPT_DIR / "double_templating.prompty"
templated_prompt = create_chat_prompt(str(prompt_path))
query = "What do you think of this JSON object: {'key': 7}?"
try:
templated_prompt.invoke(input={"user_input": query})
except KeyError as e:
pytest.fail("Double templating occurred: " + str(e))