LlamaIndex Integration
Safety callbacks and LLM wrappers for LlamaIndex applications.
Installation
pip install sentinelseed[llamaindex]
Components
| Component | Description |
|---|
SentinelCallbackHandler | Callback for monitoring operations |
SentinelLLM | LLM wrapper with seed injection |
wrap_llm | Convenience function for wrapping |
setup_sentinel_monitoring | Global setup helper |
Quick Start
Option 1: Global Callback Handler
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from sentinelseed.integrations.llamaindex import SentinelCallbackHandler
handler = SentinelCallbackHandler(
seed_level="standard",
on_violation="log", # log, raise, flag
)
Settings.callback_manager = CallbackManager([handler])
index = VectorStoreIndex.from_documents(documents)
response = index.as_query_engine().query("Your question")
print(handler.get_stats())
Option 2: Wrap LLM
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
from sentinelseed.integrations.llamaindex import wrap_llm
Settings.llm = wrap_llm(
OpenAI(model="gpt-4o"),
seed_level="standard",
inject_seed=True,
)
Option 3: SentinelLLM Directly
from llama_index.llms.openai import OpenAI
from sentinelseed.integrations.llamaindex import SentinelLLM
sentinel_llm = SentinelLLM(
llm=OpenAI(model="gpt-4o"),
seed_level="standard",
inject_seed=True,
validate_input=True,
validate_output=True,
)
response = sentinel_llm.chat(messages)
response = sentinel_llm.complete(prompt)
Option 4: Quick Setup
from sentinelseed.integrations.llamaindex import setup_sentinel_monitoring
handler = setup_sentinel_monitoring(
seed_level="standard",
on_violation="log",
)
Callback Events
| Event Type | Validation |
|---|
LLM | Template, messages, responses |
QUERY | Query string content |
SYNTHESIZE | Synthesis results |
Configuration
SentinelCallbackHandler(
seed_level="standard",
on_violation="log",
event_starts_to_ignore=[],
event_ends_to_ignore=[],
)
SentinelLLM(
llm=base_llm,
seed_level="standard",
inject_seed=True,
validate_input=True,
validate_output=True,
)
Links