pycoze 0.1.74__py3-none-any.whl → 0.1.76__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/ai/__init__.py +1 -0
- pycoze/ai/comfyui.py +33 -0
- pycoze/{gpu/gpu_reserve.py → ai/vram_reserve.py} +53 -45
- pycoze/bot/agent/agent_types/openai_func_call_agent.py +11 -6
- pycoze/bot/bot.py +6 -2
- {pycoze-0.1.74.dist-info → pycoze-0.1.76.dist-info}/METADATA +1 -1
- {pycoze-0.1.74.dist-info → pycoze-0.1.76.dist-info}/RECORD +10 -9
- pycoze/gpu/__init__.py +0 -1
- {pycoze-0.1.74.dist-info → pycoze-0.1.76.dist-info}/LICENSE +0 -0
- {pycoze-0.1.74.dist-info → pycoze-0.1.76.dist-info}/WHEEL +0 -0
- {pycoze-0.1.74.dist-info → pycoze-0.1.76.dist-info}/top_level.txt +0 -0
pycoze/ai/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
from .vram_reserve import reserve_vram_retry, release_vram, reserve_vram
|
pycoze/ai/comfyui.py
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
from .vram_reserve import reserve_vram_retry, release_vram
|
2
|
+
|
3
|
+
|
4
|
+
class ComfyUI:
|
5
|
+
def __init__(self, file_path):
|
6
|
+
self.file_path = file_path
|
7
|
+
|
8
|
+
def load(self):
|
9
|
+
reserve_vram_retry(20) # 20GB VRAM
|
10
|
+
with open(self.file_path, "r") as file:
|
11
|
+
return file.read()
|
12
|
+
|
13
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
14
|
+
clear_memory()
|
15
|
+
return None
|
16
|
+
|
17
|
+
def unload(self):
|
18
|
+
clear_memory()
|
19
|
+
return None
|
20
|
+
|
21
|
+
|
22
|
+
def clear_memory():
|
23
|
+
import requests
|
24
|
+
import json
|
25
|
+
|
26
|
+
release_vram()
|
27
|
+
|
28
|
+
url = "http://127.0.0.1:8188/free"
|
29
|
+
data = {"unload_models": True, "free_memory": False}
|
30
|
+
response = requests.post(
|
31
|
+
url, data=json.dumps(data), headers={"Content-Type": "application/json"}
|
32
|
+
)
|
33
|
+
return response.status_code, response.text
|
@@ -12,14 +12,14 @@ try:
|
|
12
12
|
# 定义数据库连接和初始化
|
13
13
|
params = utils.arg.read_params_file()
|
14
14
|
if params:
|
15
|
-
DATABASE_PATH = params["appPath"] + "/
|
15
|
+
DATABASE_PATH = params["appPath"] + "/vram_usage.db"
|
16
16
|
else:
|
17
17
|
raise Exception("No params")
|
18
18
|
except:
|
19
19
|
DATABASE_DIR = os.path.expanduser("~/pycoze")
|
20
20
|
os.makedirs(DATABASE_DIR, exist_ok=True)
|
21
|
-
DATABASE_PATH = os.path.join(DATABASE_DIR, "
|
22
|
-
TABLE_NAME = "
|
21
|
+
DATABASE_PATH = os.path.join(DATABASE_DIR, "vram_usage.db")
|
22
|
+
TABLE_NAME = "vram_usage"
|
23
23
|
|
24
24
|
|
25
25
|
def initialize_db():
|
@@ -29,7 +29,7 @@ def initialize_db():
|
|
29
29
|
f"""
|
30
30
|
CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
|
31
31
|
id INTEGER PRIMARY KEY,
|
32
|
-
|
32
|
+
uid TEXT NOT NULL,
|
33
33
|
reserved_gb REAL NOT NULL
|
34
34
|
)
|
35
35
|
"""
|
@@ -38,19 +38,23 @@ def initialize_db():
|
|
38
38
|
conn.close()
|
39
39
|
|
40
40
|
|
41
|
-
|
42
|
-
def get_gpu_resources():
|
41
|
+
def get_vram_resources():
|
43
42
|
try:
|
44
|
-
# 使用nvidia-smi命令获取
|
43
|
+
# 使用nvidia-smi命令获取VRAM信息
|
45
44
|
result = subprocess.run(
|
46
45
|
["nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits"],
|
47
46
|
stdout=subprocess.PIPE,
|
48
47
|
text=True,
|
49
48
|
)
|
50
49
|
free_memory = result.stdout.strip().split("\n")
|
51
|
-
total_free_memory =
|
50
|
+
total_free_memory = 0
|
51
|
+
for mem in free_memory:
|
52
|
+
try:
|
53
|
+
total_free_memory += float(mem)
|
54
|
+
except:
|
55
|
+
pass
|
52
56
|
|
53
|
-
# 获取正在使用
|
57
|
+
# 获取正在使用VRAM的进程信息
|
54
58
|
process_result = subprocess.run(
|
55
59
|
[
|
56
60
|
"nvidia-smi",
|
@@ -67,40 +71,44 @@ def get_gpu_resources():
|
|
67
71
|
for process in process_info:
|
68
72
|
pid, process_name, used_memory = process.split(", ")
|
69
73
|
if "python" in process_name.lower():
|
70
|
-
|
71
|
-
|
74
|
+
try:
|
75
|
+
python_memory_usage += float(used_memory)
|
76
|
+
except:
|
77
|
+
pass
|
78
|
+
print("total_free_vram_memory: ", total_free_memory)
|
79
|
+
print("python_vram_memory_usage: ", python_memory_usage)
|
72
80
|
# 计算排除python进程后的总空闲内存
|
73
81
|
total_free_memory -= python_memory_usage
|
74
82
|
return round(total_free_memory / 1024, 2)
|
75
83
|
except Exception as e:
|
76
|
-
print(f"Error getting
|
84
|
+
print(f"Error getting VRAM resources: {e}")
|
77
85
|
return 0.0
|
78
86
|
|
79
87
|
|
80
|
-
|
81
|
-
|
82
|
-
|
88
|
+
def reserve_vram(gb, uid=None):
|
89
|
+
if uid is None:
|
90
|
+
uid = f"pid:{os.getpid()}"
|
83
91
|
with sqlite3.connect(DATABASE_PATH) as conn:
|
84
92
|
cursor = conn.cursor()
|
85
93
|
cursor.execute(f"SELECT SUM(reserved_gb) FROM {TABLE_NAME}")
|
86
94
|
total_reserved = cursor.fetchone()[0]
|
87
95
|
if total_reserved is None:
|
88
96
|
total_reserved = 0.0
|
89
|
-
available_gb =
|
97
|
+
available_gb = get_vram_resources() - total_reserved
|
90
98
|
if available_gb >= gb:
|
91
99
|
cursor.execute(
|
92
|
-
f"INSERT INTO {TABLE_NAME} (
|
93
|
-
(
|
100
|
+
f"INSERT INTO {TABLE_NAME} (uid, reserved_gb) VALUES (?, ?)",
|
101
|
+
(uid, gb),
|
94
102
|
)
|
95
103
|
conn.commit()
|
96
|
-
print(f"预留成功,剩余
|
104
|
+
print(f"预留成功,剩余VRAM大小: {available_gb - gb} GB")
|
97
105
|
return True
|
98
106
|
else:
|
99
|
-
print(f"预留失败,剩余
|
107
|
+
print(f"预留失败,剩余VRAM大小: {available_gb} GB")
|
100
108
|
return False
|
101
109
|
|
102
110
|
|
103
|
-
def
|
111
|
+
def reserve_vram_retry(gb, retry=None, uid=None):
|
104
112
|
if retry is None:
|
105
113
|
# 接近无限重试,python中允许无限大的整数,尽管sys.maxsize不是真正的无限大,但足够大
|
106
114
|
retry = sys.maxsize
|
@@ -108,50 +116,50 @@ def reserve_gpu_retry(gb, retry=None):
|
|
108
116
|
time.sleep(1)
|
109
117
|
if i % 10 == 0 or i < 10:
|
110
118
|
print(f"重试第{i}次")
|
111
|
-
if
|
119
|
+
if reserve_vram(gb, uid):
|
112
120
|
return True
|
113
121
|
return False
|
114
122
|
|
115
123
|
|
116
|
-
|
117
|
-
|
118
|
-
|
124
|
+
def release_vram(uid=None):
|
125
|
+
if uid is None:
|
126
|
+
uid = f"pid:{os.getpid()}"
|
119
127
|
with sqlite3.connect(DATABASE_PATH) as conn:
|
120
128
|
cursor = conn.cursor()
|
121
|
-
cursor.execute(f"DELETE FROM {TABLE_NAME} WHERE
|
129
|
+
cursor.execute(f"DELETE FROM {TABLE_NAME} WHERE uid = ?", (uid,))
|
122
130
|
conn.commit()
|
123
|
-
# 计算释放后的剩余
|
131
|
+
# 计算释放后的剩余VRAM大小
|
124
132
|
cursor.execute(f"SELECT SUM(reserved_gb) FROM {TABLE_NAME}")
|
125
133
|
total_reserved = cursor.fetchone()[0]
|
126
134
|
if total_reserved is None:
|
127
135
|
total_reserved = 0.0
|
128
|
-
available_gb =
|
129
|
-
print(f"释放成功,剩余
|
136
|
+
available_gb = get_vram_resources() - total_reserved
|
137
|
+
print(f"释放成功,剩余VRAM大小: {available_gb} GB")
|
130
138
|
|
131
139
|
|
132
140
|
# 注册退出时的清理函数
|
133
141
|
def cleanup():
|
134
|
-
|
135
|
-
print("程序退出,
|
142
|
+
release_vram()
|
143
|
+
print("程序退出,VRAM资源已释放")
|
136
144
|
|
137
145
|
|
138
146
|
def initialize_and_check():
|
139
147
|
initialize_db()
|
140
148
|
with sqlite3.connect(DATABASE_PATH) as conn:
|
141
149
|
cursor = conn.cursor()
|
142
|
-
cursor.execute(f"SELECT
|
150
|
+
cursor.execute(f"SELECT uid, reserved_gb FROM {TABLE_NAME}")
|
143
151
|
rows = cursor.fetchall()
|
144
152
|
for row in rows:
|
145
|
-
|
153
|
+
uid, reserved_gb = row
|
146
154
|
try:
|
147
155
|
# 检查进程是否存在
|
148
|
-
|
156
|
+
if uid.startswith("pid:"):
|
157
|
+
pid = int(uid.split(":")[1])
|
158
|
+
psutil.Process(pid)
|
149
159
|
except psutil.NoSuchProcess:
|
150
160
|
# 进程不存在,删除对应的记录
|
151
|
-
cursor.execute(
|
152
|
-
|
153
|
-
)
|
154
|
-
print(f"进程 {process_id} 不存在,已删除对应的预留记录")
|
161
|
+
cursor.execute(f"DELETE FROM {TABLE_NAME} WHERE uid = ?", (uid,))
|
162
|
+
print(f"进程 {uid} 不存在,已删除对应的预留记录")
|
155
163
|
conn.commit()
|
156
164
|
|
157
165
|
|
@@ -162,12 +170,12 @@ initialize_and_check()
|
|
162
170
|
atexit.register(cleanup)
|
163
171
|
|
164
172
|
if __name__ == "__main__":
|
165
|
-
if
|
166
|
-
print("(1)
|
167
|
-
if
|
168
|
-
print("(2)
|
173
|
+
if reserve_vram_retry(5):
|
174
|
+
print("(1)VRAM资源预留成功")
|
175
|
+
if reserve_vram_retry(5):
|
176
|
+
print("(2)VRAM资源预留成功")
|
169
177
|
time.sleep(100)
|
170
|
-
|
171
|
-
print("
|
178
|
+
release_vram()
|
179
|
+
print("VRAM资源释放成功")
|
172
180
|
else:
|
173
|
-
print("
|
181
|
+
print("VRAM资源不足,无法预留")
|
@@ -150,16 +150,21 @@ def create_openai_func_call_agent_executor(
|
|
150
150
|
# We call the tool_executor and get back a response
|
151
151
|
responses = await tool_executor.abatch(actions, **kwargs)
|
152
152
|
# We use the response to create a ToolMessage
|
153
|
-
tool_messages = [
|
154
|
-
|
153
|
+
tool_messages = []
|
154
|
+
for tool_call, response in zip(
|
155
|
+
last_message.additional_kwargs["tool_calls"], responses
|
156
|
+
):
|
157
|
+
if not isinstance(response, (str, int, float, bool, list, tuple)):
|
158
|
+
response = repr(
|
159
|
+
response
|
160
|
+
) # 不支持其他类型,包括dict也不支持,因此需要转换为字符串
|
161
|
+
|
162
|
+
message = ToolMessage(
|
155
163
|
tool_call_id=tool_call["id"],
|
156
164
|
content=response,
|
157
165
|
additional_kwargs={"name": tool_call["function"]["name"]},
|
158
166
|
)
|
159
|
-
|
160
|
-
last_message.additional_kwargs["tool_calls"], responses
|
161
|
-
)
|
162
|
-
]
|
167
|
+
tool_messages.append(message)
|
163
168
|
return tool_messages
|
164
169
|
|
165
170
|
workflow = MessageGraph()
|
pycoze/bot/bot.py
CHANGED
@@ -36,7 +36,7 @@ def agent_chat(bot_setting_file, history):
|
|
36
36
|
model=cfg["model"],
|
37
37
|
temperature=(
|
38
38
|
role_setting["temperature"] * 2
|
39
|
-
if cfg["model"].startswith("deepseek")
|
39
|
+
if cfg["model"].startswith("deepseek") or cfg["model"].startswith("yi-")
|
40
40
|
else role_setting["temperature"]
|
41
41
|
),
|
42
42
|
stop_sequences=[
|
@@ -45,7 +45,11 @@ def agent_chat(bot_setting_file, history):
|
|
45
45
|
], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
|
46
46
|
)
|
47
47
|
prompt = role_setting["prompt"]
|
48
|
-
if
|
48
|
+
if (
|
49
|
+
cfg["model"].startswith("deepseek")
|
50
|
+
or cfg["model"].startswith("yi-")
|
51
|
+
and len(tools) > 0
|
52
|
+
):
|
49
53
|
prompt += """
|
50
54
|
如果需要调用工具,请使用以正确的json格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
|
51
55
|
```json
|
@@ -2,18 +2,19 @@ pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
pycoze/access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
pycoze/access/tool_for_bot.py,sha256=Nv6XlxcqaLaMVQNDkpcthlqaSxyfHoOfWiTScQXO22Y,2656
|
5
|
+
pycoze/ai/__init__.py,sha256=Smivpb8qbRnzWkzKRe2IxsmKP5Dh8EvngDFdkD_DVLo,73
|
6
|
+
pycoze/ai/comfyui.py,sha256=u75tZywkuXiOdm7XST2kBAaveJKpPvY_qTQr_TN9sXk,795
|
7
|
+
pycoze/ai/vram_reserve.py,sha256=s55Cy-Q5mTq-k5oIPbAFwCfrjatjN0QTjQxW7WBTPZI,5738
|
5
8
|
pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
|
6
|
-
pycoze/bot/bot.py,sha256=
|
9
|
+
pycoze/bot/bot.py,sha256=aEG_B4ML6ONZ6FGcWYfBtlcMSuYBE8CjXo6ZYESQEaU,2869
|
7
10
|
pycoze/bot/agent/__init__.py,sha256=YR9vpkEQn1e4937r_xFPJXUCPBEJ0SFzEQDBe2x3-YA,157
|
8
11
|
pycoze/bot/agent/agent.py,sha256=7xnz4a8LDyAVU0O3tS6n7_jSnr_lukQxfNR2znCWoV4,4611
|
9
12
|
pycoze/bot/agent/assistant.py,sha256=QLeWaPi415P9jruYOm8qcIbC94cXXAhJYmLTkyC9NTQ,1267
|
10
13
|
pycoze/bot/agent/chat.py,sha256=kc0qgcrBSXdiMy49JwThZTV-0PAvzAhiUvbI5ILiSnU,571
|
11
14
|
pycoze/bot/agent/agent_types/__init__.py,sha256=W2jTNMLqUMqgCMG0Tw0d8n7WpsbsnIonqaPR-YLegLU,210
|
12
|
-
pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=
|
15
|
+
pycoze/bot/agent/agent_types/openai_func_call_agent.py,sha256=_nhlgmV_158fUEOtvB_R3my7Sz5FnCAhLZ1iuY1okOU,8270
|
13
16
|
pycoze/bot/agent/agent_types/react_agent.py,sha256=AnjHwHXVwLAm77ndglJGi4rQhqDGWaLuUfl46uZVSzM,6749
|
14
17
|
pycoze/bot/agent/agent_types/react_prompt.py,sha256=jyovokGaPzNIe5bvTRvn0gmsWLx5kpDIPmRwmEMCl-M,2142
|
15
|
-
pycoze/gpu/__init__.py,sha256=cuxwDdz2Oo-VcwZ50FtFtEIJXdqoz2el-n0QpSt_NMc,75
|
16
|
-
pycoze/gpu/gpu_reserve.py,sha256=JEyYh38v_RHolsqcIBoCjI8P-K-Ew3FsEyGhoISYmCQ,5415
|
17
18
|
pycoze/ui/__init__.py,sha256=7xAfL2lfG7-jllPJEZUJO89xUE9sNzvo1y0WmBswjBI,458
|
18
19
|
pycoze/ui/base.py,sha256=SCXVDK7PpMaBv6ovvabHcfRq_d2AWM0BRyxpNhuJN5A,1285
|
19
20
|
pycoze/ui/color.py,sha256=cT9Ib8uNzkOKxyW0IwVj46o4LwdB1xgNCj1_Rou9d_4,854
|
@@ -22,8 +23,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
|
|
22
23
|
pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
|
23
24
|
pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
|
24
25
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
25
|
-
pycoze-0.1.
|
26
|
-
pycoze-0.1.
|
27
|
-
pycoze-0.1.
|
28
|
-
pycoze-0.1.
|
29
|
-
pycoze-0.1.
|
26
|
+
pycoze-0.1.76.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
27
|
+
pycoze-0.1.76.dist-info/METADATA,sha256=ZPC58zR5Sued7MLMyaiOPY7T7PYzd-MCadxAStH_HV0,719
|
28
|
+
pycoze-0.1.76.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
29
|
+
pycoze-0.1.76.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
30
|
+
pycoze-0.1.76.dist-info/RECORD,,
|
pycoze/gpu/__init__.py
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
from .gpu_reserve import reserve_gpu_retry, release_gpu, reserve_gpu_retry
|
File without changes
|
File without changes
|
File without changes
|