mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-04-11 21:53:37 +00:00
- Added Jinja2 template support for various agent responses, including identity, skills, and memory consolidation. - Introduced new templates for evaluating notifications, handling subagent announcements, and managing platform policies. - Updated the agent context and memory modules to utilize the new templating system for improved readability and maintainability. - Added a new dependency on Jinja2 in pyproject.toml.
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
"""Load and render agent system prompt templates (Jinja2) under nanobot/templates/.
|
|
|
|
Agent prompts live in ``templates/agent/`` (pass names like ``agent/identity.md``).
|
|
Shared copy lives under ``agent/_snippets/`` and is included via
|
|
``{% include 'agent/_snippets/....md' %}``.
|
|
"""
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
_TEMPLATES_ROOT = Path(__file__).resolve().parent.parent / "templates"
|
|
|
|
|
|
@lru_cache
|
|
def _environment() -> Environment:
|
|
# Plain-text prompts: do not HTML-escape variable values.
|
|
return Environment(
|
|
loader=FileSystemLoader(str(_TEMPLATES_ROOT)),
|
|
autoescape=False,
|
|
trim_blocks=True,
|
|
lstrip_blocks=True,
|
|
)
|
|
|
|
|
|
def render_template(name: str, *, strip: bool = False, **kwargs: Any) -> str:
|
|
"""Render ``name`` (e.g. ``agent/identity.md``, ``agent/platform_policy.md``) under ``templates/``.
|
|
|
|
Use ``strip=True`` for single-line user-facing strings when the file ends
|
|
with a trailing newline you do not want preserved.
|
|
"""
|
|
text = _environment().get_template(name).render(**kwargs)
|
|
return text.rstrip() if strip else text
|