优化IO问题
This commit is contained in:
@@ -19,6 +19,7 @@ from utils.decorator.points_decorator import plugin_points_cost
|
||||
from utils.media_downloader import MediaDownloader
|
||||
from utils.string_utils import remove_trailing_content
|
||||
from wechat_ipad import WechatAPIClient
|
||||
import aiohttp
|
||||
|
||||
# 常见的图片和视频文件扩展名
|
||||
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
|
||||
@@ -146,7 +147,7 @@ class DifyPlugin(MessagePluginInterface):
|
||||
|
||||
try:
|
||||
# 调用Dify API获取回复
|
||||
success, response = self._chat_with_dify((roomid if roomid else sender), sender, query)
|
||||
success, response = await self._chat_with_dify((roomid if roomid else sender), sender, query)
|
||||
if not success:
|
||||
return False, response
|
||||
# 去除广告内容
|
||||
@@ -162,7 +163,8 @@ class DifyPlugin(MessagePluginInterface):
|
||||
await bot.send_image_message((roomid if roomid else sender), Path(response))
|
||||
elif file_type == 2:
|
||||
first_farme = self._get_first_frame(response, f"dify_frame_{int(time.time())}.jpg")
|
||||
await bot.send_video_message((roomid if roomid else sender), Path(response), Path(first_farme))
|
||||
await bot.send_video_message((roomid if roomid else sender), Path(response),
|
||||
Path(first_farme))
|
||||
else:
|
||||
return False, "获取媒资失败"
|
||||
else:
|
||||
@@ -219,7 +221,7 @@ class DifyPlugin(MessagePluginInterface):
|
||||
|
||||
try:
|
||||
# 调用Dify API获取回复
|
||||
success, response = self._chat_with_dify(session_id, user_id, query)
|
||||
success, response = await self._chat_with_dify(session_id, user_id, query)
|
||||
if not success:
|
||||
return False, response
|
||||
# 去除广告内容
|
||||
@@ -269,7 +271,7 @@ class DifyPlugin(MessagePluginInterface):
|
||||
revoke.add_message_to_revoke((roomid if roomid else sender), client_msg_id, create_time, new_msg_id, 5)
|
||||
return False, f"处理出错: {e}"
|
||||
|
||||
def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Tuple[bool, Optional[str]]:
|
||||
async def _chat_with_dify(self, session_id: str, user_id: str, query: str) -> Tuple[bool, Optional[str]]:
|
||||
"""
|
||||
与Dify API交互获取回复
|
||||
|
||||
@@ -330,12 +332,7 @@ class DifyPlugin(MessagePluginInterface):
|
||||
data["conversation_history"] = self.conversations[session_id]
|
||||
|
||||
# 设置代理
|
||||
proxies = None
|
||||
if self.http_proxy:
|
||||
proxies = {
|
||||
"http": self.http_proxy,
|
||||
"https": self.http_proxy
|
||||
}
|
||||
proxy = self.http_proxy if self.http_proxy else None
|
||||
|
||||
# 发送请求
|
||||
url = f"{self.base_url}/workflows/run"
|
||||
@@ -344,89 +341,89 @@ class DifyPlugin(MessagePluginInterface):
|
||||
self.LOG.info(f"请求数据: {json.dumps(data, ensure_ascii=False)}")
|
||||
|
||||
try:
|
||||
# 使用普通请求(非流式)
|
||||
response = requests.post(url, headers=headers, json=data, proxies=proxies, timeout=40)
|
||||
async with aiohttp.ClientSession() as session:
|
||||
response = await session.post(url, headers=headers, json=data, proxy=proxy, timeout=40)
|
||||
if response.status != 200:
|
||||
error_text = await response.text()
|
||||
self.LOG.error(f"Dify API请求失败: {response.status} {error_text}")
|
||||
return False, f"请求失败,状态码: {response.status}"
|
||||
|
||||
if response.status_code != 200:
|
||||
self.LOG.error(f"Dify API请求失败: {response.status_code} {response.text}")
|
||||
return False, f"请求失败,状态码: {response.status_code}"
|
||||
# 解析响应
|
||||
response_data = await response.json()
|
||||
self.LOG.info(f"收到Dify API响应: {json.dumps(response_data, ensure_ascii=False)}")
|
||||
|
||||
# 解析响应
|
||||
response_data = response.json()
|
||||
self.LOG.info(f"收到Dify API响应: {json.dumps(response_data, ensure_ascii=False)}")
|
||||
# 提取回答内容
|
||||
answer = ""
|
||||
total_tokens = 0
|
||||
|
||||
# 提取回答内容
|
||||
answer = ""
|
||||
total_tokens = 0
|
||||
# 获取输出内容
|
||||
outputs = response_data.get("data", {}).get("outputs", {})
|
||||
if outputs:
|
||||
# 处理媒体类型返回
|
||||
if "result" in outputs and "type" in outputs:
|
||||
if outputs["type"] == "image":
|
||||
downloader = MediaDownloader()
|
||||
image_url = outputs["result"]
|
||||
image_path = downloader.download_media(image_url)
|
||||
answer = image_path
|
||||
if outputs["type"] == "video":
|
||||
downloader = MediaDownloader()
|
||||
image_url = outputs["result"]
|
||||
image_path = downloader.download_media(image_url)
|
||||
answer = image_path
|
||||
# 处理文本类型返回
|
||||
elif "text" in outputs and isinstance(outputs["text"], str):
|
||||
answer = outputs["text"]
|
||||
# 兼容旧版处理逻辑
|
||||
else:
|
||||
for key, value in outputs.items():
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer += value
|
||||
elif isinstance(value, dict):
|
||||
# 处理嵌套字典的情况
|
||||
for sub_key, sub_value in value.items():
|
||||
if isinstance(sub_value, str) and sub_value.strip():
|
||||
answer += sub_value
|
||||
elif isinstance(value, list):
|
||||
# 处理列表的情况
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.strip():
|
||||
answer += item
|
||||
elif isinstance(item, dict):
|
||||
# 处理列表中的字典
|
||||
for item_key, item_value in item.items():
|
||||
if isinstance(item_value, str) and item_value.strip():
|
||||
answer += item_value
|
||||
|
||||
# 获取输出内容
|
||||
outputs = response_data.get("data", {}).get("outputs", {})
|
||||
if outputs:
|
||||
# 处理媒体类型返回
|
||||
if "result" in outputs and "type" in outputs:
|
||||
if outputs["type"] == "image":
|
||||
downloader = MediaDownloader()
|
||||
image_url = outputs["result"]
|
||||
image_path = downloader.download_media(image_url)
|
||||
answer = image_path
|
||||
if outputs["type"] == "video":
|
||||
downloader = MediaDownloader()
|
||||
image_url = outputs["result"]
|
||||
image_path = downloader.download_media(image_url)
|
||||
answer = image_path
|
||||
# 处理文本类型返回
|
||||
elif "text" in outputs and isinstance(outputs["text"], str):
|
||||
answer = outputs["text"]
|
||||
# 兼容旧版处理逻辑
|
||||
else:
|
||||
for key, value in outputs.items():
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer += value
|
||||
elif isinstance(value, dict):
|
||||
# 处理嵌套字典的情况
|
||||
for sub_key, sub_value in value.items():
|
||||
if isinstance(sub_value, str) and sub_value.strip():
|
||||
answer += sub_value
|
||||
elif isinstance(value, list):
|
||||
# 处理列表的情况
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.strip():
|
||||
answer += item
|
||||
elif isinstance(item, dict):
|
||||
# 处理列表中的字典
|
||||
for item_key, item_value in item.items():
|
||||
if isinstance(item_value, str) and item_value.strip():
|
||||
answer += item_value
|
||||
# 获取token使用情况
|
||||
total_tokens = response_data.get("data", {}).get("total_tokens", 0)
|
||||
|
||||
# 获取token使用情况
|
||||
total_tokens = response_data.get("data", {}).get("total_tokens", 0)
|
||||
# 更新会话历史
|
||||
self.conversations[session_id].append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
|
||||
# 更新会话历史
|
||||
self.conversations[session_id].append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
self.conversations[session_id].append({
|
||||
"role": "assistant",
|
||||
"content": answer
|
||||
})
|
||||
|
||||
self.conversations[session_id].append({
|
||||
"role": "assistant",
|
||||
"content": answer
|
||||
})
|
||||
# 限制会话历史长度
|
||||
if len(self.conversations[session_id]) > self.max_history_length * 2:
|
||||
self.conversations[session_id] = self.conversations[session_id][-self.max_history_length * 2:]
|
||||
|
||||
# 限制会话历史长度
|
||||
if len(self.conversations[session_id]) > self.max_history_length * 2:
|
||||
self.conversations[session_id] = self.conversations[session_id][-self.max_history_length * 2:]
|
||||
# 统计token使用情况
|
||||
if total_tokens > 0:
|
||||
if user_id in self.token_usage:
|
||||
self.token_usage[user_id] += total_tokens
|
||||
else:
|
||||
self.token_usage[user_id] = total_tokens
|
||||
|
||||
# 统计token使用情况
|
||||
if total_tokens > 0:
|
||||
if user_id in self.token_usage:
|
||||
self.token_usage[user_id] += total_tokens
|
||||
else:
|
||||
self.token_usage[user_id] = total_tokens
|
||||
self.LOG.info(
|
||||
f"用户 {user_id} 本次消耗 {total_tokens} tokens,累计 {self.token_usage[user_id]} tokens")
|
||||
|
||||
self.LOG.info(
|
||||
f"用户 {user_id} 本次消耗 {total_tokens} tokens,累计 {self.token_usage[user_id]} tokens")
|
||||
|
||||
return True, answer
|
||||
return True, answer
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"处理Dify响应时出错: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user