DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v13)

로컬 LLM 셋업 가이드 (v13)

1. 개요 및 전제 조건

로컬 LLM (대형 언어 모델)을 실행하려면 적절한 하드웨어와 소프트웨어 환경이 필요합니다. 이 가이드는 최소한의 리소스로도 작동할 수 있도록 최적화된 설정을 제공합니다.

하드웨어 요구사항:

  • GPU: NVIDIA RTX 3060 이상 (필수)
  • RAM: 16GB 이상 (권장 32GB)
  • 저장소: SSD 50GB 이상 여유 공간

소프트웨어 요구사항:

# Ubuntu 22.04 LTS 기준
sudo apt update
sudo apt install build-essential git python3-pip curl wget
Enter fullscreen mode Exit fullscreen mode

CPU 아키텍처 확인:

lscpu | grep -E "(Architecture|Model name)"
Enter fullscreen mode Exit fullscreen mode

2. 프레임워크 비교

프레임워크 장점 단점 추천 사용 사례
llama.cpp 최소 의존성, 높은 성능, C++ 기반 복잡한 설정 필요 고성능, 경량화 필요
Ollama 쉬운 설치, GUI 지원, 모델 관리 메모리 사용량 많음 개발 테스트, 빠른 프로토타이핑
vLLM 높은 추론 속도, 스케일링 가능 복잡한 설치, GPU 중심 대규모 프로덕션 환경
LocalAI REST API, 다양한 백엔드 지원 최적화 부족 API 기반 애플리케이션 통합

선택 기준: 최소 리소스 + 최대 성능 = llama.cpp + Ollama 조합

3. 추천 설정 설치 절차

3.1 llama.cpp 설치:

cd ~
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make
Enter fullscreen mode Exit fullscreen mode

3.2 모델 다운로드:

# HuggingFace에서 모델 다운로드
mkdir -p models
cd models
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

3.3 Ollama 설치 (옵션):

curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama2:7b
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

4.1 일반 텍스트 생성:

  • Llama-2-7B-Chat-GGUF: 7B 파라미터, 최적화된 채팅 모델
  • Mistral-7B-v0.1-GGUF: 더 높은 성능, 일반 텍스트 생성용

4.2 코드 생성:

  • CodeLlama-7b-Python: 파이썬 코드 생성에 특화
  • StarCoder2-15B: 다국어 코드 생성

4.3 요약 및 번역:

  • Phi-3-mini: 빠른 추론 속도, 작은 모델
  • Mixtral-8x7B: 높은 정확도, 메모리 요구량 높음

5. 양자화 유형 설명

5.1 Q4_K_M (권장):

# 예시 명령어
./main -m models/llama-2-7b-chat.Q4_K_M.gguf --prompt "Hello, world!" -n 128
Enter fullscreen mode Exit fullscreen mode
  • Q4_K_M: 4비트 양자화, 최적화된 메모리 사용량
  • 장점: 3~4GB RAM 사용, 빠른 추론 속도
  • 단점: 정밀도 약간 저하

5.2 Q5_K_M:

# 더 높은 정밀도
./main -m models/llama-2-7b-chat.Q5_K_M.gguf --prompt "Hello, world!" -n 128
Enter fullscreen mode Exit fullscreen mode

5.3 FP16:

# 최고 정밀도, 메모리 요구량 많음
./main -m models/llama-2-7b-chat-fp16.gguf --prompt "Hello, world!" -n 128
Enter fullscreen mode Exit fullscreen mode

6. API 설정 및 기존 도구 통합

6.1 llama.cpp API 서버:

# REST API 서버 시작
./server -m models/llama-2-7b-chat.Q4_K_M.gguf --port 8080 --host 0.0.0.0
Enter fullscreen mode Exit fullscreen mode

6.2 Python API 클라이언트 예제:

import requests

def query_llm(prompt):
    response = requests.post(
        "http://localhost:8080/completion",
        json={
            "prompt": prompt,
            "n_predict": 128,
            "temperature": 0.7
        }
    )
    return response.json()['content']

# 사용 예
result = query_llm("Python에서 리스트를 정렬하는 방법은?")
print(result)
Enter fullscreen mode Exit fullscreen mode

6.3 VS Code 연동:

// .vscode/settings.json
{
    "llm.server.url": "http://localhost:8080",
    "llm.model": "llama-2-7b-chat.Q4_K_M.gguf"
}
Enter fullscreen mode Exit fullscreen mode

7. Systemd 서비스 설정 (24/7 운영)

7.1 서비스 파일 생성:

sudo nano /etc/systemd/system/llm.service
Enter fullscreen mode Exit fullscreen mode
[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/llama.cpp
ExecStart=/home/ubuntu/llama.cpp/server -m /home/ubuntu/models/llama-2-7b-chat.Q4_K_M.gguf --port 8080 --host 0.0.0.0
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

7.2 서비스 시작:

sudo systemctl daemon-reload
sudo systemctl enable llm.service
sudo systemctl start llm.service
sudo systemctl status llm.service
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 튜닝

8.1 메모리 사용량 모니터링:

# 실시간 메모리 모니터링
watch -n 1 nvidia-smi

# CPU 사용량
htop
Enter fullscreen mode Exit fullscreen mode

8.2 성능 테스트 명령어:

# 추론 성능 테스트
./main -m models/llama-2-7b-chat.Q4_K_M.gguf --prompt "This is a test prompt." -n 100 --timing

# 병렬 처리 테스트
for i in {1..5}; do ./main -m models/llama-2-7b-chat.Q4_K_M.gguf --prompt "Test $i" -n 50 & done
Enter fullscreen mode Exit fullscreen mode

8.3 최적화 파라미터:

# 추론 최적화 옵션
./main -m models/llama-2-7b-chat.Q4_K_M.gguf \
  --prompt "Hello, world!" \
  -n 128 \
  --temperature 0.7 \
  --top-p 0.9 \
  --repeat-penalty 1.1 \
  --batch-size 512
Enter fullscreen mode Exit fullscreen mode

9. 실제 사용 예제

9.1 간단한 채팅 애플리케이션:

import requests
import json

class LocalLLMChat:
    def __init__(self, base_url="http://localhost:8080"):
        self.base_url = base_url

    def chat(self, message):
        response = requests.post(
            f"{self.base_url}/completion",
            json={
                "prompt": f"Q: {message}\nA:",
                "n_predict": 100,
                "temperature": 0.7,
                "stop": ["Q:"]
            }
        )
        return response.json()['content']

# 사용법
chatbot = LocalLLMChat()
print(chatbot.chat("안녕하세요!"))
Enter fullscreen mode Exit fullscreen mode

9.2 성능 비교 테스트:


bash
# Q4_K_M vs Q5_K_M
echo "Q4_K_M 성능 테스트:" && time ./main -m models/llama-2-7b-chat.Q4_K_M.gguf --prompt "Test" -n 50
echo "

---

📥 **Get the full guide on Gumroad**: https://gumroad.com/l/auto ($7)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)