pub struct GuardrailHook { /* private fields */ }Expand description
An ObservabilityHook that runs guardrails on LLM input and output.
Built with a builder pattern. Input guardrails fire on HookEvent::PreLlmCall,
output guardrails fire on HookEvent::PostLlmCall. All other events pass
through with HookAction::Continue.
§Guardrail result mapping
GuardrailResult::Pass->HookAction::ContinueGuardrailResult::Tripwire->HookAction::Terminatewith the reasonGuardrailResult::Warn-> logs a warning viatracing::warn!and continues
§Example
ⓘ
use neuron_runtime::{GuardrailHook, InputGuardrail, OutputGuardrail, GuardrailResult};
struct NoSecrets;
impl InputGuardrail for NoSecrets {
fn check(&self, input: &str) -> impl Future<Output = GuardrailResult> + Send {
async move {
if input.contains("sk-") {
GuardrailResult::Tripwire("secret in input".to_string())
} else {
GuardrailResult::Pass
}
}
}
}
let hook = GuardrailHook::new()
.input_guardrail(NoSecrets);Implementations§
Source§impl GuardrailHook
impl GuardrailHook
Sourcepub fn input_guardrail<G>(self, guardrail: G) -> Selfwhere
G: ErasedInputGuardrail + 'static,
pub fn input_guardrail<G>(self, guardrail: G) -> Selfwhere
G: ErasedInputGuardrail + 'static,
Add an input guardrail.
Input guardrails run on HookEvent::PreLlmCall, checking the last
user message text in the request.
Sourcepub fn output_guardrail<G>(self, guardrail: G) -> Selfwhere
G: ErasedOutputGuardrail + 'static,
pub fn output_guardrail<G>(self, guardrail: G) -> Selfwhere
G: ErasedOutputGuardrail + 'static,
Add an output guardrail.
Output guardrails run on HookEvent::PostLlmCall, checking the
assistant response text from the response message.
Trait Implementations§
Source§impl Default for GuardrailHook
impl Default for GuardrailHook
Source§impl ObservabilityHook for GuardrailHook
impl ObservabilityHook for GuardrailHook
Source§fn on_event(
&self,
event: HookEvent<'_>,
) -> impl Future<Output = Result<HookAction, HookError>> + WasmCompatSend
fn on_event( &self, event: HookEvent<'_>, ) -> impl Future<Output = Result<HookAction, HookError>> + WasmCompatSend
Called for each event in the agentic loop.
Auto Trait Implementations§
impl Freeze for GuardrailHook
impl !RefUnwindSafe for GuardrailHook
impl Send for GuardrailHook
impl Sync for GuardrailHook
impl Unpin for GuardrailHook
impl UnsafeUnpin for GuardrailHook
impl !UnwindSafe for GuardrailHook
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more