C.W.K.
Stream
Lesson 05 of 05 · published

Option과 Structured output

~22 min · api, options, json-schema

Level 0Downloader
0 XP0/41 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Options block — generation 정밀 컨트롤

모든 generation parameter는 options 객체 아래 살아. 자주 쓰는 거:

  • temperature — 무작위성 (0 = deterministic, 1.5+ = 카오스). Default 0.8.
  • top_p — Nucleus sampling cutoff. Default 0.9.
  • top_k — Top-K sampling. Default 40.
  • num_ctx — Context window 토큰 (모델마다 다름; 보통 4096; 실전엔 8192 / 16384 / 32768로 올려).
  • num_predict — 최대 생성 토큰. -1 = 무제한.
  • repeat_penalty — 반복 토큰 penalty. Default 1.1.
  • seed — 정수; 재현 가능한 run 위해 고정.
  • stop — Stop sequence array.
  • num_gpu — GPU layer 수. 999 = 전부 GPU.

JSON Schema로 Structured output

format 필드가 JSON Schema 객체 받아. 모델이 그 schema 매칭하는 JSON emit하도록 강제됨. API에서 가장 유용한 기능 중 하나야 — chat 모델을 prompt engineering 없이 typed data extractor로 바꿔.

Options vs Modelfile 언제?

Request의 options가 Modelfile default override해. 안정적인 default는 Modelfile에 박고 (이 variant는 coder; ctx는 항상 16K), temperature, seed, num_predict는 request마다 변화시켜.

Code

Options로 튜닝된 generation·python
import httpx, json

resp = httpx.post(
    "http://localhost:11434/api/chat",
    json={
        "model": "qwen2.5:7b",
        "messages": [{"role": "user", "content": "Write three creative haiku about the sea."}],
        "stream": False,
        "options": {
            "temperature": 0.95,    # creative
            "top_p": 0.95,
            "num_ctx": 8192,
            "num_predict": 300,
            "seed": 42,             # 재현 가능
            "stop": ["---"],
        },
    },
    timeout=120.0,
)
print(resp.json()["message"]["content"])
JSON Schema로 structured output·python
schema = {
    "type": "object",
    "properties": {
        "subject": {"type": "string"},
        "verb": {"type": "string"},
        "adjectives": {"type": "array", "items": {"type": "string"}},
        "word_count": {"type": "integer"},
    },
    "required": ["subject", "verb", "adjectives", "word_count"],
}

resp = httpx.post(
    "http://localhost:11434/api/chat",
    json={
        "model": "qwen2.5:7b",
        "messages": [{
            "role": "user",
            "content": "Analyze: 'The quick brown fox jumps over the lazy dog'",
        }],
        "stream": False,
        "format": schema,
    },
    timeout=120.0,
)
result = json.loads(resp.json()["message"]["content"])
print(result)
# {'subject': 'fox', 'verb': 'jumps', 'adjectives': ['quick', 'brown', 'lazy'], 'word_count': 9}

External links

Exercise

{vendor, total_amount, currency, line_items[]} JSON Schema로 format 사용한 extract_invoice(text) 함수 만들어. 가짜 invoice 문단 셋에서 test. 작은 모델이 필드 누락하는 경우랑 모델 size 올렸을 때 어떻게 고쳐지는지 메모.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.