📝 更新(word_action.py, woker_agent.py, chat_page.py, llm_util.py):更正导入问题,调整工作代理逻辑。

This commit is contained in:
ruotongyu
2024-05-29 23:51:29 +08:00
parent 10cb0cb2ca
commit ecc0a975af
4 changed files with 18 additions and 60 deletions

View File

@@ -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):

View File

@@ -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

View File

@@ -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)

View File

@@ -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