Home / File/ test_few_shot_with_templates.py — langchain Source File

test_few_shot_with_templates.py — langchain Source File

Architecture documentation for test_few_shot_with_templates.py, a python file in the langchain codebase. 4 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  9fae2215_1172_42fd_b805_4899362f6a34["test_few_shot_with_templates.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  9fae2215_1172_42fd_b805_4899362f6a34 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  9fae2215_1172_42fd_b805_4899362f6a34 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  c97a0dce_925d_55ec_7e9c_82a22379554a["langchain_core.prompts.few_shot_with_templates"]
  9fae2215_1172_42fd_b805_4899362f6a34 --> c97a0dce_925d_55ec_7e9c_82a22379554a
  c17bcf07_a2ef_b992_448f_5088d46a1e79["langchain_core.prompts.prompt"]
  9fae2215_1172_42fd_b805_4899362f6a34 --> c17bcf07_a2ef_b992_448f_5088d46a1e79
  style 9fae2215_1172_42fd_b805_4899362f6a34 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test few shot prompt template."""

import re

import pytest

from langchain_core.prompts.few_shot_with_templates import FewShotPromptWithTemplates
from langchain_core.prompts.prompt import PromptTemplate

EXAMPLE_PROMPT = PromptTemplate(
    input_variables=["question", "answer"], template="{question}: {answer}"
)


async def test_prompttemplate_prefix_suffix() -> None:
    """Test that few shot works when prefix and suffix are PromptTemplates."""
    prefix = PromptTemplate(
        input_variables=["content"], template="This is a test about {content}."
    )
    suffix = PromptTemplate(
        input_variables=["new_content"],
        template="Now you try to talk about {new_content}.",
    )

    examples = [
        {"question": "foo", "answer": "bar"},
        {"question": "baz", "answer": "foo"},
    ]
    prompt = FewShotPromptWithTemplates(
        suffix=suffix,
        prefix=prefix,
        input_variables=["content", "new_content"],
        examples=examples,
        example_prompt=EXAMPLE_PROMPT,
        example_separator="\n",
    )
    expected_output = (
        "This is a test about animals.\n"
        "foo: bar\n"
        "baz: foo\n"
        "Now you try to talk about party."
    )
    output = prompt.format(content="animals", new_content="party")
    assert output == expected_output
    output = await prompt.aformat(content="animals", new_content="party")
    assert output == expected_output


def test_prompttemplate_validation() -> None:
    """Test that few shot works when prefix and suffix are PromptTemplates."""
    prefix = PromptTemplate(
        input_variables=["content"], template="This is a test about {content}."
    )
    suffix = PromptTemplate(
        input_variables=["new_content"],
        template="Now you try to talk about {new_content}.",
    )

    examples = [
        {"question": "foo", "answer": "bar"},
        {"question": "baz", "answer": "foo"},
    ]
    with pytest.raises(
        ValueError,
        match=re.escape("Got input_variables=[], but based on prefix/suffix expected"),
    ):
        FewShotPromptWithTemplates(
            suffix=suffix,
            prefix=prefix,
            input_variables=[],
            examples=examples,
            example_prompt=EXAMPLE_PROMPT,
            example_separator="\n",
            validate_template=True,
        )
    assert FewShotPromptWithTemplates(
        suffix=suffix,
        prefix=prefix,
        input_variables=[],
        examples=examples,
        example_prompt=EXAMPLE_PROMPT,
        example_separator="\n",
    ).input_variables == ["content", "new_content"]

Subdomains

Dependencies

  • langchain_core.prompts.few_shot_with_templates
  • langchain_core.prompts.prompt
  • pytest
  • re

Frequently Asked Questions

What does test_few_shot_with_templates.py do?
test_few_shot_with_templates.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_few_shot_with_templates.py?
test_few_shot_with_templates.py defines 2 function(s): test_prompttemplate_prefix_suffix, test_prompttemplate_validation.
What does test_few_shot_with_templates.py depend on?
test_few_shot_with_templates.py imports 4 module(s): langchain_core.prompts.few_shot_with_templates, langchain_core.prompts.prompt, pytest, re.
Where is test_few_shot_with_templates.py in the architecture?
test_few_shot_with_templates.py is located at libs/core/tests/unit_tests/prompts/test_few_shot_with_templates.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/tests/unit_tests/prompts).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free