pycoze 0.1.74__tar.gz → 0.1.76__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {pycoze-0.1.74 → pycoze-0.1.76}/PKG-INFO +1 -1
- pycoze-0.1.76/pycoze/ai/__init__.py +1 -0
- pycoze-0.1.76/pycoze/ai/comfyui.py +33 -0
- pycoze-0.1.74/pycoze/gpu/gpu_reserve.py → pycoze-0.1.76/pycoze/ai/vram_reserve.py +53 -45
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +11 -6
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/bot.py +6 -2
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze.egg-info/PKG-INFO +1 -1
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze.egg-info/SOURCES.txt +3 -2
- {pycoze-0.1.74 → pycoze-0.1.76}/setup.py +1 -1
- pycoze-0.1.74/pycoze/gpu/__init__.py +0 -1
- {pycoze-0.1.74 → pycoze-0.1.76}/LICENSE +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/README.md +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/access/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/access/tool_for_bot.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/agent.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/react_agent.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/react_prompt.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/assistant.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/bot/agent/chat.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/module.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/ui/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/ui/base.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/ui/color.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/ui/typ.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/ui/ui_def.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/utils/__init__.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/utils/arg.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze/utils/text_or_file.py +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze.egg-info/dependency_links.txt +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/pycoze.egg-info/top_level.txt +0 -0
- {pycoze-0.1.74 → pycoze-0.1.76}/setup.cfg +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
from .vram_reserve import reserve_vram_retry, release_vram, reserve_vram
|
@@ -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()
|
@@ -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
|
@@ -9,6 +9,9 @@ pycoze.egg-info/dependency_links.txt
|
|
9
9
|
pycoze.egg-info/top_level.txt
|
10
10
|
pycoze/access/__init__.py
|
11
11
|
pycoze/access/tool_for_bot.py
|
12
|
+
pycoze/ai/__init__.py
|
13
|
+
pycoze/ai/comfyui.py
|
14
|
+
pycoze/ai/vram_reserve.py
|
12
15
|
pycoze/bot/__init__.py
|
13
16
|
pycoze/bot/bot.py
|
14
17
|
pycoze/bot/agent/__init__.py
|
@@ -19,8 +22,6 @@ pycoze/bot/agent/agent_types/__init__.py
|
|
19
22
|
pycoze/bot/agent/agent_types/openai_func_call_agent.py
|
20
23
|
pycoze/bot/agent/agent_types/react_agent.py
|
21
24
|
pycoze/bot/agent/agent_types/react_prompt.py
|
22
|
-
pycoze/gpu/__init__.py
|
23
|
-
pycoze/gpu/gpu_reserve.py
|
24
25
|
pycoze/ui/__init__.py
|
25
26
|
pycoze/ui/base.py
|
26
27
|
pycoze/ui/color.py
|
@@ -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
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|