镜像自地址
https://github.com/binary-husky/gpt_academic.git
已同步 2025-12-06 06:26:47 +00:00
autogen
这个提交包含在:
@@ -2,28 +2,28 @@ from toolbox import get_log_folder, update_ui, gen_time_str, get_conf, promote_f
|
||||
from crazy_functions.agent_fns.watchdog import WatchDog
|
||||
import time, os
|
||||
|
||||
class PipeCom():
|
||||
class PipeCom:
|
||||
def __init__(self, cmd, content) -> None:
|
||||
self.cmd = cmd
|
||||
self.content = content
|
||||
|
||||
|
||||
class PluginMultiprocessManager():
|
||||
class PluginMultiprocessManager:
|
||||
def __init__(self, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
||||
# ⭐ 主进程
|
||||
self.autogen_work_dir = os.path.join(get_log_folder('autogen'), gen_time_str())
|
||||
# ⭐ run in main process
|
||||
self.autogen_work_dir = os.path.join(get_log_folder("autogen"), gen_time_str())
|
||||
self.previous_work_dir_files = {}
|
||||
self.llm_kwargs = llm_kwargs
|
||||
self.plugin_kwargs = plugin_kwargs
|
||||
self.chatbot = chatbot
|
||||
self.history = history
|
||||
self.system_prompt = system_prompt
|
||||
self.web_port = web_port
|
||||
# self.web_port = web_port
|
||||
self.alive = True
|
||||
self.use_docker = get_conf('AUTOGEN_USE_DOCKER')
|
||||
self.use_docker = get_conf("AUTOGEN_USE_DOCKER")
|
||||
|
||||
# create a thread to monitor self.heartbeat, terminate the instance if no heartbeat for a long time
|
||||
timeout_seconds = 5*60
|
||||
timeout_seconds = 5 * 60
|
||||
self.heartbeat_watchdog = WatchDog(timeout=timeout_seconds, bark_fn=self.terminate, interval=5)
|
||||
self.heartbeat_watchdog.begin_watch()
|
||||
|
||||
@@ -35,8 +35,9 @@ class PluginMultiprocessManager():
|
||||
return self.alive
|
||||
|
||||
def launch_subprocess_with_pipe(self):
|
||||
# ⭐ 主进程
|
||||
# ⭐ run in main process
|
||||
from multiprocessing import Process, Pipe
|
||||
|
||||
parent_conn, child_conn = Pipe()
|
||||
self.p = Process(target=self.subprocess_worker, args=(child_conn,))
|
||||
self.p.daemon = True
|
||||
@@ -46,14 +47,14 @@ class PluginMultiprocessManager():
|
||||
def terminate(self):
|
||||
self.p.terminate()
|
||||
self.alive = False
|
||||
print('[debug] instance terminated')
|
||||
print("[debug] instance terminated")
|
||||
|
||||
def subprocess_worker(self, child_conn):
|
||||
# ⭐⭐ 子进程
|
||||
# ⭐⭐ run in subprocess
|
||||
raise NotImplementedError
|
||||
|
||||
def send_command(self, cmd):
|
||||
# ⭐ 主进程
|
||||
# ⭐ run in main process
|
||||
self.parent_conn.send(PipeCom("user_input", cmd))
|
||||
|
||||
def immediate_showoff_when_possible(self, fp):
|
||||
@@ -63,7 +64,10 @@ class PluginMultiprocessManager():
|
||||
# 如果是文本文件, 则直接显示文本内容
|
||||
if file_type.lower() in ['png', 'jpg']:
|
||||
image_path = os.path.abspath(fp)
|
||||
self.chatbot.append(['检测到新生图像:', f'本地文件预览: <br/><div align="center"><img src="file={image_path}"></div>'])
|
||||
self.chatbot.append([
|
||||
'检测到新生图像:',
|
||||
f'本地文件预览: <br/><div align="center"><img src="file={image_path}"></div>'
|
||||
])
|
||||
yield from update_ui(chatbot=self.chatbot, history=self.history)
|
||||
|
||||
def overwatch_workdir_file_change(self):
|
||||
@@ -78,7 +82,7 @@ class PluginMultiprocessManager():
|
||||
file_path = os.path.join(root, file)
|
||||
if file_path not in self.previous_work_dir_files.keys():
|
||||
last_modified_time = os.stat(file_path).st_mtime
|
||||
self.previous_work_dir_files.update({file_path:last_modified_time})
|
||||
self.previous_work_dir_files.update({file_path: last_modified_time})
|
||||
change_list.append(file_path)
|
||||
else:
|
||||
last_modified_time = os.stat(file_path).st_mtime
|
||||
@@ -86,8 +90,8 @@ class PluginMultiprocessManager():
|
||||
self.previous_work_dir_files[file_path] = last_modified_time
|
||||
change_list.append(file_path)
|
||||
if len(change_list) > 0:
|
||||
file_links = ''
|
||||
for f in change_list:
|
||||
file_links = ""
|
||||
for f in change_list:
|
||||
res = promote_file_to_downloadzone(f)
|
||||
file_links += f'<br/><a href="file={res}" target="_blank">{res}</a>'
|
||||
yield from self.immediate_showoff_when_possible(f)
|
||||
@@ -117,19 +121,25 @@ class PluginMultiprocessManager():
|
||||
# the heartbeat watchdog might have it killed
|
||||
self.terminate()
|
||||
return "terminate"
|
||||
|
||||
if self.parent_conn.poll():
|
||||
self.feed_heartbeat_watchdog()
|
||||
if "[GPT-Academic] 等待中" in self.chatbot[-1][-1]:
|
||||
self.chatbot.pop(-1) # remove the last line
|
||||
if "等待您的进一步指令" in self.chatbot[-1][-1]:
|
||||
self.chatbot.pop(-1) # remove the last line
|
||||
if '[GPT-Academic] 等待中' in self.chatbot[-1][-1]:
|
||||
self.chatbot.pop(-1) # remove the last line
|
||||
msg = self.parent_conn.recv() # PipeCom
|
||||
if msg.cmd == "done":
|
||||
self.chatbot.append([f"结束", msg.content]); self.cnt += 1
|
||||
self.chatbot.append([f"结束", msg.content])
|
||||
self.cnt += 1
|
||||
yield from update_ui(chatbot=self.chatbot, history=self.history)
|
||||
self.terminate(); break
|
||||
self.terminate()
|
||||
break
|
||||
if msg.cmd == "show":
|
||||
yield from self.overwatch_workdir_file_change()
|
||||
self.chatbot.append([f"运行阶段-{self.cnt}", msg.content]); self.cnt += 1
|
||||
self.chatbot.append([f"运行阶段-{self.cnt}", msg.content])
|
||||
self.cnt += 1
|
||||
yield from update_ui(chatbot=self.chatbot, history=self.history)
|
||||
if msg.cmd == "interact":
|
||||
yield from self.overwatch_workdir_file_change()
|
||||
@@ -159,13 +169,13 @@ class PluginMultiprocessManager():
|
||||
return "terminate"
|
||||
|
||||
def subprocess_worker_wait_user_feedback(self, wait_msg="wait user feedback"):
|
||||
# ⭐⭐ 子进程
|
||||
# ⭐⭐ run in subprocess
|
||||
patience = 5 * 60
|
||||
begin_waiting_time = time.time()
|
||||
self.child_conn.send(PipeCom("interact", wait_msg))
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
if self.child_conn.poll():
|
||||
if self.child_conn.poll():
|
||||
wait_success = True
|
||||
break
|
||||
if time.time() - begin_waiting_time > patience:
|
||||
@@ -173,4 +183,3 @@ class PluginMultiprocessManager():
|
||||
wait_success = False
|
||||
break
|
||||
return wait_success
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户