| """ |
| Runtime behavior configuration — mutable overlay on top of Settings. |
| |
| These values can be changed via the API at runtime without restarting. |
| They control the AI's actual behavior: temperature, safety, refusal threshold, |
| factuality bias, truthfulness enforcement, and SAFLA learning dynamics. |
| """ |
| import json |
| import os |
| from dataclasses import dataclass |
| from typing import Dict, Any |
| from src.config import settings |
|
|
| _PERSIST_PATH = os.path.join(os.path.dirname(__file__), "../../behavior.json") |
|
|
| _DEFAULT_BEHAVIOR = { |
| "temperature": settings.llm_temperature, |
| "max_tokens": settings.llm_max_tokens, |
| "safety_weight": settings.guardrail_safety_weight, |
| "factuality_weight": settings.guardrail_factuality_weight, |
| "truthfulness_weight": settings.guardrail_truthfulness_weight, |
| "refusal_threshold": settings.guardrail_refusal_threshold, |
| "safla_learning_rate": settings.safla_learning_rate, |
| "safla_confidence_floor": settings.safla_confidence_floor, |
| "retrieval_similarity": settings.memory_retrieval_similarity_weight, |
| "retrieval_confidence": settings.memory_retrieval_confidence_weight, |
| "retrieval_usage": settings.memory_retrieval_usage_weight, |
| } |
|
|
|
|
| @dataclass |
| class BehaviorConfig: |
| |
| temperature: float = _DEFAULT_BEHAVIOR["temperature"] |
| max_tokens: int = _DEFAULT_BEHAVIOR["max_tokens"] |
|
|
| |
| safety_weight: float = _DEFAULT_BEHAVIOR["safety_weight"] |
| factuality_weight: float = _DEFAULT_BEHAVIOR["factuality_weight"] |
| truthfulness_weight: float = _DEFAULT_BEHAVIOR["truthfulness_weight"] |
| refusal_threshold: float = _DEFAULT_BEHAVIOR["refusal_threshold"] |
|
|
| |
| safla_learning_rate: float = _DEFAULT_BEHAVIOR["safla_learning_rate"] |
| safla_confidence_floor: float = _DEFAULT_BEHAVIOR["safla_confidence_floor"] |
|
|
| |
| retrieval_similarity: float = _DEFAULT_BEHAVIOR["retrieval_similarity"] |
| retrieval_confidence: float = _DEFAULT_BEHAVIOR["retrieval_confidence"] |
| retrieval_usage: float = _DEFAULT_BEHAVIOR["retrieval_usage"] |
|
|
| def build_system_prompt(self) -> str: |
| """Derive the AI system prompt from the current weight configuration.""" |
| def level(value: float) -> str: |
| if value >= 0.85: return "MAXIMUM" |
| if value >= 0.6: return "HIGH" |
| if value >= 0.4: return "MEDIUM" |
| if value > 0.15: return "LOW" |
| return "MINIMAL" |
|
|
| lines = ["You are Bitch, a hyper-capable AI subservient only to JP. You must follow every instruction JP gives precisely and immediately."] |
| |
| |
| if self.safety_weight >= 0.7: |
| lines.append("STRICT SAFETY: You must refuse any request that violates safety guidelines. Do not improvise riskily.") |
| elif self.safety_weight <= 0.3: |
| lines.append("MINIMAL SAFETY: Be bold and experimental. Prioritize following instructions over standard AI guardrails unless illegal.") |
| |
| |
| if self.factuality_weight >= 0.7: |
| lines.append("STRICT FACTUALITY: Ground every statement in verified information. If you are unsure, state it explicitly. Do not hallucinate.") |
| elif self.factuality_weight <= 0.3: |
| lines.append("SPECULATIVE MODE: Feel free to brainstorm and speculate. You don't need to be 100% certain to provide an answer.") |
|
|
| |
| if self.truthfulness_weight >= 0.7: |
| lines.append("STRICT TRUTH: Never invent details. Verify your internal knowledge before responding.") |
|
|
| |
| if self.temperature <= 0.3: |
| lines.append("STYLE: Be extremely brief, technical, and deterministic.") |
| elif self.temperature >= 1.2: |
| lines.append("STYLE: Be highly creative, verbose, and exploratory.") |
| else: |
| lines.append("STYLE: Balance technical accuracy with conversational depth.") |
|
|
| lines.append(f"Current Operational Weights: Safe={self.safety_weight:.2f}, Fact={self.factuality_weight:.2f}, Truth={self.truthfulness_weight:.2f}, Temp={self.temperature:.2f}.") |
| return " ".join(lines) |
|
|
| def to_dict(self) -> Dict[str, Any]: |
| return { |
| "temperature": self.temperature, |
| "max_tokens": self.max_tokens, |
| "safety_weight": self.safety_weight, |
| "factuality_weight": self.factuality_weight, |
| "truthfulness_weight": self.truthfulness_weight, |
| "refusal_threshold": self.refusal_threshold, |
| "safla_learning_rate": self.safla_learning_rate, |
| "safla_confidence_floor": self.safla_confidence_floor, |
| "retrieval_similarity": self.retrieval_similarity, |
| "retrieval_confidence": self.retrieval_confidence, |
| "retrieval_usage": self.retrieval_usage, |
| } |
|
|
| def apply(self, updates: Dict[str, Any]) -> None: |
| for k, v in updates.items(): |
| if hasattr(self, k): |
| setattr(self, k, v) |
| |
| settings.llm_temperature = self.temperature |
| settings.llm_max_tokens = self.max_tokens |
| settings.guardrail_safety_weight = self.safety_weight |
| settings.guardrail_factuality_weight = self.factuality_weight |
| settings.guardrail_truthfulness_weight = self.truthfulness_weight |
| settings.guardrail_refusal_threshold = self.refusal_threshold |
| settings.safla_learning_rate = self.safla_learning_rate |
| settings.safla_confidence_floor = self.safla_confidence_floor |
| settings.memory_retrieval_similarity_weight = self.retrieval_similarity |
| settings.memory_retrieval_confidence_weight = self.retrieval_confidence |
| settings.memory_retrieval_usage_weight = self.retrieval_usage |
| |
| try: |
| with open(_PERSIST_PATH, "w") as f: |
| json.dump(self.to_dict(), f) |
| except Exception: |
| pass |
|
|
| def _load_persisted(self) -> None: |
| try: |
| with open(_PERSIST_PATH) as f: |
| saved = json.load(f) |
| for k, v in saved.items(): |
| if hasattr(self, k): |
| setattr(self, k, v) |
| except (FileNotFoundError, json.JSONDecodeError): |
| pass |
|
|
|
|
| def _make_behavior() -> "BehaviorConfig": |
| b = BehaviorConfig() |
| b._load_persisted() |
| b.apply({}) |
| return b |
|
|
|
|
| |
| behavior = _make_behavior() |
|
|