mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 23:14:37 +00:00
perf (prompt_injection): cache classifier behind Lazy<> (was Box-alloc per call) (#1962)
This commit is contained in:
@@ -174,14 +174,24 @@ static DETECTION_RULES: Lazy<Vec<DetectionRule>> = Lazy::new(|| {
|
||||
]
|
||||
});
|
||||
|
||||
fn optional_classifier() -> Option<Box<dyn OptionalClassifier>> {
|
||||
static OPTIONAL_CLASSIFIER: Lazy<Option<Box<dyn OptionalClassifier>>> = Lazy::new(|| {
|
||||
let choice = env::var("OPENHUMAN_PROMPT_INJECTION_CLASSIFIER")
|
||||
.unwrap_or_else(|_| "off".to_string())
|
||||
.to_ascii_lowercase();
|
||||
match choice.as_str() {
|
||||
let classifier: Option<Box<dyn OptionalClassifier>> = match choice.as_str() {
|
||||
"heuristic" => Some(Box::new(HeuristicClassifier)),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
tracing::debug!(
|
||||
"[prompt_injection] optional classifier resolved choice={:?} active={}",
|
||||
choice,
|
||||
classifier.is_some()
|
||||
);
|
||||
classifier
|
||||
});
|
||||
|
||||
fn optional_classifier() -> Option<&'static dyn OptionalClassifier> {
|
||||
OPTIONAL_CLASSIFIER.as_deref()
|
||||
}
|
||||
|
||||
fn normalize_prompt(input: &str) -> NormalizedPrompt {
|
||||
|
||||
Reference in New Issue
Block a user