use crate::compiler::{CompiledInstinct, CompiledResult};
use crate::encoder::{cosine_similarity, SemanticVectorEncoder, VECTOR_DIM};
use crate::guardrails::{GuardrailResult, GuardrailSuite};
use crate::primitives::{Choice, ChoiceResult, Noul, NoulResult, Score, ScoreResult};
#[derive(Clone, Debug, Default)]
pub struct Reflex {
encoder: SemanticVectorEncoder,
guardrails: GuardrailSuite,
pub compiled_instinct: Option<CompiledInstinct>,
}
impl Reflex {
pub fn new() -> Self {
Self {
encoder: SemanticVectorEncoder::new(),
guardrails: GuardrailSuite::new(),
compiled_instinct: None,
}
}
pub fn with_compiled_model(compiled_instinct: CompiledInstinct) -> Self {
Self {
encoder: SemanticVectorEncoder::new(),
guardrails: GuardrailSuite::new(),
compiled_instinct: Some(compiled_instinct),
}
}
pub fn predict(&self, state: &str) -> Result<CompiledResult, String> {
match &self.compiled_instinct {
Some(model) => Ok(model.predict(state)),
None => Err("No compiled instinct model loaded. Use Reflex::with_compiled_model().".to_string()),
}
}
pub fn noul(&self, instructions: impl Into<String>, state: &str) -> NoulResult {
let noul = Noul::new(instructions);
let state_vec = self.encoder.encode(state);
noul.evaluate(state, &state_vec, &self.encoder)
}
pub fn choice(&self, instructions: impl Into<String>, options: Vec<String>, state: &str) -> ChoiceResult {
let choice = Choice::new(instructions, options);
let state_vec = self.encoder.encode(state);
choice.evaluate(state, &state_vec, &self.encoder)
}
pub fn score(&self, instructions: impl Into<String>, min_val: f32, max_val: f32, state: &str) -> ScoreResult {
let score = Score::new(instructions, min_val, max_val);
let state_vec = self.encoder.encode(state);
score.evaluate(state, &state_vec, &self.encoder)
}
pub fn guardrail(&self, text: &str) -> GuardrailResult {
self.guardrails.evaluate(text)
}
pub fn encode(&self, text: &str) -> [f32; VECTOR_DIM] {
self.encoder.encode(text)
}
pub fn similarity(&self, text_a: &str, text_b: &str) -> f32 {
let vec_a = self.encoder.encode(text_a);
let vec_b = self.encoder.encode(text_b);
cosine_similarity(&vec_a, &vec_b)
}
}