32 lines
1.5 KiB
Python
32 lines
1.5 KiB
Python
# 预设提示词模板库
|
||
# 使用Python的format语法进行参数替换,例如 {keyword}
|
||
|
||
PROMPTS = {
|
||
"flower_shop": "一间有着精致窗户的花店,漂亮的木质门,摆放着{flower_type},风格为{style}",
|
||
"landscape": "宏伟的{season}自然风景,包含{element},光线为{lighting},高分辨率,写实风格",
|
||
"cyberpunk_city": "赛博朋克风格的未来城市,霓虹灯闪烁,{weather}天气,街道上有{vehicle}",
|
||
"portrait": "一张{gender}的肖像照,{expression}表情,背景是{background},专业摄影布光",
|
||
"default": "一间有着精致窗户的花店,漂亮的木质门,摆放着花朵"
|
||
}
|
||
|
||
def get_prompt(template_id: str, **kwargs) -> str:
|
||
"""
|
||
获取并格式化提示词
|
||
:param template_id: 提示词模板ID
|
||
:param kwargs: 替换参数
|
||
:return: 格式化后的提示词
|
||
"""
|
||
template = PROMPTS.get(template_id)
|
||
if not template:
|
||
return None
|
||
|
||
try:
|
||
# 使用 safe_substitute 避免参数缺失报错?
|
||
# Python的format如果缺参数会报错,这里直接用format,让调用者负责提供完整参数
|
||
# 或者我们可以在这里处理默认值
|
||
return template.format(**kwargs)
|
||
except KeyError as e:
|
||
# 如果缺少参数,抛出异常或返回包含未替换占位符的字符串
|
||
# 这里为了简单,如果出错,我们尽量保留原样或者报错
|
||
raise ValueError(f"缺少提示词参数: {e}")
|