From 1e8ba982ba99f1d584b79fb81578d33f691a16c4 Mon Sep 17 00:00:00 2001 From: ikaros <327209194@qq.com> Date: Sun, 16 Jul 2023 11:45:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=B9Claude=E5=AE=9E=E7=8E=B0=E4=B8=BA?= =?UTF-8?q?=E9=9D=9E=E5=BC=82=E6=AD=A5=EF=BC=8C=E9=83=A8=E5=88=86=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=9C=89asyncio=E5=BA=93=E6=8A=A5=E9=94=99=EF=BC=8C?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/gpt_model/claude.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/utils/gpt_model/claude.py b/utils/gpt_model/claude.py index 3a11365c..60a04db8 100644 --- a/utils/gpt_model/claude.py +++ b/utils/gpt_model/claude.py @@ -47,7 +47,7 @@ def fetch_messages(self, channel, last_message_timestamp): return [msg['text'] for msg in response['messages'] if msg['user'] == self.bot_user_id] - async def get_new_messages(self, channel, last_message_timestamp): + def get_new_messages(self, channel, last_message_timestamp): timeout = 60 # 超时时间设置为60秒 start_time = time.time() @@ -58,7 +58,7 @@ async def get_new_messages(self, channel, last_message_timestamp): if time.time() - start_time > timeout: return None - await asyncio.sleep(5) + #await asyncio.sleep(3) def find_direct_message_channel(self, user_id): @@ -77,7 +77,8 @@ def get_claude_resp(self, text): else: return None - new_message = asyncio.run(self.get_new_messages(self.dm_channel_id, last_message_timestamp)) + new_message = self.get_new_messages(self.dm_channel_id, last_message_timestamp) + #new_message = asyncio.run(self.get_new_messages(self.dm_channel_id, last_message_timestamp)) if new_message is not None: return new_message return None From e7b261ccbe221ff089766760391feee71bd67196 Mon Sep 17 00:00:00 2001 From: ikaros <327209194@qq.com> Date: Sun, 16 Jul 2023 14:23:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?claude=20slack=E6=9C=89=E9=87=8D=E5=A4=A7?= =?UTF-8?q?=E7=BC=BA=E9=99=B7=EF=BC=8C=E6=9A=82=E6=97=A0=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/gpt_model/claude.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/utils/gpt_model/claude.py b/utils/gpt_model/claude.py index 60a04db8..71c1ddb1 100644 --- a/utils/gpt_model/claude.py +++ b/utils/gpt_model/claude.py @@ -1,5 +1,6 @@ import time, logging -import asyncio +import asyncio, threading +import concurrent.futures from slack_sdk import WebClient from slack_sdk.errors import SlackApiError @@ -14,6 +15,7 @@ class Claude: client = None last_message_timestamp = None dm_channel_id = None + def __init__(self, data): self.common = Common() @@ -32,6 +34,8 @@ def __init__(self, data): if not self.dm_channel_id: logging.error("Could not find DM channel with the bot.") return None + + loop = asyncio.new_event_loop() ### claude def send_message(self, channel, text): @@ -57,9 +61,7 @@ def get_new_messages(self, channel, last_message_timestamp): return messages[-1] if time.time() - start_time > timeout: return None - - #await asyncio.sleep(3) - + def find_direct_message_channel(self, user_id): try: @@ -77,6 +79,10 @@ def get_claude_resp(self, text): else: return None + # t = threading.Thread(target=lambda: asyncio.run(self.get_new_messages(self.dm_channel_id, last_message_timestamp))) + # t.start() + # new_message = t.join() + new_message = self.get_new_messages(self.dm_channel_id, last_message_timestamp) #new_message = asyncio.run(self.get_new_messages(self.dm_channel_id, last_message_timestamp)) if new_message is not None: