trulens.apps.llamaindex.guardrails¶
trulens.apps.llamaindex.guardrails
¶
Classes¶
WithFeedbackFilterNodes
¶
Bases: RetrieverQueryEngine
Attributes¶
threshold
instance-attribute
¶
A BaseQueryEngine that filters documents using a minimum threshold on a feedback function before returning them.
PARAMETER | DESCRIPTION |
---|---|
feedback |
use this feedback function to score each document.
|
threshold |
and keep documents only if their feedback value is at least this threshold.
|
"Using TruLens guardrail context filters with Llama-Index"
from trulens.apps.llamaindex.guardrails import WithFeedbackFilterNodes
# note: feedback function used for guardrail must only return a score, not also reasons
feedback = (
Feedback(provider.context_relevance)
.on_input()
.on(context)
)
filtered_query_engine = WithFeedbackFilterNodes(query_engine, feedback=feedback, threshold=0.5)
tru_recorder = TruLlama(filtered_query_engine,
app_name="LlamaIndex_App",
app_version="v1_filtered"
)
with tru_recorder as recording:
llm_response = filtered_query_engine.query("What did the author do growing up?")
Functions¶
query
¶
query(query: QueryBundle, **kwargs) -> List[NodeWithScore]
An extended query method that will:
- Query the engine with the given query bundle (like before).
- Evaluate nodes with a specified feedback function.
- Filter out nodes that do not meet the minimum threshold.
- Synthesize with only the filtered nodes.
PARAMETER | DESCRIPTION |
---|---|
query |
The query bundle to search for relevant nodes.
TYPE:
|
**kwargs |
additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
List[NodeWithScore]
|
List[NodeWithScore]: a list of filtered, relevant nodes. |