feat: 自动构建提示词以增强工作流引擎的查询处理能力
- 添加逻辑以在提示词为空时自动构建提示词,支持从知识库结果和工作流输入中提取用户查询 - 增强日志记录,提供关于提示词构建过程的详细信息 - 处理不同场景下的提示词生成,确保用户问题得到有效响应
This commit is contained in:
parent
ad2e6744fb
commit
a73afd7f25
Binary file not shown.
Binary file not shown.
|
|
@ -720,6 +720,66 @@ class WorkflowEngine:
|
||||||
# 准备提示词
|
# 准备提示词
|
||||||
prompt_template = config.get('prompt', '')
|
prompt_template = config.get('prompt', '')
|
||||||
|
|
||||||
|
# 如果提示词为空,尝试自动构建提示词
|
||||||
|
if not prompt_template:
|
||||||
|
# 检查是否有知识库搜索结果
|
||||||
|
previous_outputs = input_data.get('previous_outputs', {})
|
||||||
|
knowledge_base_results = None
|
||||||
|
user_query = None
|
||||||
|
|
||||||
|
# 查找知识库节点的输出
|
||||||
|
for node_id, output in previous_outputs.items():
|
||||||
|
if isinstance(output, dict) and output.get('knowledge_base_id'):
|
||||||
|
knowledge_base_results = output.get('results', [])
|
||||||
|
user_query = output.get('query', '')
|
||||||
|
break
|
||||||
|
|
||||||
|
# 如果没有找到知识库结果,尝试从工作流输入中获取查询
|
||||||
|
if not user_query:
|
||||||
|
workflow_input = input_data.get('workflow_input', {})
|
||||||
|
# 尝试获取第一个非空字符串值作为查询
|
||||||
|
for key, value in workflow_input.items():
|
||||||
|
if isinstance(value, str) and value.strip():
|
||||||
|
user_query = value.strip()
|
||||||
|
break
|
||||||
|
|
||||||
|
# 构建提示词
|
||||||
|
if knowledge_base_results and len(knowledge_base_results) > 0:
|
||||||
|
# 有知识库结果,构建RAG风格的提示词
|
||||||
|
context_parts = []
|
||||||
|
for i, result in enumerate(knowledge_base_results[:5], 1): # 取前5个结果
|
||||||
|
content = result.get('content', '').strip()
|
||||||
|
if content:
|
||||||
|
# 限制每个结果的长度,避免提示词过长
|
||||||
|
max_length = 1000
|
||||||
|
if len(content) > max_length:
|
||||||
|
content = content[:max_length] + "..."
|
||||||
|
context_parts.append(f"【参考文档{i}】\n{content}\n")
|
||||||
|
|
||||||
|
context = "\n\n".join(context_parts)
|
||||||
|
prompt_template = f"""你是一个专业的助手。请仔细阅读以下参考文档,然后回答用户的问题。
|
||||||
|
|
||||||
|
{context}
|
||||||
|
|
||||||
|
【用户问题】
|
||||||
|
{user_query or '请回答上述问题'}
|
||||||
|
|
||||||
|
【重要提示】
|
||||||
|
- 参考文档中包含了与用户问题相关的信息
|
||||||
|
- 请仔细阅读参考文档,提取相关信息来回答用户的问题
|
||||||
|
- 即使文档没有直接定义,也要基于文档中的相关内容进行解释和说明
|
||||||
|
- 如果文档中提到了相关概念、政策、法规等,请基于这些内容进行回答
|
||||||
|
- 回答要准确、详细、有条理,尽量引用文档中的具体内容"""
|
||||||
|
logger.info(f"自动构建RAG提示词,包含 {len(knowledge_base_results)} 个知识库结果,用户问题: {user_query}")
|
||||||
|
elif user_query:
|
||||||
|
# 没有知识库结果,但有用户查询,构建简单提示词
|
||||||
|
prompt_template = user_query
|
||||||
|
logger.info(f"自动使用工作流输入作为提示词: {user_query}")
|
||||||
|
else:
|
||||||
|
# 既没有知识库结果,也没有用户查询
|
||||||
|
prompt_template = "请帮助我处理这个任务。"
|
||||||
|
logger.warning("LLM节点提示词为空,且无法从上下文获取,使用默认提示词")
|
||||||
|
|
||||||
# 检查是否启用变量替换
|
# 检查是否启用变量替换
|
||||||
enable_variable_substitution = config.get('enable_variable_substitution', True)
|
enable_variable_substitution = config.get('enable_variable_substitution', True)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue