37 lines
933 B
Python
37 lines
933 B
Python
"""PromptSection - 模块化 Prompt 段落"""
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class PromptSection:
|
|
"""Prompt 段落定义
|
|
|
|
将 Prompt 分为 5 个标准段落,支持变量注入和 Token 预算管理。
|
|
"""
|
|
identity: str = ""
|
|
context: str = ""
|
|
instructions: str = ""
|
|
constraints: str = ""
|
|
output_format: str = ""
|
|
examples: str = ""
|
|
|
|
def render(self, variables: dict | None = None) -> str:
|
|
"""渲染段落,替换变量"""
|
|
text = "\n\n".join(
|
|
part for part in [
|
|
self.identity,
|
|
self.context,
|
|
self.instructions,
|
|
self.constraints,
|
|
self.output_format,
|
|
self.examples,
|
|
] if part
|
|
)
|
|
|
|
if variables:
|
|
for key, value in variables.items():
|
|
text = text.replace(f"${{{key}}}", str(value))
|
|
|
|
return text
|