调整总结逻辑,支持流式回复,并且加入深度思考兼容
This commit is contained in:
@@ -229,8 +229,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
# 设置请求头
|
# 设置请求头
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self._api_key}",
|
"Authorization": f"Bearer {self._api_key}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json"
|
||||||
"Accept": "text/event-stream"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -239,65 +238,35 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
async with session.post(self._api_url, headers=headers, json=data) as response:
|
async with session.post(self._api_url, headers=headers, json=data) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
self.LOG.info(f"Dify API响应状态码: {response.status}")
|
self.LOG.info(f"Dify API响应状态码: {response.status}")
|
||||||
ctype = response.headers.get("Content-Type", "")
|
answer_parts = []
|
||||||
answer = ""
|
current_event = None
|
||||||
|
data_buffer = []
|
||||||
|
async for raw_line in response.content:
|
||||||
|
line = raw_line.decode("utf-8").rstrip("\n")
|
||||||
|
if not line:
|
||||||
|
if data_buffer:
|
||||||
|
payload_str = "\n".join(data_buffer)
|
||||||
|
try:
|
||||||
|
payload = json.loads(payload_str)
|
||||||
|
if current_event == "message":
|
||||||
|
chunk = payload.get("answer") or ""
|
||||||
|
if chunk:
|
||||||
|
answer_parts.append(chunk)
|
||||||
|
except Exception as e:
|
||||||
|
self.LOG.debug(f"SSE数据解析失败: {e}")
|
||||||
|
current_event = None
|
||||||
|
data_buffer = []
|
||||||
|
continue
|
||||||
|
if line.startswith(":"):
|
||||||
|
continue
|
||||||
|
if line.startswith("event:"):
|
||||||
|
current_event = line.split(":", 1)[1].strip()
|
||||||
|
continue
|
||||||
|
if line.startswith("data:"):
|
||||||
|
data_buffer.append(line[5:].strip())
|
||||||
|
continue
|
||||||
|
answer = "".join(answer_parts)
|
||||||
spath = None
|
spath = None
|
||||||
tokens_info = ""
|
|
||||||
if "text/event-stream" in ctype:
|
|
||||||
answer_parts = []
|
|
||||||
current_event = None
|
|
||||||
data_buffer = []
|
|
||||||
async for raw_line in response.content:
|
|
||||||
line = raw_line.decode("utf-8", errors="ignore").rstrip("\r\n")
|
|
||||||
if not line:
|
|
||||||
if data_buffer:
|
|
||||||
payload_str = "\n".join(data_buffer)
|
|
||||||
try:
|
|
||||||
payload = json.loads(payload_str)
|
|
||||||
if current_event == "message":
|
|
||||||
chunk = payload.get("answer") or ""
|
|
||||||
if chunk:
|
|
||||||
answer_parts.append(chunk)
|
|
||||||
elif current_event in ("message_end", "end", "completed"):
|
|
||||||
meta = payload.get("metadata", {})
|
|
||||||
usage = meta.get("usage", {})
|
|
||||||
if usage:
|
|
||||||
pt = usage.get("prompt_tokens", 0)
|
|
||||||
ct = usage.get("completion_tokens", 0)
|
|
||||||
tt = usage.get("total_tokens", 0)
|
|
||||||
tokens_info = f"\n\n【tokens】输入: {pt} 生成: {ct} 总: {tt}"
|
|
||||||
except Exception as e:
|
|
||||||
self.LOG.debug(f"SSE数据解析失败: {e}")
|
|
||||||
current_event = None
|
|
||||||
data_buffer = []
|
|
||||||
continue
|
|
||||||
if line.startswith(":"):
|
|
||||||
continue
|
|
||||||
if line.startswith("event:"):
|
|
||||||
current_event = line.split(":", 1)[1].strip()
|
|
||||||
continue
|
|
||||||
if line.startswith("data:"):
|
|
||||||
data_buffer.append(line[5:].strip())
|
|
||||||
continue
|
|
||||||
answer = "".join(answer_parts)
|
|
||||||
if tokens_info:
|
|
||||||
answer += tokens_info
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
response_data = await response.json()
|
|
||||||
self.LOG.debug(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
|
|
||||||
answer = response_data.get("answer", "") or ""
|
|
||||||
metadata = response_data.get("metadata", {}) or {}
|
|
||||||
usage = metadata.get("usage", {}) or {}
|
|
||||||
if usage:
|
|
||||||
pt = usage.get("prompt_tokens", 0)
|
|
||||||
ct = usage.get("completion_tokens", 0)
|
|
||||||
tt = usage.get("total_tokens", 0)
|
|
||||||
tokens_info = f"\n\n【tokens】输入: {pt} 生成: {ct} 总: {tt}"
|
|
||||||
answer += tokens_info
|
|
||||||
except Exception as e:
|
|
||||||
self.LOG.error(f"解析非SSE响应失败: {e}")
|
|
||||||
answer = ""
|
|
||||||
if answer:
|
if answer:
|
||||||
try:
|
try:
|
||||||
timestamp = int(time.time())
|
timestamp = int(time.time())
|
||||||
|
|||||||
Reference in New Issue
Block a user