Home / Function/ test_prompt_with_chat_model() — langchain Function Reference

test_prompt_with_chat_model() — langchain Function Reference

Architecture documentation for the test_prompt_with_chat_model() function in test_runnable.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  39e9ce18_afd0_bc6c_ae16_f073a179b7e0["test_prompt_with_chat_model()"]
  26df6ad8_0189_51d0_c3c1_6c3248893ff5["test_runnable.py"]
  39e9ce18_afd0_bc6c_ae16_f073a179b7e0 -->|defined in| 26df6ad8_0189_51d0_c3c1_6c3248893ff5
  593bb72a_4695_6c93_b95c_e277aca006ae["batch()"]
  39e9ce18_afd0_bc6c_ae16_f073a179b7e0 -->|calls| 593bb72a_4695_6c93_b95c_e277aca006ae
  a3895c50_dab4_0c36_2d24_f63121e198a0["invoke()"]
  39e9ce18_afd0_bc6c_ae16_f073a179b7e0 -->|calls| a3895c50_dab4_0c36_2d24_f63121e198a0
  style 39e9ce18_afd0_bc6c_ae16_f073a179b7e0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/runnables/test_runnable.py lines 1773–1874

def test_prompt_with_chat_model(
    mocker: MockerFixture,
    snapshot: SnapshotAssertion,
) -> None:
    prompt = (
        SystemMessagePromptTemplate.from_template("You are a nice assistant.")
        + "{question}"
    )
    chat = FakeListChatModel(responses=["foo"])

    chain = prompt | chat

    assert repr(chain) == snapshot
    assert isinstance(chain, RunnableSequence)
    assert chain.first == prompt
    assert chain.middle == []
    assert chain.last == chat
    assert dumps(chain, pretty=True) == snapshot

    # Test invoke
    prompt_spy = mocker.spy(prompt.__class__, "invoke")
    chat_spy = mocker.spy(chat.__class__, "invoke")
    tracer = FakeTracer()
    assert chain.invoke(
        {"question": "What is your name?"}, {"callbacks": [tracer]}
    ) == _any_id_ai_message(content="foo")
    assert prompt_spy.call_args.args[1] == {"question": "What is your name?"}
    assert chat_spy.call_args.args[1] == ChatPromptValue(
        messages=[
            SystemMessage(content="You are a nice assistant."),
            HumanMessage(content="What is your name?"),
        ]
    )

    assert tracer.runs == snapshot

    mocker.stop(prompt_spy)
    mocker.stop(chat_spy)

    # Test batch
    prompt_spy = mocker.spy(prompt.__class__, "batch")
    chat_spy = mocker.spy(chat.__class__, "batch")
    tracer = FakeTracer()
    assert chain.batch(
        [
            {"question": "What is your name?"},
            {"question": "What is your favorite color?"},
        ],
        {"callbacks": [tracer]},
    ) == [
        _any_id_ai_message(content="foo"),
        _any_id_ai_message(content="foo"),
    ]
    assert prompt_spy.call_args.args[1] == [
        {"question": "What is your name?"},
        {"question": "What is your favorite color?"},
    ]
    assert chat_spy.call_args.args[1] == [
        ChatPromptValue(
            messages=[
                SystemMessage(content="You are a nice assistant."),
                HumanMessage(content="What is your name?"),
            ]
        ),
        ChatPromptValue(
            messages=[
                SystemMessage(content="You are a nice assistant."),
                HumanMessage(content="What is your favorite color?"),
            ]
        ),
    ]
    assert (
        len(
            [
                r
                for r in tracer.runs
                if r.parent_run_id is None and len(r.child_runs) == 2
            ]
        )
        == 2
    ), "Each of 2 outer runs contains exactly two inner runs (1 prompt, 1 chat)"

Domain

Subdomains

Frequently Asked Questions

What does test_prompt_with_chat_model() do?
test_prompt_with_chat_model() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/runnables/test_runnable.py.
Where is test_prompt_with_chat_model() defined?
test_prompt_with_chat_model() is defined in libs/core/tests/unit_tests/runnables/test_runnable.py at line 1773.
What does test_prompt_with_chat_model() call?
test_prompt_with_chat_model() calls 2 function(s): batch, invoke.

Analyze Your Own Codebase

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

Try Supermodel Free