Hyungi Ahn e1a2cdc677 feat(study): AI 풀이 생성 — 수동 트리거 + RAG (PR-3)
복습 답 제출 후 또는 편집 화면에서 사용자가 명시적으로 누를 때만 AI 가
4지선다 풀이 생성. 자동 일괄 생성 금지 (하루 100문제 입력 시 MLX 부하·
잘못 입력 문제 해설 위험).

데이터 모델 (migrations 191~192):
- study_questions 4 컬럼 추가: ai_explanation TEXT, ai_explanation_status
  VARCHAR(20) DEFAULT 'none' (none/pending/ready/failed/stale),
  ai_explanation_generated_at, ai_explanation_model
- partial idx (study_topic_id, ai_explanation_status) WHERE status != 'none'

PATCH stale 자동 전이: question_text/choice_*/correct_choice 변경 시
status='ready' 만 'stale' 로. 본문은 보존, UI 배지 + "다시 생성" 동선.

신규 엔드포인트: POST /api/study-questions/{id}/ai-explanation
- regenerate=false + ready/stale → 캐시 즉시 (MLX 호출 없음, is_stale 플래그)
- pending → 409 (race-safe 조건부 UPDATE 로 동시 호출 차단)
- 그 외 → 새 생성

RAG 입력 풀:
- 1순위: study_topic 매핑 documents 청크 + ai_summary, bge-reranker top-5
- 2순위: 같은 토픽 다른 questions (자기 자신 제외, ai_explanation 은 ready
  상태만 포함 — 재귀적 hallucination 방지), reranker top-3
- 제외: 필기 OCR / 외부 웹 / Premium 모델

모델: Mac mini MLX gemma-4-26b primary 단독. get_mlx_gate() Semaphore(1) 경유,
30s timeout. 실패 시 status='failed' + 직전 본문 보존.

프롬프트 (app/prompts/study_question_explanation.txt): 자료 우선순위·인용
형식·할루시네이션 방지 절대 규칙 (법령명·조항·수치·표준 번호 단정 금지,
"자료에서 확인되지 않음" 명시).

프론트:
- 복습 화면 답 제출 후 인라인 expand. status별 버튼 분기 (ready 캐시 /
  stale "이전 풀이"+"다시 생성" / failed "다시 시도")
- 편집 화면 별도 카드. 상태 배지 + "이전 풀이 보기" / "다시 생성" 분리
- 참고 근거 토글 (source_type 별 아이콘 📄/ + 제목 + snippet)

후속 PR 보류: 오답노트/통계, AI 일괄 백그라운드 생성, 필기 OCR RAG,
Premium/Claude 재생성, /api/search/ask retrieval scope 통합.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 08:41:46 +09:00

hyungi_Document_Server

Self-hosted 개인 지식관리(PKM) 웹 애플리케이션

기술 스택

  • 백엔드: FastAPI + SQLAlchemy (async)
  • 데이터베이스: PostgreSQL 16 + pgvector + pg_trgm
  • 프론트엔드: SvelteKit
  • 문서 파싱: kordoc (HWP/HWPX/PDF → Markdown)
  • AI: Qwen3.5-35B-A3B (MLX), nomic-embed-text, Claude API (폴백)
  • 인프라: Docker Compose, Caddy, Synology NAS

주요 기능

  • 문서 자동 분류/태그/요약 (AI 기반)
  • 전문검색 + 벡터 유사도 검색
  • HWP/PDF/Markdown 문서 뷰어
  • 법령 변경 모니터링 (산업안전보건법 등)
  • 이메일 자동 수집 (MailPlus IMAP)
  • 일일 다이제스트
  • CalDAV 태스크 연동 (Synology Calendar)

Quick Start

git clone https://git.hyungi.net/hyungi/hyungi_document_server.git hyungi_Document_Server
cd hyungi_Document_Server

# 인증 정보 설정
cp credentials.env.example credentials.env
nano credentials.env  # 실제 값 입력

# 실행
docker compose up -d

http://localhost:8000/docs 에서 API 문서 확인

디렉토리 구조

├── app/              FastAPI 백엔드 (API, 워커, AI 클라이언트)
├── frontend/         SvelteKit 프론트엔드
├── services/kordoc/  문서 파싱 마이크로서비스 (Node.js)
├── gpu-server/       GPU 서버 배포 (AI Gateway)
├── migrations/       PostgreSQL 스키마
├── docs/             설계 문서, 배포 가이드
└── tests/            테스트 코드

인프라 구성

서버 역할
Mac mini M4 Pro Docker Compose (FastAPI, PostgreSQL, kordoc, Caddy) + MLX AI
Synology NAS 파일 원본 저장, Synology Office/Drive/Calendar/MailPlus
GPU 서버 AI Gateway, 벡터 임베딩, OCR, 리랭킹

문서

S
Description
No description provided
Readme 14 MiB
Languages
Python 67%
Svelte 23.1%
Swift 5.3%
TypeScript 3.2%
Shell 0.5%
Other 0.9%