symfonic.agent.prompts¶
prompts ¶
symfonic.agent.prompts -- PromptSection implementations.
Public API::
from symfonic.agent.prompts import (
BRAIN_PROMPT_VERSION,
DBPromptSection,
HMSSystemPromptSection,
MemoryExtractionSection,
)
DBPromptSection
dataclass
¶
DBPromptSection(
name: str,
required: bool = False,
priority: int = 50,
cache_breakpoint: bool = False,
fallback_content: str = "",
section_key: str = "",
cache_ttl_seconds: int = 300,
)
PromptSection that loads versioned Jinja2 templates from PostgreSQL.
The state dict is passed as Jinja2 template context, enabling:
- {{ name }} variable substitution
- {% if user_is_premium %}...{% endif %} conditionals
- {% for item in items %}...{% endfor %} loops
- {{ name | upper }} filters
Gracefully degrades when Redis or DB are unavailable, falling back
to fallback_content rendered through the same Jinja2 pipeline.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Section display name used by PromptBuilder. |
required |
bool
|
Whether a render failure should abort the build. |
priority |
int
|
Lower numbers appear earlier in the assembled prompt. |
fallback_content |
str
|
Jinja2 template string used when DB/cache are unavailable. |
section_key |
str
|
DB lookup key; defaults to |
cache_ttl_seconds |
int
|
Redis TTL for cached template content (default 300s). |
configure ¶
Inject optional Redis and DB dependencies. Returns self for chaining.
Source code in src/symfonic/agent/prompts/db_section.py
render
async
¶
Render the section: cache -> DB -> fallback, all through Jinja2.
Source code in src/symfonic/agent/prompts/db_section.py
HMSSystemPromptSection
dataclass
¶
HMSSystemPromptSection(
name: str = "HMS System Prompt",
required: bool = False,
priority: int = 5,
cache_breakpoint: bool = True,
template_path: Path | None = None,
)
PromptSection that renders the HMS-aware system prompt template.
Substitutes all {{VARIABLE}} placeholders from the PromptState
context. Raises ValueError if any placeholder remains unresolved
after rendering.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Section display name used by PromptBuilder. |
required |
bool
|
Whether a render failure should abort the build. |
priority |
int
|
Lower numbers appear earlier in the assembled prompt. |
template_path |
Path | None
|
Optional override path (file or directory) for the hms_system.txt template. Defaults to the bundled template. |
estimate_tokens
staticmethod
¶
Estimate token count using a conservative 3-chars-per-token heuristic.
The len // 4 heuristic undercounts by 20-30 % on structured /
markdown text. len // 3 is closer to observed tokeniser output
for prompts that contain headings, bullet lists, and JSON blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to estimate token count for. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Estimated number of tokens (conservative upper bound). |
Source code in src/symfonic/agent/prompts/system_prompt.py
render
async
¶
Render the HMS system prompt by substituting placeholders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
PromptState
|
Mapping that must contain values for every
|
required |
Returns:
| Type | Description |
|---|---|
str
|
The fully substituted prompt text. |
Raises:
| Type | Description |
|---|---|
SecurityScopeError
|
If |
ValueError
|
If any |
Source code in src/symfonic/agent/prompts/system_prompt.py
validate_budget ¶
Check whether rendered fits within the token budget.
Does NOT raise — callers decide how to handle over-budget results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rendered
|
str
|
The already-rendered prompt text. |
required |
max_tokens
|
int
|
Maximum allowed token count. |
1000
|
Returns:
| Type | Description |
|---|---|
tuple[bool, int]
|
Tuple of |
Source code in src/symfonic/agent/prompts/system_prompt.py
MemoryExtractionSection
dataclass
¶
MemoryExtractionSection(
name: str = "Memory Extraction Directives",
required: bool = False,
priority: int = 90,
cache_breakpoint: bool = False,
template_path: Path | None = None,
template_name: str = _EXTRACTION_TEMPLATE,
)
PromptSection that renders the memory extraction directives.
Substitutes {{TENANT_ID}} from the PromptState context. Used as
a post-response instruction to guide structured memory mutations.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Section display name used by PromptBuilder. |
required |
bool
|
Whether a render failure should abort the build. |
priority |
int
|
Lower numbers appear earlier in the assembled prompt. |
template_path |
Path | None
|
Optional override path for extraction.txt. |
template_name |
str
|
Bundled template to load when |
render
async
¶
Render the extraction directives with state substitution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
PromptState
|
Must contain |
required |
Returns:
| Type | Description |
|---|---|
str
|
Rendered extraction directive string, or empty string if no |
str
|
tenant is present in state. Byte-for-byte compatible with |
str
|
pre-7.4 behaviour: the KNOWN ENTITIES block is interleaved |
str
|
at the |
str
|
template, exactly where pre-split callers expected it. |
Source code in src/symfonic/agent/prompts/extraction_prompt.py
render_split
async
¶
Render the extraction directives as a stable/volatile pair.
v7.4 Item 15: the extraction protocol's stable surface (schema, label rules, few-shot example, domain directives) lives in L1 (cached prefix) while the per-turn volatile KNOWN ENTITIES block lives in L2. This method returns the two strings separately so the engine can route them to different cache layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
PromptState
|
Same contract as :meth: |
required |
_legacy
|
bool
|
INTERNAL. When |
False
|
Returns:
| Type | Description |
|---|---|
str
|
|
str
|
empty. When no tenant is in state, both are empty. |
Source code in src/symfonic/agent/prompts/extraction_prompt.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |