refactor avatar info

This commit is contained in:
bridge
2025-10-12 02:16:39 +08:00
parent fcbd829303
commit c3cf37f468
6 changed files with 71 additions and 56 deletions

View File

@@ -8,13 +8,20 @@ import json5
from src.utils.config import CONFIG
from src.utils.io import read_txt
from src.run.log import log_llm_call
from src.utils.strings import intentify_prompt_infos
def get_prompt(template: str, infos: dict) -> str:
"""
根据模板,获取提示词
"""
prompt_template = PromptTemplate(template=template)
return prompt_template.format(**infos)
# 将 dict/list 等结构化对象转为 JSON 字符串
# 策略:
# - avatar_infos: 不包装 intent模板里已经说明是 dict[Name, info]
# - general_action_infos: 强制包装 intent 以凸显语义
# - 其他容器类型:默认包装 intent
processed_infos = intentify_prompt_infos(infos)
return prompt_template.format(**processed_infos)
def call_llm(prompt: str, mode="normal") -> str:

View File

@@ -1,10 +1,22 @@
def to_snake_case(name: str) -> str:
"""将驼峰/帕斯卡命名转换为蛇形命名。"""
chars = []
for i, ch in enumerate(name):
if ch.isupper() and i > 0:
chars.append('_')
chars.append(ch.lower())
return ''.join(chars)
def to_json_str_with_intent(data, unescape_newlines: bool = True) -> str:
"""
将任意数据包装为带有 intent 的 JSON 字符串,便于在 Prompt 中明确语义。
结构:{"intent": intent, "data": data}
- 使用缩进与 ensure_ascii=False保证可读性与中文不转义
"""
import json
s = json.dumps(data, ensure_ascii=False, indent=2)
if unescape_newlines:
s = s.replace("\\n", "\n")
return s
def intentify_prompt_infos(infos: dict) -> dict:
processed: dict = dict(infos or {})
if "avatar_infos" in processed:
processed["avatar_infos"] = to_json_str_with_intent(processed["avatar_infos"])
if "general_action_infos" in processed:
processed["general_action_infos"] = to_json_str_with_intent(processed["general_action_infos"])
return processed