터미널 AI 에이전트 구축 (v21)
터미널에서 작동하는 AI 에이전트를 구축하여 코드 작성과 리팩토링을 자동화하는 것은 현대 개발 워크플로우의 핵심입니다. 이 가이드는 실제 개발자가 사용할 수 있는, 저렴하고 효율적인 터미널 AI 에이전트 구축 방법을 다룹니다.
1. CLI AI 에이전트 생태계
현재 터미널 AI 에이전트 시장은 다음과 같은 주요 플랫폼으로 구성되어 있습니다:
Aider: GitHub Copilot과 유사한 기능으로, 코드 생성과 수정을 지원합니다.
pip install aider
aider --help
Continue.dev: VSCode 확장 프로그램이지만 터미널에서도 사용 가능합니다.
npm install -g continue
continue --help
OpenCode: OpenAI API 기반의 커스텀 에이전트입니다.
pip install openai
커스텀 스크립트: 직접 구축한 에이전트는 높은 유연성을 제공합니다.
2. 로컬 LLM API 엔드포인트 설정
로컬 LLM을 사용하여 API 엔드포인트를 설정하는 방법입니다:
# 1. llama.cpp 설치
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make
# 2. 모델 다운로드 및 변환
wget https://huggingface.co/TheBloke/Qwen-7B-Chat-GGUF/resolve/main/qwen-7b-chat.Q4_K_M.gguf
./convert-hf-to-ggml.py models/Qwen-7B-Chat/ 1
./quantize models/Qwen-7B-Chat/ggml-model-f16.bin qwen-7b-chat.Q4_K_M.gguf 4
# 3. 로컬 서버 실행
./server -m qwen-7b-chat.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
3. 간단한 Python CLI 에이전트 구축
다음은 기능 호출을 지원하는 간단한 Python 에이전트입니다:
# aider_agent.py
import subprocess
import json
import requests
import sys
class TerminalAgent:
def __init__(self, api_url="http://localhost:8080"):
self.api_url = api_url
def execute_command(self, command):
"""명령어 실행"""
try:
result = subprocess.run(
command,
shell=True,
capture_output=True,
text=True,
timeout=30
)
return {
"success": result.returncode == 0,
"output": result.stdout,
"error": result.stderr
}
except subprocess.TimeoutExpired:
return {"success": False, "error": "Command timed out"}
def chat_with_ai(self, prompt):
"""AI와 대화"""
payload = {
"prompt": prompt,
"temperature": 0.7,
"max_tokens": 512
}
try:
response = requests.post(
f"{self.api_url}/completion",
json=payload,
timeout=30
)
return response.json()
except requests.exceptions.RequestException as e:
return {"error": str(e)}
# 사용 예시
if __name__ == "__main__":
agent = TerminalAgent()
if len(sys.argv) > 1:
command = " ".join(sys.argv[1:])
if command.startswith("run:"):
result = agent.execute_command(command[4:])
print(json.dumps(result, indent=2))
else:
response = agent.chat_with_ai(command)
print(json.dumps(response, indent=2))
4. tmux와 통합
터미널 분할 환경에서 에이전트를 활용하려면 tmux와 통합해야 합니다:
# tmux 세션 생성
tmux new-session -d -s ai_agent
# 에이전트 실행 (백그라운드)
tmux send-keys -t ai_agent "python3 aider_agent.py" Enter
# 세션 정보 확인
tmux ls
사용자 정의 스크립트로 tmux 컨트롤:
# tmux_helper.py
import subprocess
import json
def create_ai_session():
"""AI 세션 생성"""
subprocess.run(["tmux", "new-session", "-d", "-s", "ai_agent"])
def send_to_session(session, command):
"""세션에 명령어 전송"""
subprocess.run(["tmux", "send-keys", "-t", session, command, "Enter"])
def get_session_output(session):
"""세션 출력 가져오기"""
result = subprocess.run(
["tmux", "capture-pane", "-p", "-t", session],
capture_output=True,
text=True
)
return result.stdout
# 사용 예시
create_ai_session()
send_to_session("ai_agent", "python3 aider_agent.py '리팩토링해줘'")
5. 사용자 정의 도구 개발
코드 검색 도구
# code_search.py
import os
import subprocess
import re
class CodeSearcher:
def __init__(self, project_root="."):
self.project_root = project_root
def search_function(self, function_name):
"""함수 검색"""
cmd = f"grep -r '{function_name}' --include='*.py' {self.project_root}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout
def search_pattern(self, pattern):
"""정규 표현식으로 검색"""
cmd = f"grep -r '{pattern}' --include='*.py' {self.project_root}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout
# 사용 예시
searcher = CodeSearcher("/path/to/project")
functions = searcher.search_function("calculate_total")
Git 도구
# git_tools.py
import subprocess
import json
class GitTools:
def __init__(self, repo_path="."):
self.repo_path = repo_path
def get_changes(self):
"""변경사항 가져오기"""
cmd = "git diff --name-only"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout.splitlines()
def get_status(self):
"""상태 정보"""
cmd = "git status --porcelain"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return result.stdout
# 사용 예시
git_tools = GitTools("/path/to/repo")
changes = git_tools.get_changes()
6. 컨텍스트 윈도우 관리
대규모 코드베이스의 컨텍스트 윈도우를 관리하기 위한 전략:
# context_manager.py
import os
import hashlib
from typing import List, Dict
class ContextManager:
def __init__(self, max_context_size=4096):
self.max_context_size = max_context_size
self.context_cache = {}
def add_file_context(self, file_path: str, content: str):
"""파일 컨텍스트 추가"""
file_hash = hashlib.md5(file_path.encode()).hexdigest()
self.context_cache[file_hash] = {
"path": file_path,
"content": content,
"size": len(content)
}
def get_context_summary(self, file_paths: List[str]) -> Dict:
"""컨텍스트 요약"""
summary = {}
total_size = 0
for path in file_paths:
if path in self.context_cache:
file_info = self.context_cache[path]
summary[path] = {
"size": file_info["size"],
"content_preview": file_info["content"][:100] + "..."
}
total_size += file_info["size"]
return {
"files": summary,
"total_size": total_size,
"over_limit": total_size > self.max_context_size
}
# 사용 예시
context = ContextManager(2048)
context.add_file_context("main.py", "def hello():\n print('Hello')")
summary = context.get_context_summary(["main.py"])
7. 비용/속도 최적화
로컬 vs API 모델의 성능 비교:
python
# performance_optimizer.py
import time
import subprocess
import json
class ModelOptimizer:
def __init__(self):
self.local_models = {
"qwen7b": "qwen-7b-chat.Q4_K_M.gguf",
"gemma7b": "gemma-7b-it.Q4_K_M.gguf"
}
def benchmark_model(self, model_name, prompt):
"""모델 성능 벤치마크"""
# 로컬 모델 테스트
start_time = time.time()
---
📥 **Get the full guide on Gumroad**: https://gumroad.com/l/auto ($5)
Top comments (0)