프라이버시 우선: Llama-3와 MLX를 사용해 Mac에서 로컬로 의료 보고서와 채팅하기 🍎
Source: Dev.to
Introduction
Your health data is probably the most sensitive information you own. Yet, in the age of AI, most people blindly upload their blood work and MRI results to cloud‑based LLMs just to get a summary. Stop right there! 🛑
In this tutorial, we are going to build a Local RAG (Retrieval‑Augmented Generation) system. We will leverage the power of Apple Silicon’s unified memory, the high‑performance MLX framework, and Llama‑3 to create a private medical assistant that never leaks a single byte to the internet. By using Local RAG and MLX‑optimized Llama‑3, you can perform complex semantic search and data extraction on your medical PDFs while keeping your data strictly on‑device.
아키텍처: 왜 MLX인가?
전통적인 RAG 스택은 종종 무거운 Docker 컨테이너나 클라우드 API에 의존합니다. 하지만 Mac (M1/M2/M3) 사용자는 MLX 프레임워크(Apple Machine Learning Research에서 개발)를 사용하면 GPU와 통합 메모리 아키텍처를 활용해 Llama‑3를 놀라운 효율로 실행할 수 있습니다.
다음은 오래된 PDF 보고서에서 의미 있는 대화로 데이터가 흐르는 과정입니다:
graph TD
A[Medical PDF Report] -->|PyMuPDF| B(Text Extraction & Cleaning)
B --> C{Chunking Strategy}
C -->|Sentence Splitting| D[ChromaDB Vector Store]
E[User Query: 'Is my cholesterol high?'] -->|MLX Embedding| F(Vector Search)
D -->|Retrieve Relevant Context| G[Prompt Augmentation]
G -->|Context + Query| H[Llama-3-8B via MLX]
H --> I[Private Local Answer]
style H fill:#f96,stroke:#333,stroke-width:2px
style D fill:#bbf,stroke:#333,stroke-width:2px
사전 요구 사항
코드에 들어가기 전에 Apple Silicon Mac과 다음 스택이 설치되어 있는지 확인하세요:
- Llama‑3‑8B – 속도를 위한 4‑bit 양자화 버전.
- MLX – Apple의 네이티브 배열 프레임워크.
- ChromaDB – 가벼운 벡터 데이터베이스.
- PyMuPDF (fitz) – 고정밀 PDF 파싱.
pip install mlx-lm chromadb pymupdf sentence-transformers
1단계: PyMuPDF를 사용한 민감한 PDF 파싱
의료 보고서는 표, 서명, 특이한 형식 등으로 매우 복잡합니다. 우리는 PyMuPDF의 속도와 신뢰성을 활용해 깨끗한 텍스트를 추출합니다.
import fitz # PyMuPDF
def extract_medical_text(pdf_path):
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text("text") + "\n"
# Simple cleaning: remove extra whitespaces
clean_text = " ".join(text.split())
return clean_text
# Usage
raw_data = extract_medical_text("my_blood_report_2024.pdf")
print(f"Extracted {len(raw_data)} characters.")
Step 2: 벡터 임베딩 및 로컬 스토리지
관련 정보를 찾기 위해(예: “내 혈당 수치는 얼마였나요?”) 텍스트를 벡터로 변환하고 ChromaDB에 저장합니다.
💡 Pro‑Tip: 보다 프로덕션에 적합한 예제와 고급 RAG 패턴을 원한다면, WellAlly Tech Blog에서 자세한 가이드를 확인하세요. 여기서 로컬 추론 최적화에 대해 깊이 있게 다룹니다.
import chromadb
from chromadb.utils import embedding_functions
# Initialize local ChromaDB
client = chromadb.PersistentClient(path="./medical_db")
# Use a local embedding model
emb_fn = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="all-MiniLM-L6-v2"
)
collection = client.get_or_create_collection(
name="medical_reports",
embedding_function=emb_fn,
)
def add_to_vector_store(text, metadata):
# Chunking text into 500‑character pieces
chunks = [text[i:i+500] for i in range(0, len(text), 500)]
ids = [f"id_{i}" for i in range(len(chunks))]
collection.add(
documents=chunks,
ids=ids,
metadatas=[metadata] * len(chunks)
)
add_to_vector_store(raw_data, {"source": "annual_checkup_2024"})
단계 3: Llama‑3 및 MLX를 사용한 로컬 추론
이제 마법을 보여줄 차례입니다. 우리는 mlx‑lm을 사용해 양자화된 Llama‑3‑8B를 로드합니다. 이를 통해 모델을 16 GB RAM을 가진 MacBook Air에서도 편하게 실행할 수 있습니다. 🚀
from mlx_lm import load, generate
# Load the model and tokenizer
model, tokenizer = load("mlx-community/Meta-Llama-3-8B-Instruct-4bit")
def query_private_ai(user_question):
# 1. Retrieve context from ChromaDB
results = collection.query(query_texts=[user_question], n_results=3)
context = "\n".join(results["documents"][0])
# 2. Construct the prompt
prompt = f"""
You are a private medical assistant. Use the provided medical report context to answer the user's question.
If you don't know the answer based on the context, say so.
Context: {context}
---
Question: {user_question}
Answer:
"""
# 3. Generate response using MLX
response = generate(
model,
tokenizer,
prompt=prompt,
verbose=False,
max_tokens=500,
)
return response
# Example Query
print(query_private_ai("What are the key concerns in my blood report?"))
더 나아가기: “공식” 방법
이 스크립트가 시작점이 되지만, 프로덕션 수준의 의료 AI를 구축하려면 멀티모달 데이터(예: X‑레이)를 처리하고 로컬 엣지 디바이스에서도 엄격한 HIPAA와 유사한 규정을 준수해야 합니다.
팀 WellAlly는 “프라이버시‑우선 AI” 아키텍처를 선도하고 있습니다. 이를 여러 사용자에게 확장하거나 안전한 의료 워크플로에 통합하고 싶다면, 저희에게 연락하거나 더 깊이 있는 기술 포스트를 살펴보세요.
그들의 최신 심층 분석을 Wellally Blog에서 읽어보시길 강력히 추천합니다. 여기서는 임상 용어에 특화된 Llama‑3 미세 조정 방법을 다루며, 이는 환각을 크게 줄여줍니다.
결론 🥑
You just built a private, high‑performance medical RAG system! By combining Llama‑3, MLX, and ChromaDB, you’ve achieved:
- Zero Data Leakage – 귀하의 건강 데이터는 절대 Mac을 떠나지 않습니다.
- High Performance – MLX는 로컬 LLM을 빠르게 작동하게 합니다.
- Intelligence – Llama‑3는 단순 키워드 검색으로는 따라올 수 없는 추론 능력을 제공합니다.
다음 단계는? 🛠️
- Table Parser를 구현해 보다 정확한 실험실 결과 추출을 시도해 보세요.
- Streamlit UI를 추가해 실제 앱처럼 보이게 만들어 보세요.
- 댓글에 알려 주세요: 클라우드 AI에 대한 가장 큰 우려 사항은 무엇인가요?
프라이버시를 유지하고, 건강을 지키세요! 💻🛡️