临时优化一下。
This commit is contained in:
+8
-7
@@ -144,7 +144,8 @@ class DifyChat:
|
|||||||
"query": query,
|
"query": query,
|
||||||
"conversation_id": session_id
|
"conversation_id": session_id
|
||||||
}
|
}
|
||||||
# 准备请求数据
|
|
||||||
|
# 准备请求数据
|
||||||
data = {
|
data = {
|
||||||
"sys.files": [],
|
"sys.files": [],
|
||||||
"user": user_id,
|
"user": user_id,
|
||||||
@@ -166,14 +167,14 @@ class DifyChat:
|
|||||||
|
|
||||||
# 发送请求
|
# 发送请求
|
||||||
url = f"{self.base_url}/workflows/run"
|
url = f"{self.base_url}/workflows/run"
|
||||||
|
|
||||||
self.LOG.info(f"发送请求到Dify API: {url}")
|
self.LOG.info(f"发送请求到Dify API: {url}")
|
||||||
self.LOG.info(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
|
self.LOG.info(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 使用普通请求(非流式)
|
# 使用普通请求(非流式)
|
||||||
response = requests.post(url, headers=headers, json=data, proxies=proxies)
|
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}"
|
||||||
@@ -181,11 +182,11 @@ class DifyChat:
|
|||||||
# 解析响应
|
# 解析响应
|
||||||
response_data = response.json()
|
response_data = response.json()
|
||||||
self.LOG.info(f"收到Dify API响应: {json.dumps(response_data, ensure_ascii=False)}")
|
self.LOG.info(f"收到Dify API响应: {json.dumps(response_data, ensure_ascii=False)}")
|
||||||
|
|
||||||
# 提取回答内容
|
# 提取回答内容
|
||||||
answer = ""
|
answer = ""
|
||||||
total_tokens = 0
|
total_tokens = 0
|
||||||
|
|
||||||
# 获取输出内容
|
# 获取输出内容
|
||||||
outputs = response_data.get("data", {}).get("outputs", {})
|
outputs = response_data.get("data", {}).get("outputs", {})
|
||||||
if outputs:
|
if outputs:
|
||||||
@@ -212,7 +213,7 @@ class DifyChat:
|
|||||||
for item_key, item_value in item.items():
|
for item_key, item_value in item.items():
|
||||||
if isinstance(item_value, str) and item_value.strip():
|
if isinstance(item_value, str) and item_value.strip():
|
||||||
answer += item_value
|
answer += item_value
|
||||||
|
|
||||||
# 获取token使用情况
|
# 获取token使用情况
|
||||||
total_tokens = response_data.get("data", {}).get("total_tokens", 0)
|
total_tokens = response_data.get("data", {}).get("total_tokens", 0)
|
||||||
|
|
||||||
|
|||||||
@@ -73,10 +73,10 @@ class MessageStorage:
|
|||||||
|
|
||||||
if time_diff < timedelta(hours=3):
|
if time_diff < timedelta(hours=3):
|
||||||
# 小于 3 小时,取 8 小时前
|
# 小于 3 小时,取 8 小时前
|
||||||
last_summary_time = (current_time - timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S')
|
last_summary_time = (current_time - timedelta(hours=3)).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
elif time_diff > timedelta(days=1):
|
elif time_diff > timedelta(days=1):
|
||||||
# 大于 24 小时,取 10 小时前
|
# 大于 24 小时,取 10 小时前
|
||||||
last_summary_time = (current_time - timedelta(hours=10)).strftime('%Y-%m-%d %H:%M:%S')
|
last_summary_time = (current_time - timedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
|
||||||
# 更新 Redis 存储的当前时间
|
# 更新 Redis 存储的当前时间
|
||||||
self._get_redis_connection().set(key, current_date)
|
self._get_redis_connection().set(key, current_date)
|
||||||
|
|||||||
Reference in New Issue
Block a user