Normalize product data prompt formatting
This commit is contained in:
@@ -397,6 +397,8 @@ def build_planning_prompt(
|
||||
|
||||
|
||||
def wrap_chat_prompt(system_prompt: str, instruction: str) -> str:
|
||||
system_prompt = prompt_section_without_trailing_newline(system_prompt)
|
||||
instruction = prompt_section_with_one_trailing_newline(instruction)
|
||||
return (
|
||||
f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
||||
f"<|im_start|>user\n{instruction}<|im_end|>\n"
|
||||
@@ -404,6 +406,14 @@ def wrap_chat_prompt(system_prompt: str, instruction: str) -> str:
|
||||
)
|
||||
|
||||
|
||||
def prompt_section_without_trailing_newline(text: str) -> str:
|
||||
return str(text or "").rstrip("\n")
|
||||
|
||||
|
||||
def prompt_section_with_one_trailing_newline(text: str) -> str:
|
||||
return prompt_section_without_trailing_newline(text) + "\n"
|
||||
|
||||
|
||||
def build_training_instruction(record: dict[str, Any], session_num: int, session_time_minutes: int) -> str:
|
||||
turn = record.get("turn") if isinstance(record.get("turn"), dict) else {}
|
||||
query = str(turn.get("query") or "")
|
||||
@@ -414,7 +424,7 @@ def build_training_instruction(record: dict[str, Any], session_num: int, session
|
||||
return (
|
||||
"请参考用户的[当前query]、[对话历史]、[知识注入]、[系统状态]识别出[当前query]的[function]结果,[function]是python的code形式。\n"
|
||||
"[知识注入]\n"
|
||||
f"{json.dumps({'location': str(context.get('location') or ''), 'rag': str(context.get('rag') or '')}, ensure_ascii=False, indent=0)}\n"
|
||||
f"{context_prompt_block(context)}\n"
|
||||
"[系统状态]\n"
|
||||
"{}\n"
|
||||
"[对话历史]\n"
|
||||
@@ -425,6 +435,17 @@ def build_training_instruction(record: dict[str, Any], session_num: int, session
|
||||
)
|
||||
|
||||
|
||||
def context_prompt_block(context: dict[str, Any]) -> str:
|
||||
fields = ("location", "rag")
|
||||
lines = ["{"]
|
||||
for index, field in enumerate(fields):
|
||||
comma = "," if index < len(fields) - 1 else ""
|
||||
value = str(context.get(field) or "")
|
||||
lines.append(f'"{field}": {json.dumps(value, ensure_ascii=False)}{comma}')
|
||||
lines.append("}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def render_history(prev_session: list[Any], current_ts: int | None, session_num: int, session_time_minutes: int) -> str:
|
||||
usable: list[dict[str, Any]] = []
|
||||
for item in prev_session[-session_num:]:
|
||||
|
||||
Reference in New Issue
Block a user