pycoze 0.1.201__py3-none-any.whl → 0.1.203__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
pycoze/bot/bot.py CHANGED
@@ -6,62 +6,32 @@ import threading
6
6
  import queue
7
7
  import time
8
8
  import asyncio
9
- import multiprocessing
10
- from multiprocessing import Queue, Event
11
9
 
12
- # 假设 PeekableQueue 类已经定义好了,如果没有,请自行实现或使用其他方式
13
- class PeekableQueue:
14
- def __init__(self, maxsize=0):
15
- self.queue = Queue(maxsize=maxsize)
16
-
17
- def put(self, item):
18
- self.queue.put(item)
19
-
20
- def get(self):
21
- return self.queue.get()
22
-
23
- def peek(self):
24
- with self.queue.mutex:
25
- if self.queue.empty():
26
- return None
27
- return self.queue.queue[0]
28
-
29
- def empty(self):
30
- return self.queue.empty()
31
-
32
- def input_process(input_queue: Queue, interrupt_event: Event):
33
- while not interrupt_event.is_set():
34
- input_text = input()
35
- if input_text == INTERRUPT_MESSAGE or input_text.startswith(INPUT_MESSAGE):
36
- input_queue.put(input_text)
37
- else:
38
- raise ValueError("Invalid message")
39
10
 
40
- async def handle_input_queue(input_queue: Queue, should_exit: asyncio.Event, history):
41
- while not should_exit.is_set():
11
+ class PeekableQueue(queue.Queue):
12
+ def peek(self):
42
13
  try:
43
- next_input_text = input_queue.get_nowait()
44
- if next_input_text == INTERRUPT_MESSAGE:
45
- history.append(AIMessage(content=CHAT_DATA["info"]))
46
- should_exit.set() # 设置退出标志
47
- break
14
+ return self._get()
48
15
  except queue.Empty:
49
- pass
50
- await asyncio.sleep(0.1) # 非阻塞地等待一段时间
16
+ return None
51
17
 
52
18
  def chat(bot_setting_file: str):
53
19
  history = []
54
20
  input_queue = PeekableQueue()
55
- interrupt_event = Event() # 用于中断输入进程的事件
56
21
 
57
- # 创建并启动输入进程
58
- input_process_instance = multiprocessing.Process(target=input_process, args=(input_queue, interrupt_event))
59
- input_process_instance.start()
22
+ def input_thread(input_queue: PeekableQueue):
23
+ while True:
24
+ input_text = input()
25
+ if input_text == INTERRUPT_MESSAGE or input_text.startswith(INPUT_MESSAGE):
26
+ input_queue.put(input_text)
27
+ else:
28
+ raise ValueError("Invalid message")
29
+
60
30
 
61
- try:
62
- loop = asyncio.new_event_loop()
63
- asyncio.set_event_loop(loop)
31
+ input_thread_instance = threading.Thread(target=input_thread, args=(input_queue,))
32
+ input_thread_instance.start()
64
33
 
34
+ try:
65
35
  while True:
66
36
  if not input_queue.empty():
67
37
  input_text = input_queue.get()
@@ -71,21 +41,23 @@ def chat(bot_setting_file: str):
71
41
  message = json.loads(input_text[len(INPUT_MESSAGE):])
72
42
  history.append(HumanMessage(message["content"]))
73
43
  clear_chat_data()
74
-
75
- # 创建一个 Event 对象用于中断
76
- should_exit = asyncio.Event()
77
-
78
- # 并发运行 agent_chat 和 handle_input_queue
79
- agent_chat_task = loop.create_task(agent_chat(bot_setting_file, history, should_exit))
80
- input_handler_task = loop.create_task(handle_input_queue(input_queue, should_exit, history))
81
-
82
- # 运行所有任务
83
- loop.run_until_complete(asyncio.gather(agent_chat_task, input_handler_task))
84
-
85
- # 如果没有中断,则添加 AI 的响应到历史记录
86
- if not should_exit.is_set():
44
+
45
+ # 为每个线程创建一个独立的退出标志
46
+ should_exit = threading.Event()
47
+ agent_chat_thread = threading.Thread(target=asyncio.run, args=(agent_chat(bot_setting_file, history, should_exit),))
48
+ agent_chat_thread.start()
49
+
50
+ next_input_text = ""
51
+ while agent_chat_thread.is_alive():
52
+ if not input_queue.empty():
53
+ next_input_text = input_queue.peek()
54
+ if next_input_text == INTERRUPT_MESSAGE:
55
+ history.append(AIMessage(content=CHAT_DATA["info"]))
56
+ should_exit.set() # 设置退出标志
57
+ break
58
+ time.sleep(0.1) # 每隔 0.1 秒检查一次
59
+ if next_input_text != INTERRUPT_MESSAGE:
87
60
  history.append(AIMessage(content=CHAT_DATA["output"]))
88
-
89
61
  except json.JSONDecodeError:
90
62
  print("Invalid JSON format in input message.")
91
63
  except KeyError:
@@ -93,9 +65,7 @@ def chat(bot_setting_file: str):
93
65
  except Exception as e:
94
66
  print(f"An error occurred: {e}")
95
67
  finally:
96
- # 设置中断事件以停止输入进程
97
- interrupt_event.set()
98
- input_process_instance.join()
68
+ input_thread_instance.join()
99
69
 
100
70
 
101
71
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.201
3
+ Version: 0.1.203
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -6,7 +6,7 @@ pycoze/automation/browser/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJW
6
6
  pycoze/automation/browser/edge_driver_manager.py,sha256=gpgseunph5owZH6EskSYthuhey2SU3UP204gY0yIcuI,3022
7
7
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
8
8
  pycoze/bot/agent_chat.py,sha256=hr0jGTd-lvLSe4vCcvaN2rKhB7BMBcVBtI1FpVa6ngs,3016
9
- pycoze/bot/bot.py,sha256=9WtvKt3_kgXnBXZEXINoIm_Vz1Oj2C_onWxnS7rvY2k,4041
9
+ pycoze/bot/bot.py,sha256=CCnBmxtmpmz7aLR_MIIiOuKR9IACo5P4uSkQXb5d0KU,3061
10
10
  pycoze/bot/agent/__init__.py,sha256=3wE8_FFQS8j2BY-g9Cr-onV0POEvDRZaw_NCzpqrNus,265
11
11
  pycoze/bot/agent/agent.py,sha256=Ueste4xWvkQFk5Giklv4c-kcxiPL-D-KafzZ4DONosY,3613
12
12
  pycoze/bot/agent/assistant.py,sha256=3iLxnRvf_ia0cP-FHK5Fv4ylltlnzPq1KscRCFYqjkc,1147
@@ -28,8 +28,8 @@ pycoze/utils/__init__.py,sha256=Gi5EnrWZGMD2JRejgV4c_VLCXyvA2wwBFI_niDF5MUE,110
28
28
  pycoze/utils/arg.py,sha256=GtfGbMTMdaK75Fwh6MpUe1pCA5X6Ep4LFG7a72YrzjI,525
29
29
  pycoze/utils/env.py,sha256=W04lhvTHhAAC6EldP6kk2xrctqtu8K6kl1vDLZDNeh8,561
30
30
  pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
31
- pycoze-0.1.201.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
- pycoze-0.1.201.dist-info/METADATA,sha256=NhdIlRwQ0ND-FWYkLsIv1xrVms2SVbF4w5hmBb15N9s,726
33
- pycoze-0.1.201.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
- pycoze-0.1.201.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
- pycoze-0.1.201.dist-info/RECORD,,
31
+ pycoze-0.1.203.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
+ pycoze-0.1.203.dist-info/METADATA,sha256=0avX2C5q7v5RHgIqf0ZMJOEj3cA5Fl37d67c6vzmvFc,726
33
+ pycoze-0.1.203.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
+ pycoze-0.1.203.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
+ pycoze-0.1.203.dist-info/RECORD,,