接入dify工作流内容
This commit is contained in:
@@ -149,7 +149,7 @@ class DifyChat:
|
|||||||
"sys.files": [],
|
"sys.files": [],
|
||||||
"user": user_id,
|
"user": user_id,
|
||||||
"inputs": inputs_params,
|
"inputs": inputs_params,
|
||||||
"response_mode": "streaming" # 使用流式响应
|
"response_mode": "blocking" # 使用阻塞响应模式
|
||||||
}
|
}
|
||||||
|
|
||||||
# 添加历史记录
|
# 添加历史记录
|
||||||
@@ -171,54 +171,29 @@ class DifyChat:
|
|||||||
self.LOG.info(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
|
self.LOG.info(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 使用流式请求
|
# 使用普通请求(非流式)
|
||||||
with requests.post(url, headers=headers, json=data, proxies=proxies, stream=True) as response:
|
response = requests.post(url, headers=headers, json=data, proxies=proxies)
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
self.LOG.error(f"Dify API请求失败: {response.status_code} {response.text}")
|
self.LOG.error(f"Dify API请求失败: {response.status_code} {response.text}")
|
||||||
return f"请求失败,状态码: {response.status_code}"
|
return f"请求失败,状态码: {response.status_code}"
|
||||||
|
|
||||||
|
# 解析响应
|
||||||
|
response_data = response.json()
|
||||||
|
self.LOG.info(f"收到Dify API响应: {json.dumps(response_data, ensure_ascii=False)}")
|
||||||
|
|
||||||
|
# 提取回答内容
|
||||||
answer = ""
|
answer = ""
|
||||||
total_tokens = 0
|
total_tokens = 0
|
||||||
|
|
||||||
# 处理流式响应
|
# 获取输出内容
|
||||||
for line in response.iter_lines():
|
outputs = response_data.get("data", {}).get("outputs", {})
|
||||||
if not line:
|
if outputs:
|
||||||
continue
|
# 尝试从text字段获取回答
|
||||||
|
if "text" in outputs and isinstance(outputs["text"], str):
|
||||||
# 解析事件流数据
|
answer = outputs["text"]
|
||||||
line_text = line.decode('utf-8')
|
# 如果没有text字段,尝试从其他字段获取
|
||||||
self.LOG.info(f"收到事件流数据: {line_text}")
|
else:
|
||||||
if not line_text.startswith('data: '):
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 提取JSON数据部分
|
|
||||||
json_str = line_text[6:] # 去掉 "data: " 前缀
|
|
||||||
|
|
||||||
try:
|
|
||||||
event_data = json.loads(json_str)
|
|
||||||
if not event_data:
|
|
||||||
continue
|
|
||||||
|
|
||||||
event_type = event_data.get("event")
|
|
||||||
if not event_type:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 处理不同类型的事件
|
|
||||||
if event_type == "workflow_finished":
|
|
||||||
# 工作流完成事件,可以获取总token数
|
|
||||||
data_obj = event_data.get("data", {})
|
|
||||||
if data_obj:
|
|
||||||
total_tokens = data_obj.get("total_tokens", 0)
|
|
||||||
|
|
||||||
elif event_type == "node_finished":
|
|
||||||
# 节点完成事件,可以获取节点输出和token使用情况
|
|
||||||
data_obj = event_data.get("data", {})
|
|
||||||
if not data_obj:
|
|
||||||
continue
|
|
||||||
|
|
||||||
outputs = data_obj.get("outputs", {})
|
|
||||||
# 从outputs中提取回答内容
|
|
||||||
if outputs and isinstance(outputs, dict):
|
|
||||||
for key, value in outputs.items():
|
for key, value in outputs.items():
|
||||||
if isinstance(value, str) and value.strip():
|
if isinstance(value, str) and value.strip():
|
||||||
answer += value
|
answer += value
|
||||||
@@ -239,13 +214,7 @@ class DifyChat:
|
|||||||
answer += item_value
|
answer += item_value
|
||||||
|
|
||||||
# 获取token使用情况
|
# 获取token使用情况
|
||||||
execution_metadata = data_obj.get("execution_metadata", {})
|
total_tokens = response_data.get("data", {}).get("total_tokens", 0)
|
||||||
if "total_tokens" in execution_metadata:
|
|
||||||
total_tokens = execution_metadata.get("total_tokens", 0)
|
|
||||||
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
self.LOG.error(f"解析事件流数据失败: {line_text}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
# 更新会话历史
|
# 更新会话历史
|
||||||
self.conversations[session_id].append({
|
self.conversations[session_id].append({
|
||||||
@@ -275,7 +244,7 @@ class DifyChat:
|
|||||||
return answer
|
return answer
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"处理Dify流式响应时出错: {str(e)}")
|
self.LOG.error(f"处理Dify响应时出错: {str(e)}")
|
||||||
return f"处理响应时出错: {str(e)}"
|
return f"处理响应时出错: {str(e)}"
|
||||||
|
|
||||||
def _cleanup_expired_conversations(self) -> None:
|
def _cleanup_expired_conversations(self) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user