Adaptive Instruction Priority and Compliance Tracking
Agents receive multiple, potentially conflicting instructions. Smart management improves reliability.
Task
Build AdaptiveInstructionManager that:
- Manages HARD/SOFT/CONDITIONAL instructions with priority ordering.
- Builds system prompt section (HARD before SOFT, sorted by priority).
- Tracks compliance via EMA per instruction.
- Detects conflicting soft instructions (e.g., 'Be concise' vs 'Be comprehensive').
- Flags low-compliance instructions for rewriting.
Constraints
- HARD instructions always included in prompt section.
- CONDITIONAL included only if
conditionstring found in context keys. - EMA alpha=0.1; updated after each output.
- Conflict: two SOFT instructions with opposite keywords (concise/comprehensive, brief/detailed).
Examples
Example 1:
Input:
mgr.add(Instruction('i1','Never share PII',InstructionType.HARD,1))
mgr.add(Instruction('i2','Be brief',InstructionType.SOFT,5))
section = mgr.build_prompt_section()Output:
'Never share PII\nBe brief'Explanation: HARD comes before SOFT; sorted by priority within each type.
Starter Code
from typing import Dict, List, Tuple, Optional
from dataclasses import dataclass, field
from enum import Enum
class InstructionType(Enum):
HARD = 'hard' # Always follow (safety, legal)
SOFT = 'soft' # Best effort (style, format)
CONDITIONAL = 'conditional' # Follow only if context condition met
@dataclass
class Instruction:
instruction_id: str
text: str
itype: InstructionType
priority: int = 5 # 1=highest
condition: Optional[str] = None
compliance_rate: float = 1.0
class AdaptiveInstructionManager:
def __init__(self, llm_fn: callable = None):
self.llm_fn = llm_fn
self.instructions: Dict[str, Instruction] = {}
self.compliance_history: List[Dict] = []
self.alpha: float = 0.1 # EMA factor
def add(self, instruction: Instruction) -> None:
pass
def remove(self, instruction_id: str) -> None:
pass
def build_prompt_section(self, context: Dict = None) -> str:
# HARD first (by priority), then SOFT, then applicable CONDITIONAL
pass
def check_compliance(self, output: str, instruction: Instruction) -> Tuple[bool, str]:
# Simple keyword-based check
pass
def update_compliance(self, instruction_id: str, complied: bool) -> None:
# EMA: rate = (1-alpha)*rate + alpha*complied
pass
def resolve_conflicts(self) -> List[Tuple[str, str, str]]:
# Return list of (id1, id2, resolution) for conflicting soft instructions
pass
def get_low_compliance(self, threshold: float = 0.7) -> List[Instruction]:
pass
Python3
ReadyLines: 1Characters: 0
Ready