레이블이 llmfit인 게시물을 표시합니다. 모든 게시물 표시
레이블이 llmfit인 게시물을 표시합니다. 모든 게시물 표시

local llm with claude code and opencode

어떤 로컬 llmfit(https://github.com/AlexsJones/llmfit) 을 사용하면 내 하드웨어에 맞는 최고의 모델이 무엇인지알 수 있다.
llmfit 버전을 설치해서 실행해야 최신 모델이 보인다.
uv tool upgrade llmfit
또는
brew upgrade llmfit

Inst 컬럼은 설치 가능 여부 표시다.
O ollama 로 다운로드 가능
L llama.cpp 로 다운로드 가능
S LM Studio 로 다운로드 가능
- 다운로드 가능한 프로바이더 찾지 못함
✓ 로컬에 설치되어 있음

내 시스템 스펙(cpu,gpu,mem) 조회
llmfit system

llmfit 에서도 perfect 라고 나와도 실제 내가 사용중인 프로그램/환경등으로 인해 리소스가 부족해 안뜰 수 있다.
MLX 모델은 ollama 에서 찾을 수 없어 qwen3-coder 로 사용해보자.
ollama pull qwen3-coder:latest
ollama ls
ollama run qwen3-coder
ollama ps

ollama는 OpenAI 호환 API (/v1/chat/completions 등)를 제공한다.    
opencode.json > provider > ollama 에 다음과 같이 설정한다.
{
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama (local)",
      "options": {
        "baseURL": "http://localhost:11434/v1"
      },
      "models": {
        "qwen3-coder:latest": {
          "name": "Qwen3 Coder 30.5B"
        },
        "llama3.2:latest": {
          "name": "Llama 3.2 3B"
        }
      }
    }
  }
}

opencode 에서 로컬 llm 사용 결과

claude code 에서는 openAI api 호환이 되지 않아 중간에 litellm(proxy) 서버를 둬야 한다.
litellm[proxy] 로 기본 + 프록시 실행에 필요한 추가 패키지들(backoff, uvicorn, fastapi 등)을 함께 설치해야 한다.
uv pip install 'litellm[proxy]' --system

litellm 프록시 서버 구동
litellm --model qwen-coder3 --api_base http://localhost:11434/v1 --port 8001

이제 다음 환경변수를 .local_llm_env_for_claude_code 등의 파일로 저장하자.

# 로컬 llm URL
export ANTHROPIC_BASE_URL="http://localhost:8001"

# 더미값으로 설정하면 된다.(sk:SecretKey)
export ANTHROPIC_API_KEY="sk-dummy-key"

# Claude Code의 외부 통신을 최소화하기 위해 다음 환경변수도 설정하자.
# 텔레메트리(사용 통계 수집)를 비활성화
export CLAUDE_CODE_ENABLE_TELEMETRY=0

# 핵심 API 호출 외의 불필요한 네트워크 트래픽 차단
export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1

# API 요청 시 Claude Code가 보내는 attribution 헤더(어떤 클라이언트에서 요청했는지 식별하는 정보)를 비활성화
export CLAUDE_CODE_ATTRIBUTION_HEADER=0

이제 다음과 같이 실행한다.
source .local_llm_env_for_claude_code; claude --model qwen3-coder

실행 후 api key 사용을 선택

왼쪽: 기본 opus 모델을 사용한 경우
오른쪽: 로컬 llm(qwen3-coder) 사용한 경우

#####

사용해보니 ollama M1 맥북에서 느리다.
Apple Silicon 전용 머신러닝 프레임워크 MLX(https://github.com/ml-explore/mlx)를 사용하자.
위 llmfit 나온것도 MLX 모델이다.

# 모델은 huggingface 에서 다운받는다.
# 참고로 모델은 ~/.cache/huggingface/hub 에 모델이 있다.
# huggingface_hub(hf)툴 설치
uv tool install huggingface_hub

# 캐시(모델) 목록 조회(모델명이 길면 뒤가 ...로 보인다.)
hf cache list

# 모델명 전체 출력으로 조회
hf cache list --format agent

# 모델 삭제시
hf cache rm 모델ID

# 모델 다운로드
# hugging face에 올라온 대다수의 모델은 공개 상태이므로 huggingface-cli login 단계를 거치지 않고 바로 다운로드 명령어를 실행하면 된다.
hf download 모델ID

# mlx-lm 설치
pip install mlx-lm
# 또는
uv tool install mlx-lm

# mlx-lm 실행
mlx_lm.server \
    --model lmstudio-community/Qwen2.5-Coder-7B-Instruct-MLX-4bit \
    --port 8080

# 모델명을 확인한다.
http://localhost:8080/v1/models

# ~/.config/opencode/opencode.json 에 다음 모델 설정을 추가한다.
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "mlx-local": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "MLX Local",
      "options": {
        "baseURL": "http://localhost:1234/v1"
      },
      "models": {
        "mlx-community/Qwen3-8B-4bit": {
          "name": "Qwen3 8B (4bit)"
        },
        "lmstudio-community/Qwen3-Coder-30B-A3B-Instruct-MLX-4bit": {
          "name": "Qwen3-Coder-30B-A3B-Instruct-MLX-4bit"
        },
        "lmstudio-community/Qwen2.5-Coder-7B-Instruct-MLX-4bit": {
          "name": "Qwen2.5-Coder-7B-Instruct-MLX-4bit"
        }
      }
    }
  }
}

이제 opencode 를 열고 모델을 선택해 사용하면 된다.

모델 테스트 결과
Qwen3-Coder-30B-A3B-Instruct-MLX-4bit
큰 모델은 맥북 M1 MAX(32G RAM)에서 메모리 사용율이 100% 가 되면서 모자란 메모리에 대해 스왑이 발생해 CPU 100%가 되고 정작 GPU 놀고 맥북은 버벅거리는 경우가 발생했다.

Qwen2.5-Coder-7B-Instruct-MLX-4bit
tool_call(LLM 은 텍스트만 생성, 파일을 쓰거나 명령을 실행하는 건 못 하는데 그 간극을 메우는 약속)지원 안된다.

Prev