nemoguardrails.testing.fixtures

View as Markdown

Pytest fixtures exposing the public testing surface.

To opt in, add the following to your project’s conftest.py::

pytest_plugins = [“nemoguardrails.testing.fixtures”]

The fixtures below provide reasonable defaults that cover the most common testing patterns: a scriptable :class:FakeLLMModel and a :class:TestChat factory that wires the fake model into an :class:LLMRails app.

Module Contents

Functions

NameDescription
fake_llmReturn a :class:FakeLLMModel with a single "Hello!" response.
make_fake_llmReturn a factory for building :class:FakeLLMModel instances.
make_test_chatReturn a factory that builds :class:TestChat instances bound to a

API

nemoguardrails.testing.fixtures.fake_llm() -> nemoguardrails.testing.fake_model.FakeLLMModel

Return a :class:FakeLLMModel with a single "Hello!" response.

Override the responses by overriding this fixture in your own conftest, or instantiate :class:FakeLLMModel directly when you need custom behaviour::

from nemoguardrails.testing import FakeLLMModel

llm = FakeLLMModel(responses=[“My scripted answer”])

nemoguardrails.testing.fixtures.make_fake_llm() -> typing.Callable[..., nemoguardrails.testing.fake_model.FakeLLMModel]

Return a factory for building :class:FakeLLMModel instances.

Useful when a single test needs more than one fake model, or when the set of responses is computed dynamically::

def test_two_calls(make_fake_llm): llm_one = make_fake_llm(responses=[“one”]) llm_two = make_fake_llm(responses=[“two”])

nemoguardrails.testing.fixtures.make_test_chat() -> typing.Callable[..., nemoguardrails.testing.chat_harness.TestChat]

Return a factory that builds :class:TestChat instances bound to a fake LLM.

Example::

def test_greeting(make_test_chat): config = RailsConfig.from_path(”./examples/bots/abc”) chat = make_test_chat(config, llm_completions=[“Hi there!”]) chat.user(“hi”) chat.bot(“Hi there!”)