调整总结逻辑,支持流式回复,并且加入深度思考兼容
This commit is contained in:
@@ -229,7 +229,8 @@ 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:
|
||||||
@@ -238,11 +239,16 @@ 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 = ""
|
||||||
|
spath = None
|
||||||
|
tokens_info = ""
|
||||||
|
if "text/event-stream" in ctype:
|
||||||
answer_parts = []
|
answer_parts = []
|
||||||
current_event = None
|
current_event = None
|
||||||
data_buffer = []
|
data_buffer = []
|
||||||
async for raw_line in response.content:
|
async for raw_line in response.content:
|
||||||
line = raw_line.decode("utf-8").rstrip("\n")
|
line = raw_line.decode("utf-8", errors="ignore").rstrip("\r\n")
|
||||||
if not line:
|
if not line:
|
||||||
if data_buffer:
|
if data_buffer:
|
||||||
payload_str = "\n".join(data_buffer)
|
payload_str = "\n".join(data_buffer)
|
||||||
@@ -252,6 +258,14 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
chunk = payload.get("answer") or ""
|
chunk = payload.get("answer") or ""
|
||||||
if chunk:
|
if chunk:
|
||||||
answer_parts.append(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:
|
except Exception as e:
|
||||||
self.LOG.debug(f"SSE数据解析失败: {e}")
|
self.LOG.debug(f"SSE数据解析失败: {e}")
|
||||||
current_event = None
|
current_event = None
|
||||||
@@ -266,7 +280,24 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
data_buffer.append(line[5:].strip())
|
data_buffer.append(line[5:].strip())
|
||||||
continue
|
continue
|
||||||
answer = "".join(answer_parts)
|
answer = "".join(answer_parts)
|
||||||
spath = None
|
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