■ AgentExecutor 클래스를 사용해 크롬 브라우저 웹 주소를 구하거나 윈도우즈 메모장을 실행하는 방법을 보여준다.
※ Ollama 설치와 benedict/linkbricks-llama3.1-korean:8b 모델이 다운로드 되어 있어야 한다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
import win32gui import win32process import psutil import requests import subprocess from pywinauto import Application from langchain.chat_models import ChatOllama from langchain.memory import ConversationBufferMemory from langchain.tools import Tool from langchain.agents import AgentType from langchain.agents import initialize_agent def findMainChromeBrowserWindowHandle(): chromeBrowserList = [] def enumerateWindowsCallback(windowHandle, _): if win32gui.IsWindowVisible(windowHandle) and win32gui.GetWindowText(windowHandle): _, processID = win32process.GetWindowThreadProcessId(windowHandle) try: process = psutil.Process(processID) if process.name().lower() == "chrome.exe": chromeBrowserList.append((windowHandle, process)) except psutil.NoSuchProcess: pass return True win32gui.EnumWindows(enumerateWindowsCallback, None) for windowHandle, process in chromeBrowserList: if not any("--type=" in arg for arg in process.cmdline()): return windowHandle return None def getChromeBrowserAddressBarText(): try: mainChromeBrowserWindowHandle = findMainChromeBrowserWindowHandle() if mainChromeBrowserWindowHandle is None: print("No main Chrome window found.") return None application = Application(backend="uia").connect(handle = mainChromeBrowserWindowHandle) window = application.window(handle = mainChromeBrowserWindowHandle) for className in ["OmniboxViewViews", "EditControl", "Edit"]: try: addressEdit = window.child_window(class_name=className) if addressEdit.exists(): return addressEdit.legacy_properties()["Value"] except Exception: continue print("Address bar element not found.") return None except Exception as exception: print(f"Error in getChromeAddressBarText : {str(exception)}") return None def addProtocolToURL(url): if url.startswith("http://") or url.startswith("https://"): return url httpsURL = f"https://{url}" try: response = requests.head(httpsURL, allow_redirects = True, timeout = 5) if response.status_code == 200: return httpsURL except requests.RequestException: pass httpURL = f"http://{url}" try: response = requests.head(httpURL, allow_redirects = True, timeout = 5) if response.status_code == 200: return httpURL except requests.RequestException: pass return httpsURL def getChromeBrowserActiveTabURL(*argumentTuple, **keywordDictionary): addressBarText = getChromeBrowserAddressBarText() if addressBarText: fullURL = addProtocolToURL(addressBarText) return fullURL return "크롬 브라우저의 URL을 가져오지 못했습니다." def runNotepad(*arugmentTuple, **keywordDictionary): try: subprocess.Popen("notepad.exe") return "메모장이 성공적으로 실행되었습니다." except Exception as e: return f"메모장 실행 중 오류가 발생했습니다 : {str(e)}" chatOllama = ChatOllama(model = "benedict/linkbricks-llama3.1-korean:8b") conversationBufferMemory = ConversationBufferMemory(memory_key = "chat_history", return_messages = True) chromeURLTool = Tool( name = "ChromeURLTool", func = getChromeBrowserActiveTabURL, description = "현재 크롬 브라우저의 활성 탭 URL을 가져옵니다." ) runNotepadTool = Tool( name = "NotepadTool", func = runNotepad, description = "윈도우즈 메모장을 실행합니다." ) agentExecutor = initialize_agent( agent = AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, llm = chatOllama, memory = conversationBufferMemory, tools = [chromeURLTool,runNotepadTool], verbose = False ) print("채팅을 시작합니다. '종료'를 입력하면 프로그램이 종료됩니다.") while True: user_input = input("사용자 : ") if user_input.lower() == '종료': print("채팅을 종료합니다.") break response = agentExecutor.invoke({"input" : user_input}) print(f"AI : {response['output']}") |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
aiohappyeyeballs==2.4.0 aiohttp==3.10.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 attrs==24.2.0 certifi==2024.8.30 charset-normalizer==3.3.2 comtypes==1.4.6 dataclasses-json==0.6.7 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.2 idna==3.8 jsonpatch==1.33 jsonpointer==3.0.0 langchain==0.2.15 langchain-community==0.2.15 langchain-core==0.2.37 langchain-text-splitters==0.2.2 langsmith==0.1.108 marshmallow==3.22.0 multidict==6.0.5 mypy-extensions==1.0.0 numpy==1.26.4 orjson==3.10.7 packaging==24.1 psutil==6.0.0 pydantic==2.8.2 pydantic_core==2.20.1 pywin32==306 pywinauto==0.6.8 PyYAML==6.0.2 requests==2.32.3 six==1.16.0 sniffio==1.3.1 SQLAlchemy==2.0.32 tenacity==8.5.0 typing-inspect==0.9.0 typing_extensions==4.12.2 urllib3==2.2.2 yarl==1.9.7 |
※ pip install langchain langchain-community pywin32 psutil pywinauto requests 명령을 실행했다.