From ecc0a975afd3e2279d7a7bac5c503a7142be2760 Mon Sep 17 00:00:00 2001 From: ruotongyu Date: Wed, 29 May 2024 23:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20=E6=9B=B4=E6=96=B0(word=5Faction?= =?UTF-8?q?.py,=20woker=5Fagent.py,=20chat=5Fpage.py,=20llm=5Futil.py)?= =?UTF-8?q?=EF=BC=9A=E6=9B=B4=E6=AD=A3=E5=AF=BC=E5=85=A5=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E8=B0=83=E6=95=B4=E5=B7=A5=E4=BD=9C=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/word_action.py | 2 +- agent/woker_agent.py | 62 ++++++++++-------------------------------- pages/chat_page.py | 12 ++------ utils/llm_util.py | 2 +- 4 files changed, 18 insertions(+), 60 deletions(-) diff --git a/actions/word_action.py b/actions/word_action.py index 7a7150d..56ccb07 100644 --- a/actions/word_action.py +++ b/actions/word_action.py @@ -1,4 +1,4 @@ -from action_util import action, ActionBase +from actions.action_base import action, ActionBase class WordAction(ActionBase): actions_description = "word基本操作" def __init__(self): diff --git a/agent/woker_agent.py b/agent/woker_agent.py index 4917d44..57936e5 100644 --- a/agent/woker_agent.py +++ b/agent/woker_agent.py @@ -1,55 +1,21 @@ -from langchain import hub -from langchain.agents import create_react_agent, AgentExecutor -from langchain_core.prompts import PromptTemplate, ChatMessagePromptTemplate from actions.action_util import ActionUtil -from system_prompt import system_prompt -from tools.tools_util import ToolsUtil +from agent.system_prompt import system_prompt from utils.llm_util import LLM_Util class WorkerAgent: - def get_executor(self): - llm = LLM_Util().llm() - tools = ToolsUtil.get_tools() - prompt = hub.pull("langchain-ai/react-agent-template") - prompt.partial(instructions="") - agent = create_react_agent(llm=llm, tools=tools, prompt=prompt) - agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) - return agent_executor - - def run(self, question): - return self.get_executor().invoke({"input": question})["output"] - - @staticmethod - def get_iter(question): - llm = LLM_Util().llm() - tools = ToolsUtil.get_tools() - example = """Thought: 我需要使用工具吗? 需要\nAction: 桌面路径\nAction Input: ""\nObservation: c:/path/develop\n\n -Thought: 我需要使用工具吗? 需要\nAction: 打开应用\nAction Input: ""\nObservation: 打开成功\n\n -Thought: 我需要使用工具吗? 不需要\nFinal Answer: 您的桌面上有以下文件\n -""" - prompt = PromptTemplate( - template="### TOOLS ###\n{tools}#######\n" - "### THOUGHT ###Thought:我需要使用工具吗? 需要\n" - "Action:{tool_names}\n" - "Action Input:Action的输入,如果没有参数请设置为""\n" - "Observation:运行[ACTION]得到的结果\n\n" - "当输出内容或者不需要使用工具,必须使用以下格式: Thought: 我需要使用工具吗? 不需要\nFinal Answer: [你的回复]######\n" - "### EXAMPLE ###\n{example}######\n" - "### PREVIOUS CONVERSATION HISTORY ###\n{chat_history}######\n" - "### NEW INPUT ###\n{input}######\n{agent_scratchpad}", - input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'], - partial_variables={'chat_history': '', 'instructions': ""} - ) - prompt = prompt.partial(example=example) - agent = create_react_agent(llm=llm, tools=tools, prompt=prompt) - agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) - return agent_executor.iter({"input": question}) - - def run(self, question): + def __init__(self): action_descriptions = "" - for action in ActionUtil.get_actions(): + for action_class in ActionUtil.get_actions(): + action = action_class() action_descriptions += action.package_actions_description() + "\n" - messages = [{"content": system_prompt.substitute(python_code=action_descriptions), "role": "system"}] - messages.append({"content": question, "role": "user"}) - LLM_Util().invoke(messages) + self.messages = [{"content": system_prompt.substitute(python_code=action_descriptions), "role": "system"}] + + + def run(self, question): + self.messages.append({"content": question, "role": "user"}) + res = LLM_Util().invoke(self.messages) + self.messages.append({"content": res, "role": "assistant"}) + return res + + diff --git a/pages/chat_page.py b/pages/chat_page.py index 128a899..87a7f1d 100644 --- a/pages/chat_page.py +++ b/pages/chat_page.py @@ -101,16 +101,8 @@ class WorkerThread(QThread): def run(self): try: - agent_iter = WorkerAgent().get_iter(self.text) - for step in agent_iter: - content = "" - if output := step.get("intermediate_step"): - action, value = output[0] - content = f"{action.tool} \n{value}" - elif step.get("output"): - content = step["output"] - content = content.replace("```", "") - self.finished_signal.emit(content) + content = WorkerAgent().run(self.text) + self.finished_signal.emit(content) except Exception as e: traceback.print_exc(e) diff --git a/utils/llm_util.py b/utils/llm_util.py index b0fd4f0..c251319 100644 --- a/utils/llm_util.py +++ b/utils/llm_util.py @@ -20,6 +20,6 @@ class LLM_Util: if self.base_url == "": response = completion(model=self.model, api_key=self.api_key, messages=messages) else: - response = completion(model=self.model, base_url=self.base_url, api_key=self.key, messages=messages) + response = completion(model=self.model, base_url=self.base_url, api_key=self.api_key, messages=messages) return response.choices[0].message.content \ No newline at end of file