함수 생성
import subprocess
def new_window():
try:
subprocess.Popen([r"C:\Users\leegy\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd", "--new-window"])
return {"status": "VS Code opened in new window"}
except Exception as e:
return {"status": "error", "message": str(e)}
def close_window():
try:
subprocess.run("taskkill /IM Code.exe /F", shell=True) # Code.exe 프로세스를 강제로 종료
return {"status": "All VS Code windows closed"}
except Exception as e:
return {"status": "error", "message": str(e)}
우선 간단하게 VS Code의 새로운 창을 열고 닫는 코드를 작성했습니다.
LangChain 툴 생성
from langchain.agents import Tool
from mcp_tool.vs_code import *
vs_code_tools = [
Tool(
name="new_window",
func=new_window,
description="Open a new VS Code window"
),
Tool(
name="close_window",
func=close_window,
description="Close all VS Code windows"
),
]
만들어둔 함수들을 LangChain 에이전트가 이해하고 사용할 수 있는 툴로 만듭니다.
에이전트 생성
from langchain.agents import initialize_agent, AgentType
from langchain_community.chat_models import ChatOpenAI
from langchain_service.tools.vscode_tools import *
from core.config import EMBEDDING_API
tools = vs_code_tools
llm = ChatOpenAI(model="gpt-4", temperature=0.7, openai_api_key=EMBEDDING_API)
agent = initialize_agent(tools, llm, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
def handle_user_command():
while True:
user_input = input("Enter your command (or type 'exit' to quit): ")
if user_input.lower() == "exit":
print("Exiting the agent...")
break
# 에이전트로 명령을 전달하고 응답 받기
response = agent.run(user_input)
print("Agent response:", response)
# 실시간 명령 처리 시작
if __name__ == "__main__":
handle_user_command()