pycoze 0.1.211__py3-none-any.whl → 0.1.213__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
pycoze/bot/agent_chat.py CHANGED
@@ -1,12 +1,13 @@
1
1
  import json
2
2
  from langchain_openai import ChatOpenAI
3
- from .agent import run_agent, Runnable, output
3
+ from .agent import run_agent, Runnable, output, CHAT_DATA, clear_chat_data
4
4
  import asyncio
5
5
  from pycoze import utils
6
6
  from pycoze.reference.bot import ref_bot
7
7
  from pycoze.reference.tool import ref_tools
8
8
  from pycoze.reference.workflow import ref_workflow
9
9
  from langchain_core.utils.function_calling import convert_to_openai_tool
10
+ import os
10
11
 
11
12
  cfg = utils.read_json_file("llm.json")
12
13
 
@@ -34,6 +35,36 @@ def load_abilities(bot_setting_file: str):
34
35
  return abilities
35
36
 
36
37
 
38
+ async def check_interrupt_file(interval, interrupt_file,agent_task):
39
+ while True:
40
+ await asyncio.sleep(interval)
41
+ if os.path.exists(interrupt_file):
42
+ os.remove(interrupt_file)
43
+ agent_task.cancel()
44
+ break
45
+
46
+ async def run_with_interrupt_check(agent, history, tool_compatibility_mode, interrupt_file, check_interval=1):
47
+ clear_chat_data()
48
+ try:
49
+ agent_task = asyncio.create_task(run_agent(agent, history, tool_compatibility_mode))
50
+ check_task = asyncio.create_task(check_interrupt_file(check_interval, interrupt_file, agent_task))
51
+ result = await agent_task
52
+ return result
53
+ except asyncio.CancelledError:
54
+ return CHAT_DATA['info']
55
+ except Exception as e:
56
+ import traceback
57
+ print(traceback.format_exc())
58
+ return None # 返回 None 或者处理异常后的结果
59
+ finally:
60
+ # 确保即使发生异常也会取消检查任务
61
+ if not check_task.done():
62
+ check_task.cancel()
63
+ try:
64
+ await check_task
65
+ except asyncio.CancelledError:
66
+ pass # 忽略取消错误
67
+
37
68
  async def agent_chat(bot_setting_file, history):
38
69
  role_setting = load_role_setting(bot_setting_file)
39
70
  abilities = load_abilities(bot_setting_file)
@@ -77,9 +108,11 @@ async def agent_chat(bot_setting_file, history):
77
108
  assistant_message=prompt,
78
109
  tool_compatibility_mode=cfg["toolCompatibilityMode"],
79
110
  )
80
- try:
111
+ params = utils.read_params_file()
112
+ if "interruptFile" in params:
113
+ interrupt_file_path = params["interruptFile"]
114
+ result = await run_with_interrupt_check(agent, history, cfg["toolCompatibilityMode"], interrupt_file_path)
115
+ else:
81
116
  result = await run_agent(agent, history, cfg["toolCompatibilityMode"])
82
- return result
83
- except KeyboardInterrupt:
84
- return CHAT_DATA["info"]
85
-
117
+ return result
118
+
pycoze/bot/bot.py CHANGED
@@ -1,18 +1,10 @@
1
1
  from langchain_core.messages import HumanMessage, AIMessage
2
2
  import threading
3
- import queue
4
3
  import json
5
4
  from .agent import INPUT_MESSAGE, output, CHAT_DATA, clear_chat_data
6
5
  from .agent_chat import agent_chat
7
6
  import asyncio
8
7
 
9
- class PeekableQueue(queue.Queue):
10
- def peek(self):
11
- try:
12
- return self._get()
13
- except queue.Empty:
14
- return None
15
-
16
8
 
17
9
  def chat(bot_setting_file: str):
18
10
  history = []
@@ -23,13 +15,10 @@ def chat(bot_setting_file: str):
23
15
  message = json.loads(input_text[len(INPUT_MESSAGE) :])
24
16
  history.append(HumanMessage(message["content"]))
25
17
  result = asyncio.run(agent_chat(bot_setting_file, history))
18
+ output("assistant", result)
26
19
  history.append(AIMessage(result))
27
20
 
28
21
 
29
-
30
-
31
22
  def get_chat_response(bot_setting_file: str, input_text: str):
32
- history = [HumanMessage(input_text)]
33
- clear_chat_data()
34
- asyncio.run(agent_chat(bot_setting_file, history))
35
- return CHAT_DATA["output"]
23
+ result = asyncio.run(agent_chat(bot_setting_file, [HumanMessage(input_text)]))
24
+ return result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.211
3
+ Version: 0.1.213
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -5,8 +5,8 @@ pycoze/automation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU
5
5
  pycoze/automation/browser/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  pycoze/automation/browser/edge_driver_manager.py,sha256=gpgseunph5owZH6EskSYthuhey2SU3UP204gY0yIcuI,3022
7
7
  pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
8
- pycoze/bot/agent_chat.py,sha256=4J-y7lSM2ULQQYIUB0L4i94m0KyeB4Gdf2U3zhVMOG4,3033
9
- pycoze/bot/bot.py,sha256=A1EryCW6WCnb6jIKc0V58Qzv6qQebKTfQxhcLp8Ngtc,1051
8
+ pycoze/bot/agent_chat.py,sha256=uukxrfzyOyGTymhTA3u0o4JdDUoKPDUfPN0LE5VWdKY,4432
9
+ pycoze/bot/bot.py,sha256=fmcgnWcYTFeOxDuAwqWhFhOJzv4mAyJGLqbod-nkhJE,862
10
10
  pycoze/bot/agent/__init__.py,sha256=3wE8_FFQS8j2BY-g9Cr-onV0POEvDRZaw_NCzpqrNus,265
11
11
  pycoze/bot/agent/agent.py,sha256=chUgNZh6v6375L_Y2dBEAaLJyfmw4SygYjVVrDN8VIk,3548
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.211.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
- pycoze-0.1.211.dist-info/METADATA,sha256=MPKRrqQHGS9v4n7MnO6JZ9eAvl1cf9rK2BVdmdzRp2g,726
33
- pycoze-0.1.211.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
- pycoze-0.1.211.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
- pycoze-0.1.211.dist-info/RECORD,,
31
+ pycoze-0.1.213.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
32
+ pycoze-0.1.213.dist-info/METADATA,sha256=ri60xG0hbqLctzLvRq_r8PSdP87pcOcuWds0DVeIcQ0,726
33
+ pycoze-0.1.213.dist-info/WHEEL,sha256=bFJAMchF8aTQGUgMZzHJyDDMPTO3ToJ7x23SLJa1SVo,92
34
+ pycoze-0.1.213.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
35
+ pycoze-0.1.213.dist-info/RECORD,,