Home / File/ test_loading.py — langchain Source File

test_loading.py — langchain Source File

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

File python CoreAbstractions Serialization 8 imports 11 functions

Entity Profile

Dependency Diagram

graph LR
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b["test_loading.py"]
  9e98f0a7_ec6e_708f_4f1b_e9428b316e1c["os"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> 9e98f0a7_ec6e_708f_4f1b_e9428b316e1c
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56["contextlib"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56
  b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8["pathlib"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> 120e2591_3e15_b895_72b6_cb26195e40a6
  4fa023ed_e78b_1291_b37f_a81c605fc08d["langchain_core.prompts.few_shot"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> 4fa023ed_e78b_1291_b37f_a81c605fc08d
  2a05822d_373f_590f_ce0e_57ccb68dc8b0["langchain_core.prompts.loading"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> 2a05822d_373f_590f_ce0e_57ccb68dc8b0
  c17bcf07_a2ef_b992_448f_5088d46a1e79["langchain_core.prompts.prompt"]
  eea05055_0cc3_c5ac_867f_b44ce74a7c7b --> c17bcf07_a2ef_b992_448f_5088d46a1e79
  style eea05055_0cc3_c5ac_867f_b44ce74a7c7b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test loading functionality."""

import os
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path

import pytest

from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts.loading import load_prompt
from langchain_core.prompts.prompt import PromptTemplate

EXAMPLE_DIR = (Path(__file__).parent.parent / "examples").absolute()


@contextmanager
def change_directory(dir_path: Path) -> Iterator[None]:
    """Change the working directory to the right folder."""
    origin = Path().absolute()
    try:
        os.chdir(dir_path)
        yield
    finally:
        os.chdir(origin)


def test_loading_from_yaml() -> None:
    """Test loading from yaml file."""
    prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.yaml")
    expected_prompt = PromptTemplate(
        input_variables=["adjective"],
        partial_variables={"content": "dogs"},
        template="Tell me a {adjective} joke about {content}.",
    )
    assert prompt == expected_prompt


def test_loading_from_json() -> None:
    """Test loading from json file."""
    prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.json")
    expected_prompt = PromptTemplate(
        input_variables=["adjective", "content"],
        template="Tell me a {adjective} joke about {content}.",
    )
    assert prompt == expected_prompt


def test_loading_jinja_from_json() -> None:
    """Test that loading jinja2 format prompts from JSON raises ValueError."""
    prompt_path = EXAMPLE_DIR / "jinja_injection_prompt.json"
    with pytest.raises(ValueError, match=r".*can lead to arbitrary code execution.*"):
        load_prompt(prompt_path)


def test_loading_jinja_from_yaml() -> None:
    """Test that loading jinja2 format prompts from YAML raises ValueError."""
    prompt_path = EXAMPLE_DIR / "jinja_injection_prompt.yaml"
    with pytest.raises(ValueError, match=r".*can lead to arbitrary code execution.*"):
        load_prompt(prompt_path)
// ... (120 more lines)

Subdomains

Dependencies

  • collections.abc
  • contextlib
  • langchain_core.prompts.few_shot
  • langchain_core.prompts.loading
  • langchain_core.prompts.prompt
  • os
  • pathlib
  • pytest

Frequently Asked Questions

What does test_loading.py do?
test_loading.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_loading.py?
test_loading.py defines 11 function(s): change_directory, test_loading_few_shot_prompt_example_prompt, test_loading_few_shot_prompt_from_json, test_loading_few_shot_prompt_from_yaml, test_loading_few_shot_prompt_when_examples_in_config, test_loading_from_json, test_loading_from_yaml, test_loading_jinja_from_json, test_loading_jinja_from_yaml, test_loading_with_template_as_file, and 1 more.
What does test_loading.py depend on?
test_loading.py imports 8 module(s): collections.abc, contextlib, langchain_core.prompts.few_shot, langchain_core.prompts.loading, langchain_core.prompts.prompt, os, pathlib, pytest.
Where is test_loading.py in the architecture?
test_loading.py is located at libs/core/tests/unit_tests/prompts/test_loading.py (domain: CoreAbstractions, subdomain: Serialization, 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