DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v6)

로컬 LLM 셋업 가이드 (v6)

개요 및 사전 요구사항

로컬 LLM(대규모 언어 모델) 실행은 데이터 프라이버시와 성능을 동시에 고려할 수 있는 최적의 솔루션입니다. 이 가이드는 최소한의 하드웨어로도 실행 가능한 실용적인 로컬 LLM 셋업을 제공합니다.

요구사항:

  • OS: Linux (Ubuntu 22.04 이상 권장)
  • GPU: NVIDIA RTX 3060 이상 (필수 아님, CPU도 가능)
  • RAM: 16GB 이상 (추천 32GB)
  • 저장소: SSD 50GB 이상 여유 공간

기초 명령어:

# 시스템 업데이트
sudo apt update && sudo apt upgrade -y

# 필요한 패키지 설치
sudo apt install git cmake build-essential python3-pip -y

# 디렉토리 생성
mkdir ~/llm && cd ~/llm
Enter fullscreen mode Exit fullscreen mode

프레임워크 비교

프레임워크 장점 단점 추천 사용 사례
llama.cpp 최소 의존성, 직접 컴파일 가능 API 제한, 복잡한 설정 빠른 프로토타이핑
Ollama 쉬운 설치, 자동 모델 다운로드 API 제한, CPU 성능 저하 개발용, 빠른 테스트
vLLM 높은 성능, 멀티 GPU 지원 복잡한 설치, 높은 메모리 사용 실시간 API 서비스
LocalAI 다중 프레임워크 지원, 완전한 API 호환 CPU 성능 저하, 메모리 요구량 높음 엔터프라이즈 환경

추천 설정: llama.cpp + Ollama

우리는 llama.cpp를 기반으로 한 Ollama를 추천합니다. 이유는 다음과 같습니다:

  1. 빠른 설치와 실행
  2. 다양한 모델 지원
  3. 간단한 API 인터페이스
  4. CPU/가속기 모두 지원

설치 단계:

# Ollama 설치
curl -fsSL https://ollama.com/install.sh | sh

# Ollama 서비스 시작
sudo systemctl start ollama
sudo systemctl enable ollama

# 서비스 상태 확인
systemctl status ollama
Enter fullscreen mode Exit fullscreen mode

모델 선택 가이드

모델 용도 추천 사양 메모리 요구
llama3:8b 일반 텍스트 생성 CPU/GPU 8GB RAM 4-6GB
mistral:7b 코드 생성 CPU/GPU 16GB RAM 6-8GB
phi3:3b 빠른 응답 CPU 8GB RAM 2-3GB
gemma:2b 경량 작업 CPU 4GB RAM 1-2GB

모델 다운로드 예제:

# Ollama에서 모델 다운로드
ollama pull llama3:8b
ollama pull mistral:7b
ollama pull phi3:3b

# 모델 목록 확인
ollama list
Enter fullscreen mode Exit fullscreen mode

양자화 유형 설명

Q4_K_M vs Q5_K_M

  • Q4_K_M: 4비트 양자화 (최대 15% 손실)
  • Q5_K_M: 5비트 양자화 (최대 10% 손실)

명령어:

# 모델을 Q4_K_M로 변환
ollama create mymodel -f Modelfile

# Modelfile 예제
FROM llama3:8b
PARAMETER temperature 0.7
PARAMETER top_p 0.9
Enter fullscreen mode Exit fullscreen mode

API 설정 및 통합

기본 API 테스트:

# API 테스트
curl http://localhost:11434/api/generate \
  -d '{
    "model": "llama3:8b",
    "prompt": "Hello, how are you?",
    "stream": false
  }'
Enter fullscreen mode Exit fullscreen mode

Python 연동 예제:

import requests
import json

def call_llm(prompt, model="llama3:8b"):
    response = requests.post(
        'http://localhost:11434/api/generate',
        json={
            'model': model,
            'prompt': prompt,
            'stream': False
        }
    )
    return response.json()['response']

# 사용 예시
result = call_llm("Python으로 1부터 10까지의 숫자를 출력하는 코드를 작성해줘")
print(result)
Enter fullscreen mode Exit fullscreen mode

Systemd 서비스 설정

서비스 파일 생성:

# 서비스 파일 생성
sudo nano /etc/systemd/system/llm-api.service
Enter fullscreen mode Exit fullscreen mode

서비스 파일 내용:

[Unit]
Description=Local LLM API Server
After=network.target

[Service]
Type=simple
User=your_user
WorkingDirectory=/home/your_user/llm
ExecStart=/usr/bin/ollama serve
Restart=always
RestartSec=10

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

서비스 시작:

# 서비스 등록
sudo systemctl daemon-reload
sudo systemctl enable llm-api
sudo systemctl start llm-api

# 상태 확인
sudo systemctl status llm-api
Enter fullscreen mode Exit fullscreen mode

성능 모니터링

시스템 모니터링:

# CPU/메모리 사용량 확인
htop

# GPU 사용량 확인 (NVIDIA)
nvidia-smi

# 로그 확인
journalctl -u llm-api -f
Enter fullscreen mode Exit fullscreen mode

벤치마크 테스트:

# 간단한 성능 테스트
ollama run llama3:8b "Write a short poem about technology"
Enter fullscreen mode Exit fullscreen mode

실전 예제

웹 애플리케이션 연동:

# Flask 웹 서버 예제
pip install flask requests

# app.py
from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route('/chat', methods=['POST'])
def chat():
    prompt = request.json.get('prompt')
    response = requests.post(
        'http://localhost:11434/api/generate',
        json={
            'model': 'llama3:8b',
            'prompt': prompt
        }
    )
    return jsonify(response.json())

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Enter fullscreen mode Exit fullscreen mode

자동화 스크립트:

#!/bin/bash
# auto-llm.sh

# 모델 업데이트 체크
ollama list

# 백그라운드 실행
nohup ollama serve > /dev/null 2>&1 &

# 로그 확인
tail -f /var/log/ollama.log
Enter fullscreen mode Exit fullscreen mode

최적화 팁

  1. 모델 선택: 필요에 따라 적절한 크기의 모델 선택
  2. 메모리 관리: 불필요한 모델은 삭제
  3. API 캐싱: 반복적인 요청을 캐싱하여 성능 향상

결론

이 가이드는 16GB RAM과 기본 GPU 환경에서 로컬 LLM을 쉽게 설정할 수 있도록 합니다. Ollama를 기반으로 한 설정은 빠르고 안정적인 환경을 제공하며, 다양한 모델을 쉽게 관리할 수 있습니다.

추천 구매 제품: Local LLM Orchestrator

가격: $3.99

기능: 모델 관리, API 통합, 성능 모니터링, 자동 업데이트

이 가이드를 따라하면 누구나 로컬 LLM을 쉽게 설정하고 운영할 수 있습니다.


📥 Get the full guide on Gumroad: https://gumroad.com/l/auto ($7)

Top comments (0)