优化成员画像工作流调用稳定性并禁止写入兜底垃圾数据
- 将 member_context 的 Dify workflow 调用响应模式切换为 streaming,提高长耗时工作流的连接稳定性 - 将成员画像工作流请求超时时间从 60 秒提升到 240 秒,适配当前群日批量提取任务的实际耗时 - 扩展 DifyClient,支持 workflow streaming 响应解析,在流式场景下尽量提取最终输出或增量文本 - 调整群日画像提取逻辑,AI 未返回成员有效结构化结果时不再写入 fallback 通用数据,而是直接跳过,等待下次任务重试 - 调整周/月周期摘要生成逻辑,AI 未返回有效结果时不再使用本地兜底拼装摘要,避免写入低质量周期画像 - 删除成员日摘要和周期摘要对应的 fallback 生成逻辑,彻底阻断这类无意义垃圾画像继续入库 - 新增跳过日志,明确标记哪些成员或周期摘要因为未提取到有效 AI 结果而未入库,便于后续诊断稳定性问题
This commit is contained in:
@@ -20,6 +20,7 @@ class DifyClient:
|
||||
default_endpoint = "workflows/run" if self.mode == "workflow" else "completion-messages"
|
||||
self.endpoint = str(api_config.get("endpoint", default_endpoint)).lstrip("/")
|
||||
self.workflow_output_key = str(api_config.get("workflow_output_key", "text")).strip()
|
||||
self.response_mode = str(api_config.get("response_mode", "blocking")).strip().lower()
|
||||
|
||||
def is_available(self) -> bool:
|
||||
return self.enabled and bool(self.base_url and self.api_key)
|
||||
@@ -41,27 +42,81 @@ class DifyClient:
|
||||
|
||||
payload = {
|
||||
"inputs": payload_inputs,
|
||||
"response_mode": "blocking",
|
||||
"response_mode": self.response_mode,
|
||||
"user": user,
|
||||
}
|
||||
url = f"{self.base_url}/{self.endpoint}"
|
||||
try:
|
||||
self.LOG.info(f"[成员交互摘要][Dify] 发起请求: mode={self.mode}, endpoint={self.endpoint}, tag={tag}")
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=self.timeout)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
parsed = self._parse_response(data)
|
||||
self.LOG.info(
|
||||
f"[成员交互摘要][Dify] 发起请求: mode={self.mode}, response_mode={self.response_mode}, "
|
||||
f"endpoint={self.endpoint}, tag={tag}"
|
||||
)
|
||||
if self.response_mode == "streaming":
|
||||
parsed = self._run_streaming(url, headers, payload, tag)
|
||||
else:
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=self.timeout)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
parsed = self._parse_response(data)
|
||||
if parsed is not None:
|
||||
return parsed
|
||||
self.LOG.warning(
|
||||
f"[成员交互摘要][Dify] 响应内容为空: mode={self.mode}, tag={tag}, "
|
||||
f"response_preview={(response.text or '')[:300]}"
|
||||
)
|
||||
self.LOG.warning(f"[成员交互摘要][Dify] 响应内容为空: mode={self.mode}, tag={tag}")
|
||||
return None
|
||||
except Exception as e:
|
||||
self.LOG.warning(f"[成员交互摘要][Dify] 请求失败: mode={self.mode}, tag={tag}, error={e}")
|
||||
return None
|
||||
|
||||
def _run_streaming(self, url: str, headers: Dict, payload: Dict, tag: str) -> Optional[Dict]:
|
||||
with requests.post(url, headers=headers, json=payload, timeout=self.timeout, stream=True) as response:
|
||||
response.raise_for_status()
|
||||
event_name = ""
|
||||
text_fragments = []
|
||||
final_payload = None
|
||||
|
||||
for raw_line in response.iter_lines(decode_unicode=True):
|
||||
if raw_line is None:
|
||||
continue
|
||||
line = str(raw_line).strip()
|
||||
if not line:
|
||||
continue
|
||||
if line.startswith("event:"):
|
||||
event_name = line[6:].strip()
|
||||
continue
|
||||
if not line.startswith("data:"):
|
||||
continue
|
||||
|
||||
data_text = line[5:].strip()
|
||||
if not data_text or data_text == "[DONE]":
|
||||
continue
|
||||
try:
|
||||
chunk = json.loads(data_text)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
candidate_text = self._extract_stream_text(chunk)
|
||||
if candidate_text:
|
||||
text_fragments.append(candidate_text)
|
||||
|
||||
chunk_event = str(chunk.get("event") or event_name or "").strip()
|
||||
if chunk_event in {"workflow_finished", "message_end"}:
|
||||
final_payload = chunk
|
||||
|
||||
if final_payload:
|
||||
parsed = self._parse_response(final_payload)
|
||||
if parsed and parsed.get("text"):
|
||||
return parsed
|
||||
|
||||
text = "".join(fragment for fragment in text_fragments if fragment)
|
||||
if text:
|
||||
return {
|
||||
"text": text.strip(),
|
||||
"usage": {},
|
||||
"raw": final_payload or {},
|
||||
}
|
||||
|
||||
self.LOG.warning(f"[成员交互摘要][Dify] 流式响应未产出有效内容: tag={tag}")
|
||||
return None
|
||||
|
||||
def _parse_response(self, data: Dict) -> Optional[Dict]:
|
||||
if self.mode == "workflow":
|
||||
return self._parse_workflow_response(data)
|
||||
@@ -105,6 +160,22 @@ class DifyClient:
|
||||
"raw": data,
|
||||
}
|
||||
|
||||
def _extract_stream_text(self, chunk: Dict) -> str:
|
||||
if not isinstance(chunk, dict):
|
||||
return ""
|
||||
payload = (chunk.get("data") or {}) if isinstance(chunk.get("data"), dict) else {}
|
||||
outputs = payload.get("outputs", {}) if isinstance(payload.get("outputs"), dict) else {}
|
||||
|
||||
for key in filter(None, [self.workflow_output_key, "text", "answer", "result_json", "result"]):
|
||||
if outputs.get(key) is not None:
|
||||
return self._stringify_output(outputs.get(key))
|
||||
|
||||
for key in ("text", "answer"):
|
||||
if chunk.get(key) is not None:
|
||||
return self._stringify_output(chunk.get(key))
|
||||
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def _stringify_output(value) -> str:
|
||||
if value is None:
|
||||
|
||||
Reference in New Issue
Block a user