diff --git a/actions/action_base.py b/actions/action_base.py index f196eb0..5f8bcc1 100644 --- a/actions/action_base.py +++ b/actions/action_base.py @@ -18,21 +18,14 @@ class ActionBase(BaseModel): def __init__(self, **data: Any): super().__init__(**data) self.__ui_name_and_line_edit = {} - self.__config_ui = QtUtil.load_ui("config_page.ui") + self.__config_ui = QtUtil.load_ui("action_config_page.ui") + def run(self, *args, **kwargs): raise TypeError("Not realize run function") def run_with_out_arg(self): - return self.run(**self.action_arg) - - def convert_langchain_tool(self): - return StructuredTool.from_function( - func=self.run, - name=self.name, - description=self.description, - args_schema=self.args - ) + return self.run(**self.args.model_dump()) def _run(self, *args, **kwargs): self.run(*args, **kwargs) @@ -63,10 +56,11 @@ class ActionBase(BaseModel): self.__config_ui.hide() def __save_button_clicked(self, the_insert_row): - args = {} from pages.edit_page import GlobalUtil + arg = {} for arg_name in self.__ui_name_and_line_edit: - args[arg_name] = self.__ui_name_and_line_edit[arg_name].text() + arg[arg_name] = self.__ui_name_and_line_edit[arg_name].text() + self.args= self.args.model_validate(arg) self.action_pos = the_insert_row self.action_level = 0 # 向新位置增加元素 diff --git a/actions/open_application_action.py b/actions/open_application_action.py index f6003d4..acb143f 100644 --- a/actions/open_application_action.py +++ b/actions/open_application_action.py @@ -15,4 +15,5 @@ class OpenApplicationAction(ActionBase): args: OpenApplicationInput def run(self, path): + print("运行了这里", path) subprocess.Popen(path) diff --git a/actions/search_engine_action.py b/actions/search_engine_action.py index 433a47d..dc16d23 100644 --- a/actions/search_engine_action.py +++ b/actions/search_engine_action.py @@ -5,7 +5,7 @@ from utils.selenium_util import SeleniumUtil class SearchInput(BaseModel): - key: str = Field(description="要查询的关键词", default="") + key: str = Field(description="要查询的内容", title="要查询的内容", default="") # 利用搜索引擎搜索关键词 diff --git a/agent/woker_agent.py b/agent/woker_agent.py index 62e044b..05099ce 100644 --- a/agent/woker_agent.py +++ b/agent/woker_agent.py @@ -10,16 +10,6 @@ class WorkerAgent: def get_executor(self): llm = LLM_Util().llm() tools = ToolsUtil.get_tools() - # prompt = PromptTemplate( - # template="{instructions}\n\nTOOLS:\n------\n\n你可以使用以下工具:\n\n{tools}\n\n使用工具时,请使用以下格式:\n\n" - # "```\nThought: 我需要使用工具吗? 需要\nAction: {tool_names}\n" - # "Action Input: Action的输入'\n'Observation: 运行Action的结果\n```" - # "\n\n当输出内容时,或者不需要使用工具,必须使用以下格式: " - # "\n\n```\nThought: 我需要使用工具吗? 不需要\nFinal Answer: [你的回复]\n```\n\n开始!\n\n" - # "Previous conversation history:\n{chat_history}\n\nNew input: {input}\n{agent_scratchpad}", - # input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'], - # partial_variables={'chat_history': '', 'instructions': ""} - # ) prompt = hub.pull("langchain-ai/react-agent-template") prompt.partial(instructions="") agent = create_react_agent(llm=llm, tools=tools, prompt=prompt) diff --git a/config_tmp.yaml b/config_tmp.yaml index ab15752..ea82cb2 100644 --- a/config_tmp.yaml +++ b/config_tmp.yaml @@ -21,4 +21,4 @@ components: browser: description: 配置本电脑的浏览器信息,用于浏览器自动化 config: - brow: edge \ No newline at end of file + 浏览器类型: edge \ No newline at end of file diff --git a/main.py b/main.py index 70bb9f3..421b668 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import traceback import leancloud from PyQt6.QtWidgets import QApplication +from actions import open_application_action from pages.chat_page import ChatPage from pages.edit_page import GlobalUtil, EditPage @@ -48,6 +49,7 @@ def excepthook(exc_type, exc_value, exc_tb): def load(): # 生成 config.yaml 文件 Config() + EditPage.global_load() @@ -56,7 +58,7 @@ if __name__ == "__main__": sys.excepthook = excepthook app = QApplication(sys.argv) load() - # page = FuncListPage() page = ChatPage() page.show() sys.exit(app.exec()) + diff --git a/pages/action_config_page.ui b/pages/action_config_page.ui new file mode 100644 index 0000000..540dd66 --- /dev/null +++ b/pages/action_config_page.ui @@ -0,0 +1,121 @@ + + + MainWindow + + + + 0 + 0 + 586 + 447 + + + + MainWindow + + + + + + 20 + 60 + 531 + 261 + + + + + + + + 560 + 40 + 20 + 321 + + + + Qt::Vertical + + + + + + 460 + 380 + 51 + 21 + + + + 保存 + + + + + + 520 + 380 + 51 + 21 + + + + 取消 + + + + + + 10 + 30 + 54 + 12 + + + + 配置 + + + + + + 0 + 0 + 54 + 12 + + + + description + + + + + + 0 + 10 + 581 + 16 + + + + Qt::Horizontal + + + + + + + 0 + 0 + 586 + 22 + + + + + + + + \ No newline at end of file diff --git a/pages/edit_function_page.py b/pages/edit_function_page.py index eaaf3e7..183e891 100644 --- a/pages/edit_function_page.py +++ b/pages/edit_function_page.py @@ -6,7 +6,7 @@ from PyQt6.QtWidgets import QAbstractItemView, QListWidget, QListWidgetItem from actions.action_util import ActionUtil from pages.edit_action_list_view import ActionListItem -from pages.global_util import GlobalUtil +from utils.global_util import GlobalUtil class FunctionListView(QListWidget): diff --git a/pages/edit_page.py b/pages/edit_page.py index afc4184..393fa86 100644 --- a/pages/edit_page.py +++ b/pages/edit_page.py @@ -1,8 +1,9 @@ from pages.edit_action_list_view import ActionList from pages.edit_function_page import FunctionListView -from pages.global_util import GlobalUtil +from utils.global_util import GlobalUtil from utils.qt_util import QtUtil from PyQt6.QtWidgets import QMainWindow +from PyQt6.QtCore import pyqtSignal @@ -10,24 +11,26 @@ interface_ui = QtUtil.load_ui_type("edit_page.ui") class EditPage(QMainWindow, interface_ui): - def __init__(self, parent_page, func_status, func_list_pos_row, func_list_pos_column, action_list: ActionList = None): + # 页面关闭的信号 + page_closed = pyqtSignal(str) + + def __init__(self, func_status, func_list_pos_row, func_list_pos_column, action_list: ActionList = None, func_name="默认名称", func_description="无"): self.func_list_pos_column = func_list_pos_column self.func_list_pos_row = func_list_pos_row # 属于通用还是专属 self.func_status = func_status - self.func_description = "" + self.func_description = func_description # 在func上的名称 - self.func_name = "默认名称" + self.func_name = func_name if not action_list: action_list = ActionList() self.action_list = action_list - self.parent_page = parent_page super().__init__() self.setupUi(self) self.setup_up() def closeEvent(self, event): - self.parent_page.show() + self.page_closed.emit(self.func_name) def dump(self): return {"func_list_pos_column": self.func_list_pos_column, @@ -44,7 +47,7 @@ class EditPage(QMainWindow, interface_ui): function_list_view = FunctionListView() self.function_list_layout.addWidget(function_list_view) self.action_list_view_layout.addWidget(self.action_list) - self.run_button.clicked.connect(self.__run_button_click) + self.run_button.clicked.connect(self.run_action) self.save_button.clicked.connect(self.__save_button_click) self.cancel_button.clicked.connect(self.__cancel_button_click) # 设置间距 @@ -65,10 +68,11 @@ class EditPage(QMainWindow, interface_ui): self.close() - def __run_button_click(self): + def run_action(self, *args, **kwargs): + print("run_action", args, kwargs) for index in range(self.action_list.count()): func = self.action_list.item(index) - res = func.__getattribute__("get_action")().run_with_out_arg() + res = func.action.run_with_out_arg() print("执行结果:", res) @@ -81,7 +85,7 @@ class EditPage(QMainWindow, interface_ui): @staticmethod - def global_load( parent): + def global_load(): # 根据配置文件的配置,从本地文件中或者网上读取 edit_pages_json = GlobalUtil.read_from_local() for edit_page_json in edit_pages_json: @@ -91,9 +95,9 @@ class EditPage(QMainWindow, interface_ui): func_status=edit_page_json["func_status"], func_list_pos_row=edit_page_json["func_list_pos_row"], func_list_pos_column=edit_page_json["func_list_pos_column"], - # TODO待优化加载问题 - action_list=ActionList.load(edit_page_json["action_list"]), - parent_page=parent + func_name = edit_page_json["func_name"], + func_description = edit_page_json["func_description"], + action_list=ActionList.load(edit_page_json["action_list"]) ) edit_page.func_name = edit_page_json["func_name"] edit_page.func_description = edit_page_json["func_description"] diff --git a/pages/func_list_page.py b/pages/func_list_page.py index 29fe9d4..59b5957 100644 --- a/pages/func_list_page.py +++ b/pages/func_list_page.py @@ -1,5 +1,5 @@ import typing -from PyQt6 import QtCore +from PyQt6 import QtCore, QtGui from PyQt6.QtCore import QSize, Qt from PyQt6.QtGui import QIcon from PyQt6.QtWidgets import QGraphicsOpacityEffect, QToolButton, QMainWindow, QWidget @@ -37,10 +37,12 @@ class AddFuncButton(QToolButton): def click(self): self.func_list_page.hide() if not self.edit_page: - edit_page = EditPage(self.func_list_page, self.func_status, self.func_list_pos_row, self.func_list_pos_column) + edit_page = EditPage(self.func_status, self.func_list_pos_row, self.func_list_pos_column) GlobalUtil.current_page = edit_page else: GlobalUtil.current_page = self.edit_page + # 接收信号 + GlobalUtil.current_page.page_closed.connect(lambda: self.func_list_page.show()) GlobalUtil.current_page.show() def enterEvent(self, event): @@ -51,25 +53,24 @@ class AddFuncButton(QToolButton): if not self.edit_page: self.opacity_effect.setOpacity(0) - - - interface_ui = QtUtil.load_ui_type("func_list_page.ui") - - class FuncListPage(QMainWindow, interface_ui): def __init__(self, parent): self.parent_ui = parent super().__init__() self.setupUi(self) - self.setup_up() + # self.setup_up() # 关闭事件 def closeEvent(self, event): self.parent_ui.show() + + def showEvent(self, a0) -> None: + self.setup_up() + return super().showEvent(a0) + def setup_up(self): - EditPage.global_load(self) # self.ui = QtUtil.load_ui("func_list_page.ui") # 四行三列,辅满通用应用列表布局 for i in range(3): # 3行 diff --git a/tools/tools_util.py b/tools/tools_util.py index 209458e..87c3f13 100644 --- a/tools/tools_util.py +++ b/tools/tools_util.py @@ -2,9 +2,23 @@ from tools.FindDesktopPath import FindDesktopPath from tools.ListAllFile import ListAllFile from tools.ListDesktopFiles import ListDesktopFiles from tools.OpenApplication import OpenApplicationAction +from langchain.tools import StructuredTool +from utils.global_util import GlobalUtil class ToolsUtil: @staticmethod def get_tools(): - return [OpenApplicationAction(), FindDesktopPath(), ListAllFile(), ListDesktopFiles()] + print("get_tools") + action_tools = [] + # 从 function_list 中生成工具 + for edit_page in GlobalUtil.edit_page_global: + # 动态生成 langchain 工具 + langchain_tools = StructuredTool.from_function( + func=edit_page.run_action, + name=edit_page.func_name, + description=edit_page.func_description, + return_direct=True) + action_tools.append(langchain_tools) + print(action_tools) + return [OpenApplicationAction(), FindDesktopPath(), ListAllFile(), ListDesktopFiles()] + action_tools diff --git a/pages/global_util.py b/utils/global_util.py similarity index 100% rename from pages/global_util.py rename to utils/global_util.py