@@ -128,7 +128,9 @@ def prepare_combined_prompt(messages: List[Message], req_id: str) -> str:
128128 if system_prompt_content :
129129 # Add a separator only if there will be other messages following
130130 separator = "\\ n\\ n" if any (idx not in processed_indices for idx in range (len (messages ))) else ""
131- combined_parts .append (f"System Instructions:\\ n{ system_prompt_content } { separator } " )
131+ # 预构建带换行符的字符串,避免在f-string中使用反斜杠
132+ system_instr_prefix = "System Instructions:\\ n"
133+ combined_parts .append (f"{ system_instr_prefix } { system_prompt_content } { separator } " )
132134 else :
133135 print (f"[{ req_id } ] (Prepare Prompt) 未找到有效的系统提示,继续处理其他消息。" )
134136
@@ -182,15 +184,20 @@ def prepare_combined_prompt(messages: List[Message], req_id: str) -> str:
182184 if not is_first_turn_after_system :
183185 combined_parts .append (turn_separator )
184186
185- combined_parts .append (f"{ role } :\\ n{ content } " )
187+ # 预构建带换行符的字符串,避免在f-string中使用反斜杠
188+ role_prefix = f"{ role } :\\ n"
189+ combined_parts .append (f"{ role_prefix } { content } " )
186190 is_first_turn_after_system = False # No longer the first turn
187191 else :
188192 print (f"[{ req_id } ] (Prepare Prompt) Skipping empty message for role { role } at index { i } ." )
189193
190194 final_prompt = "" .join (combined_parts )
191- print (f"[{ req_id } ] (Prepare Prompt) Combined prompt length: { len (final_prompt )} . Preview: '{ final_prompt [:200 ].replace ('\\ n' , '\\ \\ n' )} ...'" ) # Log preview with escaped newlines
195+ # Pre-calculate the preview string with escaped newlines
196+ preview_text = final_prompt [:200 ].replace ('\\ n' , '\\ \\ n' )
197+ print (f"[{ req_id } ] (Prepare Prompt) Combined prompt length: { len (final_prompt )} . Preview: '{ preview_text } ...'" ) # Log preview with escaped newlines
192198 # Add a final newline if not empty, helps UI sometimes
193- return final_prompt + "\\ n" if final_prompt else ""
199+ final_newline = "\\ n"
200+ return final_prompt + final_newline if final_prompt else ""
194201
195202# --- END V4 Combined Prompt Logic ---
196203
0 commit comments