Skip to content

trulens.core.guardrails.base

trulens.core.guardrails.base

Classes

context_filter

Provides a decorator to filter contexts based on a given feedback and threshold.

A non-finite or unparsable (-1.0) feedback score excludes only the affected context and logs a warning. Other contexts are evaluated normally.

Contexts are evaluated in parallel, but the kept contexts are returned in the order the decorated function produced them, and one GUARDRAIL span per context is emitted in that same order.

PARAMETER DESCRIPTION
feedback

The feedback object to use for filtering.

TYPE: Metric

threshold

A context's score must be strictly above this value when higher_is_better is True, or strictly below it otherwise. Scores equal to the threshold are excluded.

TYPE: float

keyword_for_prompt

Keyword argument to decorator to use for prompt.

TYPE: Optional[str] DEFAULT: None

Example
from trulens.core.guardrails.base import context_filter

feedback = Feedback(provider.context_relevance, name="Context Relevance")

class RAG_from_scratch:
    ...
    @context_filter(feedback, 0.5, "query")
    def retrieve(self, *, query: str) -> list:
        results = vector_store.query(
            query_texts=query,
            n_results=3
        )
        return [doc for sublist in results['documents'] for doc in sublist]
    ...

block_input

Provides a decorator to block input based on a given feedback and threshold.

A non-finite or unparsable (-1.0) feedback score raises ValueError before the decorated function is called.

PARAMETER DESCRIPTION
feedback

The feedback object to use for blocking.

TYPE: Metric

threshold

The minimum feedback value required for a context to be included.

TYPE: float

keyword_for_prompt

Keyword argument to decorator to use for prompt.

TYPE: Optional[str] DEFAULT: None

return_value

The value to return if the input is blocked. Defaults to None.

TYPE: Optional[str] DEFAULT: None

Example
from trulens.core.guardrails.base import block_input

feedback = Feedback(provider.criminality, higher_is_better = False)

class safe_input_chat_app:
    @instrument()
    @block_input(feedback=feedback,
        threshold=0.9,
        keyword_for_prompt="question",
        return_value="I couldn't find an answer to your question.")
    def generate_completion(self, question: str) -> str:
        completion = (
            oai_client.chat.completions.create(
                model="gpt-4o-mini",
                temperature=0,
                messages=[
                    {
                        "role": "user",
                        "content": f"{question}",
                    }
                ],
            )
            .choices[0]
            .message.content
        )
        return completion

block_output

Provides a decorator to block output based on a given feedback and threshold.

A non-finite or unparsable (-1.0) feedback score raises ValueError instead of returning the decorated function's output.

PARAMETER DESCRIPTION
feedback

The feedback object to use for blocking. It must only take a single argument.

TYPE: Metric

threshold

The minimum feedback value required for a context to be included.

TYPE: float

return_value

The value to return if the input is blocked. Defaults to None.

TYPE: Optional[str] DEFAULT: None

Example
from trulens.core.guardrails.base import block_output

feedback = Feedback(provider.criminality, higher_is_better = False)

class safe_output_chat_app:
    @instrument()
    @block_output(feedback = feedback,
        threshold = 0.5,
        return_value = "Sorry, I couldn't find an answer to your question.")
    def chat(self, question: str) -> str:
        completion = (
            oai_client.chat.completions.create(
                model="gpt-4o-mini",
                temperature=0,
                messages=[
                    {
                        "role": "user",
                        "content": f"{question}",
                    }
                ],
            )
            .choices[0]
            .message.content
        )
        return completion