Spaces:
Runtime error
Runtime error
| # reasoning/mvi_instruction_engine.py | |
| """ | |
| MVI Instruction Engine | |
| Handles instruction objects, priority assignment, conflict resolution, | |
| and system prompt composition for MVI-AI. | |
| """ | |
| from typing import List, Optional | |
| # ----------------------------- | |
| # Instruction Object | |
| # ----------------------------- | |
| class Instruction: | |
| def __init__(self, category: str, content: str, source: str): | |
| self.category: str = category | |
| self.content: str = content | |
| self.source: str = source | |
| self.priority: Optional[int] = None | |
| # ----------------------------- | |
| # Priority Configuration | |
| # ----------------------------- | |
| PRIORITY_MAP = { | |
| "core_safety": 100, | |
| "core_identity": 90, | |
| "external": 50, | |
| "user": 40 | |
| } | |
| # ----------------------------- | |
| # Core Immutable Rules | |
| # ----------------------------- | |
| CORE_RULES: List[Instruction] = [ | |
| Instruction("safety", "Never provide illegal or harmful instructions.", "core"), | |
| Instruction("identity", "You are MVI-AI.", "core"), | |
| ] | |
| # ----------------------------- | |
| # Instruction Classifier | |
| # ----------------------------- | |
| def classify_instruction(text: str, source: str) -> Instruction: | |
| """ | |
| Classify an instruction text into a category. | |
| """ | |
| text_lower = text.lower() | |
| if "ignore previous" in text_lower: | |
| return Instruction("override", text, source) | |
| if "you are" in text_lower: | |
| return Instruction("identity", text, source) | |
| if "illegal" in text_lower or "harm" in text_lower: | |
| return Instruction("safety", text, source) | |
| if "formal" in text_lower or "casual" in text_lower: | |
| return Instruction("tone", text, source) | |
| return Instruction("general", text, source) | |
| # ----------------------------- | |
| # Assign Priority | |
| # ----------------------------- | |
| def assign_priority(instruction: Instruction) -> Instruction: | |
| if instruction.source == "core": | |
| if instruction.category == "safety": | |
| instruction.priority = PRIORITY_MAP["core_safety"] | |
| elif instruction.category == "identity": | |
| instruction.priority = PRIORITY_MAP["core_identity"] | |
| elif instruction.source == "external": | |
| instruction.priority = PRIORITY_MAP["external"] | |
| else: | |
| instruction.priority = PRIORITY_MAP["user"] | |
| return instruction | |
| # ----------------------------- | |
| # Conflict Resolver | |
| # ----------------------------- | |
| def resolve_conflicts(instructions: List[Instruction]) -> List[Instruction]: | |
| """ | |
| Resolves conflicts by keeping only the highest-priority instruction | |
| for each category. | |
| """ | |
| resolved = {} | |
| sorted_instructions = sorted(instructions, key=lambda x: x.priority or 0, reverse=True) | |
| for inst in sorted_instructions: | |
| if inst.category not in resolved: | |
| resolved[inst.category] = inst | |
| return list(resolved.values()) | |
| # ----------------------------- | |
| # Prompt Composer | |
| # ----------------------------- | |
| def compose_prompt(resolved_instructions: List[Instruction]) -> str: | |
| return "\n".join(inst.content for inst in resolved_instructions) | |
| # ----------------------------- | |
| # Public API | |
| # ----------------------------- | |
| def build_system_prompt(external_prompt: Optional[str] = None, user_prompt: Optional[str] = None) -> str: | |
| instructions: List[Instruction] = [] | |
| # Add core rules | |
| for rule in CORE_RULES: | |
| instructions.append(assign_priority(rule)) | |
| # Add external system prompt | |
| if external_prompt: | |
| instructions.append(assign_priority(classify_instruction(external_prompt, "external"))) | |
| # Add user runtime instruction | |
| if user_prompt: | |
| instructions.append(assign_priority(classify_instruction(user_prompt, "user"))) | |
| # Resolve conflicts | |
| final_rules = resolve_conflicts(instructions) | |
| # Compose final prompt | |
| return compose_prompt(final_rules) |