Sprint 9C: prompt_pack.py — epigenetic optimization (prompts before weights)
Browse files
purpose_agent/optimization/prompt_pack.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
prompt_pack.py — Epigenetic optimization: optimize prompts BEFORE touching weights.
|
| 3 |
+
|
| 4 |
+
A PromptPack is the compiled output of optimization — ready to deploy:
|
| 5 |
+
- Optimized system instructions
|
| 6 |
+
- Selected skills (highest fitness)
|
| 7 |
+
- Few-shot examples (from best traces)
|
| 8 |
+
- Tool policies
|
| 9 |
+
- Output schema hints
|
| 10 |
+
- Token budget compliance guaranteed
|
| 11 |
+
"""
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
from dataclasses import dataclass, field
|
| 14 |
+
from typing import Any
|
| 15 |
+
from purpose_agent.skills.schema import SkillCard
|
| 16 |
+
from purpose_agent.memory_homeostasis import MemoryBudget
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class PromptPack:
|
| 21 |
+
"""Compiled prompt optimization output — deployable artifact."""
|
| 22 |
+
name: str = "default"
|
| 23 |
+
version: int = 1
|
| 24 |
+
system_instructions: list[str] = field(default_factory=list)
|
| 25 |
+
skills: list[dict[str, Any]] = field(default_factory=list)
|
| 26 |
+
examples: list[dict[str, str]] = field(default_factory=list) # {input, output}
|
| 27 |
+
tool_policies: list[str] = field(default_factory=list)
|
| 28 |
+
output_hints: list[str] = field(default_factory=list)
|
| 29 |
+
token_estimate: int = 0
|
| 30 |
+
metadata: dict[str, Any] = field(default_factory=dict)
|
| 31 |
+
|
| 32 |
+
def to_system_prompt(self) -> str:
|
| 33 |
+
"""Compile into a single system prompt string."""
|
| 34 |
+
parts = []
|
| 35 |
+
if self.system_instructions:
|
| 36 |
+
parts.append("## Instructions\n" + "\n".join(f"- {i}" for i in self.system_instructions))
|
| 37 |
+
if self.skills:
|
| 38 |
+
parts.append("## Skills\n" + "\n".join(
|
| 39 |
+
f"- {s.get('trigger','')}: {s.get('procedure','')}" for s in self.skills[:5]))
|
| 40 |
+
if self.examples:
|
| 41 |
+
parts.append("## Examples")
|
| 42 |
+
for i, ex in enumerate(self.examples[:3], 1):
|
| 43 |
+
parts.append(f"### Example {i}\nInput: {ex.get('input','')}\nOutput: {ex.get('output','')}")
|
| 44 |
+
if self.tool_policies:
|
| 45 |
+
parts.append("## Tool Policies\n" + "\n".join(f"- {p}" for p in self.tool_policies))
|
| 46 |
+
return "\n\n".join(parts)
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def total_chars(self) -> int:
|
| 50 |
+
return len(self.to_system_prompt())
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class PromptPackBuilder:
|
| 54 |
+
"""
|
| 55 |
+
Builds optimized PromptPacks from skills, traces, and memory.
|
| 56 |
+
|
| 57 |
+
Usage:
|
| 58 |
+
builder = PromptPackBuilder(budget=MemoryBudget(max_injected_tokens=500))
|
| 59 |
+
pack = builder.build(
|
| 60 |
+
skills=active_skills,
|
| 61 |
+
instructions=["Always validate input"],
|
| 62 |
+
examples=[{"input": "fib(5)", "output": "5"}],
|
| 63 |
+
)
|
| 64 |
+
system_prompt = pack.to_system_prompt()
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
def __init__(self, budget: MemoryBudget | None = None):
|
| 68 |
+
self.budget = budget or MemoryBudget()
|
| 69 |
+
|
| 70 |
+
def build(
|
| 71 |
+
self,
|
| 72 |
+
skills: list[SkillCard] | None = None,
|
| 73 |
+
instructions: list[str] | None = None,
|
| 74 |
+
examples: list[dict[str, str]] | None = None,
|
| 75 |
+
tool_policies: list[str] | None = None,
|
| 76 |
+
) -> PromptPack:
|
| 77 |
+
"""Build a token-budget-compliant PromptPack."""
|
| 78 |
+
pack = PromptPack(
|
| 79 |
+
system_instructions=instructions or [],
|
| 80 |
+
tool_policies=tool_policies or [],
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Add skills sorted by fitness, under budget
|
| 84 |
+
token_used = self.budget.estimate_tokens(pack.to_system_prompt())
|
| 85 |
+
max_tokens = self.budget.max_injected_tokens
|
| 86 |
+
|
| 87 |
+
if skills:
|
| 88 |
+
sorted_skills = sorted(skills, key=lambda s: -s.fitness_score)
|
| 89 |
+
for skill in sorted_skills:
|
| 90 |
+
skill_text = f"{skill.trigger}: {' → '.join(skill.procedure[:3])}"
|
| 91 |
+
skill_tokens = self.budget.estimate_tokens(skill_text)
|
| 92 |
+
if token_used + skill_tokens > max_tokens:
|
| 93 |
+
break
|
| 94 |
+
pack.skills.append({"trigger": skill.trigger, "procedure": skill_text,
|
| 95 |
+
"fitness": skill.fitness_score})
|
| 96 |
+
token_used += skill_tokens
|
| 97 |
+
|
| 98 |
+
# Add examples under remaining budget
|
| 99 |
+
if examples:
|
| 100 |
+
for ex in examples[:5]:
|
| 101 |
+
ex_text = f"{ex.get('input','')} {ex.get('output','')}"
|
| 102 |
+
ex_tokens = self.budget.estimate_tokens(ex_text)
|
| 103 |
+
if token_used + ex_tokens > max_tokens:
|
| 104 |
+
break
|
| 105 |
+
pack.examples.append(ex)
|
| 106 |
+
token_used += ex_tokens
|
| 107 |
+
|
| 108 |
+
pack.token_estimate = token_used
|
| 109 |
+
return pack
|