LlamaIndex Integration
Add hallucination detection and content verification to any LlamaIndex RAG pipeline using the TruthVouch Trust API. Verify retrieval results and LLM outputs for factual accuracy.
Installation
pip install llama-index-truthvouchRequires Python 3.9+ and llama-index-core >= 0.10.
Quick Start
import osos.environ["TRUTHVOUCH_API_KEY"] = "your-api-key"
from truthvouch_llamaindex import TruthVouchResponseEvaluator
evaluator = TruthVouchResponseEvaluator(threshold=0.8)result = await evaluator.evaluate("The Eiffel Tower is in Paris, France.")print(result.passing) # Trueprint(result.score) # e.g. 0.96print(result.feedback) # human-readable explanationComponents
TrustApiClient
Low-level async/sync client for the Trust API verify endpoint:
from truthvouch_llamaindex import TrustApiClient
client = TrustApiClient(api_key="your-api-key")result = client.verify_sync("The Eiffel Tower is in Paris.", mode="standard")print(result.trust_score) # 0.97TruthVouchNodePostprocessor
Filters retrieved nodes by trust score before they reach the LLM:
from llama_index.core import VectorStoreIndexfrom truthvouch_llamaindex import TruthVouchNodePostprocessor
postprocessor = TruthVouchNodePostprocessor(threshold=0.75)query_engine = index.as_query_engine(node_postprocessors=[postprocessor])response = query_engine.query("What is the capital of France?")TruthVouchResponseEvaluator
Evaluate a RAG response string for factual accuracy:
from truthvouch_llamaindex import TruthVouchResponseEvaluator
evaluator = TruthVouchResponseEvaluator(threshold=0.8)result = await evaluator.evaluate("Some response text")if not result.passing: print("Hallucination risk:", result.feedback)TruthVouchQueryEngine
Wrapper query engine that auto-verifies every response:
from truthvouch_llamaindex import TruthVouchQueryEngine
engine = TruthVouchQueryEngine(inner_query_engine=base_engine, threshold=0.8)response = engine.query("Tell me about the Eiffel Tower.")print(response.metadata["trust_score"])Configuration
| Parameter | Environment Variable | Default |
|---|---|---|
api_key | TRUTHVOUCH_API_KEY | (required) |
base_url | — | http://localhost:5004/api/v1/trust |
threshold | — | 0.8 |
mode | — | spot_check |
License
Apache-2.0