mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 13:07:17 +08:00
new
This commit is contained in:
14
README.md
14
README.md
@@ -60,10 +60,20 @@ driver.quit()
|
||||
|
||||
|
||||
## 安装依赖
|
||||
建议使用 python3.9,安装依赖:
|
||||
安装python的3.9+版本。
|
||||
|
||||
安装包依赖管理工具 poetry,打开 PowerShell 或命令提示符,使用以下命令下载并安装 Poetry:
|
||||
```cmd
|
||||
curl -sSL https://install.python-poetry.org/ | python
|
||||
```
|
||||
配置 poetry,将虚拟环境配置到当前项目目录:
|
||||
```commandline
|
||||
pip install -r requirements.txt
|
||||
poetry config virtualenvs.in-project true
|
||||
```
|
||||
创建虚拟环境并安装依赖:
|
||||
```commandline
|
||||
poetry env use python
|
||||
poetry install
|
||||
```
|
||||
|
||||
## 运行
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import logging
|
||||
from agent.agent_base import AgentBase
|
||||
from tools.tools_base import ToolsBase
|
||||
from tools.tool_base import ToolBase
|
||||
from tools.tools_factory import ToolsFactory
|
||||
from work_principle.okr_principle import OKR_Object
|
||||
|
||||
class PlanAgent(AgentBase):
|
||||
def __init__(self):
|
||||
self.logger = logging.getLogger(__name__)
|
||||
super().__init__('你是一名计划拆解者,挑选工具列表中合适的工具将object拆解成key_result然后自动填充参数,建议拆解成多个key_result,' +
|
||||
'多个key_result完成这一个目标,返回格式如下:[{"tools_name":"web_browser","request_param":"请求参数"}]')
|
||||
|
||||
super().__init__('#Goals:\n你是一名计划拆解者,将object拆解成key_result,\n#Workflow:1.')
|
||||
'''
|
||||
'''
|
||||
# '多个key_result完成这一个目标,返回格式如下:{"tools_name":"web_browser","request_param":"请求参数"}'
|
||||
# 根据object和已有的工具能力拆解成key result,key
|
||||
def aligning(self, okr_object: OKR_Object):
|
||||
raw = okr_object.raw_user_task
|
||||
|
||||
10
main.py
10
main.py
@@ -6,25 +6,25 @@ import logging
|
||||
# 设置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
class AutoMate:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def rule_define(self):
|
||||
# 与用户对齐任务
|
||||
while True:
|
||||
o_kr = OKR_Object("因为想要增加编程效率,对比一下copilot和curson谁更好用,比较提示词数量、安装易用性,给出不少于100字的文章")
|
||||
o_kr = OKR_Object(
|
||||
"因为想要增加编程效率,对比一下copilot和curson谁更好用,比较提示词数量、安装易用性,给出不少于100字的文章")
|
||||
ManagerAgent().optimization_Object(o_kr)
|
||||
r = input(f"最终对齐的任务是:{o_kr.raw_user_task},一切都OK对吧?y/n\n")
|
||||
if r == "y":
|
||||
break
|
||||
|
||||
|
||||
# 让计划拆解者拆解任务
|
||||
PlanAgent().aligning(o_kr)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
automator = AutoMate()
|
||||
automator.rule_define()
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
[tool.poetry]
|
||||
name = "automate"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["yuruo"]
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
PyYAML = "6.0.1"
|
||||
requests = "2.31.0"
|
||||
selenium = "4.16.0"
|
||||
webdriver-manager = "4.0.1"
|
||||
langchain = "0.1.7"
|
||||
langchain-openai = "0.0.6"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
@@ -1,10 +1,9 @@
|
||||
import unittest
|
||||
|
||||
from agent.plan_agent import PlanAgent
|
||||
from work_principle.okr_principle import OKR_Object
|
||||
|
||||
|
||||
class TestPlanAgent(unittest.TestCase):
|
||||
class TestPlanAgent:
|
||||
def test_aligning(self):
|
||||
# Create an instance of the PlanAgent class
|
||||
plan_agent = PlanAgent()
|
||||
@@ -15,5 +14,3 @@ class TestPlanAgent(unittest.TestCase):
|
||||
plan_agent.aligning(okr_object)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
39
tests/test_web_browser.py
Normal file
39
tests/test_web_browser.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from langchain.agents import create_openai_functions_agent, AgentExecutor
|
||||
from langchain_core.messages import HumanMessage
|
||||
from langchain_core.prompts import SystemMessagePromptTemplate, PromptTemplate, MessagesPlaceholder, \
|
||||
HumanMessagePromptTemplate, ChatPromptTemplate
|
||||
from langchain_core.utils.function_calling import convert_to_openai_function
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from tools.web_browser_tool import WebBrowserTool
|
||||
from utils.llm_util import LLMUtil
|
||||
|
||||
|
||||
class TestWebBrowser:
|
||||
def test_web_browser(self):
|
||||
model = LLMUtil().llm()
|
||||
tools = [WebBrowserTool()]
|
||||
model_with_functions = model.bind_functions(tools)
|
||||
s = model_with_functions.invoke([HumanMessage(
|
||||
content="帮我查询一下这个网页的内容 https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9510051560337988929%22%7D&n_type=-1&p_from=-1")])
|
||||
print(s.additional_kwargs["function_call"])
|
||||
|
||||
def test_agent(self):
|
||||
prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
SystemMessagePromptTemplate(
|
||||
prompt=PromptTemplate(input_variables=[], template='你是一个工作助手')),
|
||||
MessagesPlaceholder(variable_name='chat_history', optional=True),
|
||||
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template='{input}')),
|
||||
MessagesPlaceholder(variable_name='agent_scratchpad')
|
||||
]
|
||||
)
|
||||
model = LLMUtil().llm()
|
||||
tools = [WebBrowserTool()]
|
||||
agent = create_openai_functions_agent(model, tools, prompt)
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, return_intermediate_steps=True)
|
||||
# agent_executor.invoke({"input": "你好!你是谁"})
|
||||
agent_executor.invoke({"input": "帮我查询一下这个网页的内容 https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9510051560337988929%22%7D&n_type=-1&p_from=-1"})
|
||||
@@ -1,10 +1,10 @@
|
||||
from langchain_core.messages import SystemMessage, HumanMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from tools.tools_base import ToolsBase
|
||||
from tools.tool_base import ToolBase
|
||||
|
||||
|
||||
class LLMTools(ToolsBase):
|
||||
class LLMTools(ToolBase):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.name = "llm_tools"
|
||||
|
||||
13
tools/tool_base.py
Normal file
13
tools/tool_base.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from abc import ABC
|
||||
from typing import Any
|
||||
|
||||
from langchain_core.tools import BaseTool
|
||||
|
||||
from utils.config import Config
|
||||
|
||||
|
||||
class ToolBase(BaseTool, ABC):
|
||||
def get_info(self):
|
||||
return {"name": self.name, "description": self.description, "param": self.request_param, "return_content": self.return_content}
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
from utils.config import Config
|
||||
|
||||
|
||||
class ToolsBase:
|
||||
def __init__(self):
|
||||
self.name = ""
|
||||
self.description = ""
|
||||
self.request_param = ""
|
||||
self.return_content = ""
|
||||
self.config = Config()
|
||||
|
||||
def get_info(self):
|
||||
return {"name": self.name, "description": self.description, "param": self.request_param, "return_content": self.return_content}
|
||||
|
||||
def run(self, param=None):
|
||||
pass
|
||||
@@ -9,9 +9,10 @@ from webdriver_manager.opera import OperaDriverManager
|
||||
from selenium.webdriver.edge.service import Service as EdgeService
|
||||
from selenium.webdriver.common.by import By
|
||||
|
||||
from tools.tools_base import ToolsBase
|
||||
from tools.tool_base import ToolBase
|
||||
|
||||
class WebBrowserElement(ToolsBase):
|
||||
|
||||
class WebBrowserElement(ToolBase):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.name = "web_element"
|
||||
@@ -28,5 +29,3 @@ class WebBrowserElement(ToolsBase):
|
||||
elif action == "send_text":
|
||||
driver.find_element(By.XPATH, xpath).send_keys(param["text"])
|
||||
return True
|
||||
|
||||
|
||||
|
||||
55
tools/web_browser_tool.py
Normal file
55
tools/web_browser_tool.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional, Type, Any
|
||||
|
||||
from langchain_core.tools import BaseTool
|
||||
from selenium.webdriver.chrome.service import Service as ChromeService
|
||||
from langchain_core.callbacks import CallbackManagerForToolRun, AsyncCallbackManagerForToolRun
|
||||
from langchain.pydantic_v1 import BaseModel, Field
|
||||
from selenium import webdriver
|
||||
from webdriver_manager.chrome import ChromeDriverManager
|
||||
from webdriver_manager.microsoft import EdgeChromiumDriverManager
|
||||
from selenium.webdriver.edge.service import Service as EdgeService
|
||||
|
||||
from tools.tool_base import ToolBase
|
||||
from utils.config import Config
|
||||
|
||||
|
||||
class SearchInput(BaseModel):
|
||||
url: str = Field(description="要查询的网址")
|
||||
|
||||
|
||||
class WebBrowserTool(ToolBase):
|
||||
name = "web_browser"
|
||||
description = "利用浏览器访问url,得到网页源码"
|
||||
args_schema: Type[BaseModel] = SearchInput
|
||||
|
||||
def _run(self, url: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
|
||||
"""Use the tool."""
|
||||
config = Config()
|
||||
# Download webdriver based on browser type
|
||||
browser_type = config.BROWSER.get("browser_type")
|
||||
if browser_type == "chrome":
|
||||
options = webdriver.ChromeOptions()
|
||||
options.add_argument("--headless") # Enable headless mode
|
||||
webdriver_manager = ChromeDriverManager()
|
||||
driver = webdriver.Chrome(service=ChromeService(webdriver_manager.install()), options=options)
|
||||
elif browser_type == "edge":
|
||||
options = webdriver.EdgeOptions()
|
||||
options.add_argument("--headless") # Enable headless mode
|
||||
webdriver_manager = EdgeChromiumDriverManager()
|
||||
driver = webdriver.Edge(service=EdgeService(webdriver_manager.install()), options=options)
|
||||
else:
|
||||
return ""
|
||||
driver.implicitly_wait(10)
|
||||
driver.get(url)
|
||||
driver.quit()
|
||||
print(f"res {driver.page_source}")
|
||||
|
||||
return driver.page_source
|
||||
|
||||
async def _arun(
|
||||
self, url: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
|
||||
) -> str:
|
||||
"""Use the tool asynchronously."""
|
||||
raise NotImplementedError("custom_search does not support async")
|
||||
@@ -5,10 +5,10 @@ from webdriver_manager.microsoft import EdgeChromiumDriverManager
|
||||
from selenium.webdriver.edge.service import Service as EdgeService
|
||||
from selenium.webdriver.chrome.service import Service as ChromeService
|
||||
|
||||
from tools.tools_base import ToolsBase
|
||||
from tools.tool_base import ToolBase
|
||||
|
||||
|
||||
class WebBrowserUrl(ToolsBase):
|
||||
class WebBrowserUrl(ToolBase):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.name = "web_browser"
|
||||
|
||||
15
utils/llm_util.py
Normal file
15
utils/llm_util.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from utils.config import Config
|
||||
|
||||
|
||||
class LLMUtil:
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.config = Config()
|
||||
self.api_key = self.config.OPEN_AI.get("api_key")
|
||||
self.base_url = self.config.OPEN_AI.get("api_url")
|
||||
|
||||
def llm(self):
|
||||
return ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", openai_api_key=self.api_key,
|
||||
openai_api_base=self.base_url)
|
||||
Reference in New Issue
Block a user