Fake Chain#

This fake Chain can be used for mocking Chain calls during testing.

[1]:
from langchain_contrib.chains.testing import FakeChain

chain = FakeChain(output={"Hello": "World"})
chain({})
[1]:
{'Hello': 'World'}

If you want it to accept inputs, then you should declare its expected_inputs so that LangChain doesn’t complain about unexpected input keys:

[2]:
chain = FakeChain(output={"Hello": "World"}, expected_inputs=["a"])
chain({"a": "sdf"})
[2]:
{'a': 'sdf', 'Hello': 'World'}

If you want to have it programmatically convert inputs to outputs so that you don’t have to define a new FakeChain every single time, you can define its inputs_to_outputs function. When doing so, you should also declare its expected_outputs so that LangChain doesn’t complain about unexpected output keys:

[3]:
from typing import Dict

def append_exclamation(inputs: Dict[str, str]) -> Dict[str, str]:
    return {"b": inputs["a"] + "!"}

chain = FakeChain(
    expected_inputs=["a"],
    expected_outputs=["b"],
    inputs_to_outputs=append_exclamation,
)
chain({"a": "Hello World"}, return_only_outputs=True)
[3]:
{'b': 'Hello World!'}