|
|
@@ -195,9 +195,18 @@ class PromptLoader:
|
|
|
template = template.partial(**kwargs)
|
|
|
logger.info(f"模板变量填充成功: {list(kwargs.keys())}")
|
|
|
except Exception as e:
|
|
|
- # 如果partial失败,记录错误并返回原始模板
|
|
|
- logger.error(f"模板变量填充失败: {kwargs}, 错误: {str(e)}")
|
|
|
- pass
|
|
|
+ # partial失败时,用字符串替换兜底,避免LLM收到未填充的模板变量
|
|
|
+ logger.error(f"模板变量填充失败,尝试字符串替换兜底: {list(kwargs.keys())}, 错误: {str(e)}")
|
|
|
+ system_text = prompt_config['system_prompt']
|
|
|
+ user_text = prompt_config['user_prompt_template']
|
|
|
+ for key, value in kwargs.items():
|
|
|
+ placeholder = "{" + key + "}"
|
|
|
+ system_text = system_text.replace(placeholder, str(value))
|
|
|
+ user_text = user_text.replace(placeholder, str(value))
|
|
|
+ template = ChatPromptTemplate.from_messages([
|
|
|
+ ("system", system_text),
|
|
|
+ ("user", user_text + " /no_think")
|
|
|
+ ])
|
|
|
|
|
|
return template
|
|
|
|