pycoze 0.1.341__py3-none-any.whl → 0.1.342__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycoze/bot/chat.py +130 -100
- {pycoze-0.1.341.dist-info → pycoze-0.1.342.dist-info}/METADATA +1 -1
- {pycoze-0.1.341.dist-info → pycoze-0.1.342.dist-info}/RECORD +6 -6
- {pycoze-0.1.341.dist-info → pycoze-0.1.342.dist-info}/LICENSE +0 -0
- {pycoze-0.1.341.dist-info → pycoze-0.1.342.dist-info}/WHEEL +0 -0
- {pycoze-0.1.341.dist-info → pycoze-0.1.342.dist-info}/top_level.txt +0 -0
pycoze/bot/chat.py
CHANGED
@@ -1,100 +1,130 @@
|
|
1
|
-
import json
|
2
|
-
from .chat_base import handle_user_inputs
|
3
|
-
from .lib import get_abilities, get_system_prompt
|
4
|
-
from .message import INPUT_MESSAGE, output, CHAT_DATA, clear_chat_data
|
5
|
-
import os
|
6
|
-
import asyncio
|
7
|
-
from pycoze import utils
|
8
|
-
import tempfile
|
9
|
-
|
10
|
-
|
11
|
-
async def check_interrupt_file(interval, interrupt_file, chat_task):
|
12
|
-
while True:
|
13
|
-
await asyncio.sleep(interval)
|
14
|
-
if os.path.exists(interrupt_file):
|
15
|
-
os.remove(interrupt_file)
|
16
|
-
chat_task.cancel()
|
17
|
-
break
|
18
|
-
|
19
|
-
|
20
|
-
async def run_with_interrupt_check(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
1
|
+
import json
|
2
|
+
from .chat_base import handle_user_inputs
|
3
|
+
from .lib import get_abilities, get_system_prompt
|
4
|
+
from .message import INPUT_MESSAGE, output, CHAT_DATA, clear_chat_data
|
5
|
+
import os
|
6
|
+
import asyncio
|
7
|
+
from pycoze import utils
|
8
|
+
import tempfile
|
9
|
+
|
10
|
+
|
11
|
+
async def check_interrupt_file(interval, interrupt_file, chat_task):
|
12
|
+
while True:
|
13
|
+
await asyncio.sleep(interval)
|
14
|
+
if os.path.exists(interrupt_file):
|
15
|
+
os.remove(interrupt_file)
|
16
|
+
chat_task.cancel()
|
17
|
+
break
|
18
|
+
|
19
|
+
|
20
|
+
async def run_with_interrupt_check(
|
21
|
+
conversation_history,
|
22
|
+
user_input,
|
23
|
+
cwd: str,
|
24
|
+
abilities,
|
25
|
+
has_any_tool,
|
26
|
+
bot_setting,
|
27
|
+
interrupt_file,
|
28
|
+
):
|
29
|
+
clear_chat_data()
|
30
|
+
try:
|
31
|
+
chat_task = asyncio.create_task(
|
32
|
+
handle_user_inputs(
|
33
|
+
conversation_history,
|
34
|
+
user_input,
|
35
|
+
cwd,
|
36
|
+
abilities,
|
37
|
+
has_any_tool,
|
38
|
+
bot_setting,
|
39
|
+
)
|
40
|
+
)
|
41
|
+
check_task = asyncio.create_task(
|
42
|
+
check_interrupt_file(0.5, interrupt_file, chat_task)
|
43
|
+
)
|
44
|
+
result = await chat_task
|
45
|
+
return result
|
46
|
+
except asyncio.CancelledError:
|
47
|
+
return CHAT_DATA["info"]
|
48
|
+
except Exception as e:
|
49
|
+
import traceback
|
50
|
+
|
51
|
+
print(traceback.format_exc())
|
52
|
+
return None # 返回 None 或者处理异常后的结果
|
53
|
+
finally:
|
54
|
+
if not chat_task.done():
|
55
|
+
chat_task.cancel()
|
56
|
+
# 确保即使发生异常也会取消检查任务
|
57
|
+
if not check_task.done():
|
58
|
+
check_task.cancel()
|
59
|
+
try:
|
60
|
+
await check_task
|
61
|
+
except asyncio.CancelledError:
|
62
|
+
pass # 忽略取消错误
|
63
|
+
|
64
|
+
|
65
|
+
def chat(bot_setting_file: str):
|
66
|
+
with open(bot_setting_file, encoding="utf-8") as f:
|
67
|
+
bot_setting = json.load(f)
|
68
|
+
abilities = get_abilities(bot_setting)
|
69
|
+
cwd = tempfile.mkdtemp()
|
70
|
+
system_prompt, has_any_tool = get_system_prompt(abilities, bot_setting)
|
71
|
+
conversation_history = [
|
72
|
+
{
|
73
|
+
"role": "system",
|
74
|
+
"content": system_prompt,
|
75
|
+
}
|
76
|
+
]
|
77
|
+
while True:
|
78
|
+
clear_chat_data()
|
79
|
+
input_text = input()
|
80
|
+
if not input_text.startswith(INPUT_MESSAGE):
|
81
|
+
raise ValueError("Invalid message")
|
82
|
+
message = json.loads(input_text[len(INPUT_MESSAGE) :])
|
83
|
+
user_input = message["content"]
|
84
|
+
params = utils.params
|
85
|
+
if "interruptFile" in params:
|
86
|
+
asyncio.run(
|
87
|
+
run_with_interrupt_check(
|
88
|
+
conversation_history,
|
89
|
+
user_input,
|
90
|
+
cwd,
|
91
|
+
abilities,
|
92
|
+
has_any_tool,
|
93
|
+
bot_setting,
|
94
|
+
params["interruptFile"],
|
95
|
+
)
|
96
|
+
)
|
97
|
+
else:
|
98
|
+
asyncio.run(
|
99
|
+
handle_user_inputs(
|
100
|
+
conversation_history,
|
101
|
+
user_input,
|
102
|
+
cwd,
|
103
|
+
abilities,
|
104
|
+
has_any_tool,
|
105
|
+
bot_setting,
|
106
|
+
)
|
107
|
+
)
|
108
|
+
print("has_output")
|
109
|
+
output("assistant", CHAT_DATA["info"])
|
110
|
+
|
111
|
+
|
112
|
+
def get_chat_response(bot_setting_file: str, user_input: str):
|
113
|
+
with open(bot_setting_file, encoding="utf-8") as f:
|
114
|
+
bot_setting = json.load(f)
|
115
|
+
abilities = get_abilities(bot_setting)
|
116
|
+
cwd = tempfile.mkdtemp()
|
117
|
+
system_prompt, has_any_tool = get_system_prompt(abilities, bot_setting)
|
118
|
+
conversation_history = [
|
119
|
+
{
|
120
|
+
"role": "system",
|
121
|
+
"content": system_prompt,
|
122
|
+
}
|
123
|
+
]
|
124
|
+
asyncio.run(
|
125
|
+
handle_user_inputs(
|
126
|
+
conversation_history, user_input, cwd, abilities, has_any_tool, bot_setting
|
127
|
+
)
|
128
|
+
)
|
129
|
+
|
130
|
+
return CHAT_DATA["info"]
|
@@ -12,7 +12,7 @@ pycoze/api/lib/view.py,sha256=_PIpTfeuTPPlMDKshMGsqFQYMq7ZiO4Hg5XwHwDoU60,7357
|
|
12
12
|
pycoze/api/lib/web.py,sha256=GWgtiTJOolKOX2drXcwuyqTcbo5FQVxa1NuBGcNyjyc,223
|
13
13
|
pycoze/api/lib/window.py,sha256=bTkQCzQZ7i3pYXB70bUSTBNJ9C4TW_X3yMae1VkquGk,1944
|
14
14
|
pycoze/bot/__init__.py,sha256=rL3Q-ycczRpSFfKn84fg3QBl5k22WpyeIU5qOEjEby8,79
|
15
|
-
pycoze/bot/chat.py,sha256=
|
15
|
+
pycoze/bot/chat.py,sha256=DB0fUb4B0EVRWWEavCzIa-cSXOpiZrxIusJnQYGPKG8,3765
|
16
16
|
pycoze/bot/chat_base.py,sha256=d7rsp_E1woC8UFBsqPExicNfwMHAB0e50vsodL8ISGA,9722
|
17
17
|
pycoze/bot/lib.py,sha256=smigeWuhl8esHE-Y5l_9bpjJkEJ5OqrxTyPcO8JIubM,7224
|
18
18
|
pycoze/bot/message.py,sha256=Zq-_k8HztBMOUIs3hbOvWvwHBNopn4UJJBliCROIGcc,718
|
@@ -33,8 +33,8 @@ pycoze/utils/arg.py,sha256=jop1tBfe5hYkHW1NSpCeaZBEznkgguBscj_7M2dWfrs,503
|
|
33
33
|
pycoze/utils/env.py,sha256=5pWlXfM1F5ZU9hhv1rHlDEanjEW5wf0nbyez9bNRqqA,559
|
34
34
|
pycoze/utils/socket.py,sha256=bZbFFRH4mfThzRqt55BAAGQ6eICx_ja4x8UGGrUdAm8,2428
|
35
35
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
36
|
-
pycoze-0.1.
|
37
|
-
pycoze-0.1.
|
38
|
-
pycoze-0.1.
|
39
|
-
pycoze-0.1.
|
40
|
-
pycoze-0.1.
|
36
|
+
pycoze-0.1.342.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
37
|
+
pycoze-0.1.342.dist-info/METADATA,sha256=n0QSg_AqHoz9DnUvyQOYb6qErvMiKX1lcq4mAWjLfwo,755
|
38
|
+
pycoze-0.1.342.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
|
39
|
+
pycoze-0.1.342.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
40
|
+
pycoze-0.1.342.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|