Skip to main content

로컬 LLM(Ollama)로 할루시네이션과 컨텍스트 정보 소실을 실습하는 교육용 패키지

Project description

hallu_lab — LLM 할루시네이션 & 컨텍스트 소실 실습

로컬 LLM(Ollama)을 사용해 다음 두 가지 현상을 직접 관찰하는 교육용 파이썬 패키지입니다.

  1. 할루시네이션(환각) — 모델이 모르는 것을 그럴듯하게 지어내는 현상
  2. 컨텍스트 정보 소실 — 문맥이 길어지거나 핵심 정보가 가운데 묻히면 놓치는 현상

1. 사전 준비

# (1) Ollama 설치  →  https://ollama.com/download
# (2) 가벼운 모델 내려받기 (3B 권장: 노트북에서도 무난)
ollama pull llama3.2

# (3) 이 패키지 설치 (code 디렉토리에서)
pip install -e .
#   주피터까지 함께:  pip install -e ".[notebook]"

설치 후 Ollama 서버가 떠 있는지 확인:

from hallu_lab import OllamaClient
client = OllamaClient(model="llama3.2")
print(client.is_available())   # True 면 준비 완료

2. 빠른 사용 (주피터)

from hallu_lab import OllamaClient, run_hallucination_demo, length_sweep, position_sweep, summarize

client = OllamaClient(model="llama3.2", seed=0)

# 실습 A: 할루시네이션 — 가드레일 유무 비교
for r in run_hallucination_demo(client):
    r.show()

# 실습 B-1: 문서가 길어질수록 핵심 정보를 놓치는가 (길이 효과)
summarize(length_sweep(client, filler_sizes=[10, 100, 300, 600]))

# 실습 B-2: 핵심 정보 위치에 따른 차이 (lost-in-the-middle)
summarize(position_sweep(client, n_filler=400))

자세한 단계별 실습은 examples/demo.py 를 참고하세요.


3. 패키지 구조

파일 역할
hallu_lab/client.py Ollama 호출 래퍼 (OllamaClient)
hallu_lab/datasets.py 허구 질문 / 바늘·채움 텍스트 생성
hallu_lab/hallucination.py 할루시네이션 실습 (run_hallucination_demo, temperature_sweep)
hallu_lab/context_loss.py 컨텍스트 소실 실습 (length_sweep, position_sweep)

4. 실습 포인트 (강의용)

  • 가드레일 효과: 시스템 프롬프트로 "모르면 모른다고 하라"고 지시했을 때 할루시네이션이 줄어드는지 비교.
  • 온도(temperature): temperature_sweep로 온도가 높을수록 답이 흔들리는 것을 관찰.
  • 길이 효과: length_sweep에서 num_ctx를 작게(예: 512) 주면 컨텍스트 창을 넘쳐 정보가 잘리는 현상을 더 극적으로 재현.
  • 위치 효과: position_sweep에서 보통 중앙(50%)에서 성공률이 가장 낮음.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hallu_lab-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hallu_lab-0.1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file hallu_lab-0.1.0.tar.gz.

File metadata

  • Download URL: hallu_lab-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for hallu_lab-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a3435fadf4ce506ca6bc537b7bb12181584ee7757495e27db4daa2b26d91413c
MD5 163ed61a08e262ed7b5c3500065bb265
BLAKE2b-256 66d310dbd459e81e0e853c447977170876f16ef79633301ce1f259f65eb8d687

See more details on using hashes here.

File details

Details for the file hallu_lab-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hallu_lab-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for hallu_lab-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 560d44ace61926b0e8e77941d11ae654b6cf61c52dc5f8a07b63aeb23b2e5f15
MD5 086ce75e65c4c4362d6d1555b455a081
BLAKE2b-256 b3d1d7fd7b8439a6846fb161ca0ef338ead305cb81435bd2096c4232fd7c0b2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page