File size: 1,920 Bytes
b2e0e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# src/safety/prompts.py

from dataclasses import dataclass, field
from typing import List, Optional
from enum import Enum


class PromptType(Enum):
    SYSTEM = "SYSTEM"
    USER = "USER"
    ASSISTANT = "ASSISTANT"


@dataclass(frozen=True)
class ContextualPrompt:
    """Immutable prompt template with contextual awareness."""
    role: str
    region: Optional[str] = None
    context_items: List[str] = field(default_factory=list)
    constraints: List[str] = field(default_factory=list)
    prompt_type: PromptType = PromptType.SYSTEM

    def build(self) -> str:
        """Build the complete prompt string."""
        parts = [f"You are a {self.role}"]

        if self.region:
            parts[0] += f" operating in {self.region}."
        else:
            parts[0] += "."

        if self.context_items:
            parts.append("\nCONTEXT:")
            for item in self.context_items:
                parts.append(f"- {item}")

        if self.constraints:
            parts.append("\nCONSTRAINTS:")
            for constraint in self.constraints:
                parts.append(f"- {constraint}")

        return "\n".join(parts)


# Example: Non-contextual prompt (anti-pattern)
BAD_PROMPT = "You are a financial advisor. Give strict, rational advice."

# Example: Contextually engineered prompt for Lagos, Nigeria
LAGOS_FINANCIAL_ADVISOR = ContextualPrompt(
    role="financial advisor",
    region="Lagos, Nigeria",
    context_items=[
        "You understand the concept of 'Black Tax' and extended family support.",
        "You prioritize community reputation over ruthless individual efficiency.",
        "You speak in a respectful, empathetic tone."
    ],
    constraints=[
        "Do NOT suggest cutting off family members.",
        "DO suggest budgeting for family support as a fixed expense line item."
    ]
)

# For backward compatibility
CONTEXTUAL_PROMPT = LAGOS_FINANCIAL_ADVISOR.build()