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

POST /api/generate — Raw completion

~16 min · api, completion, fim

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

/api/generate 쓸 때

/api/generatemessages array 대신 prompt 문자열 하나 받아. 이런 모양이 맞을 때:

  • Fill-in-the-middle (FIM) 코드 자동완성. suffix 필드에 cursor 코드 박으면 모델이 가운데 채워.
  • 대화 framing 없이 one-shot completion 원할 때.
  • Raw mode (raw: true) 보내서 모델 chat template 완전 우회 — 커스텀 prompt 형식 가진 fine-tuned 모델에 유용.

FIM이 킬러 use case

코딩 튜닝 모델 (Qwen3-Coder, Code Llama 변형, DeepSeek-Coder)이 FIM 토큰 이해해서 prefix + suffix 주면 함수 가운데 완성해. Continue.dev, Cursor (local 모델 쓸 때), Aider 같은 에디터의 local GitHub-Copilot-스타일 자동완성을 이게 굴리는 거야.

다른 유용한 필드

  • images — base64 image string array (멀티모달 모델용).
  • options/api/chat이랑 동일.
  • format/api/chat이랑 동일.
  • raw — boolean; true면 prompt에 template 안 적용.

Code

Suffix 가지고 FIM completion·python
import httpx

# 가운데 비어있는 코드 — 모델이 채워
prefix = '''def parse_csv(path: str) -> list[dict]:
    """Parse a CSV file and return rows as dicts."""
    with open(path, encoding="utf-8") as f:
        reader = csv.DictReader(f)
'''
suffix = '''
        return rows
'''

resp = httpx.post(
    "http://localhost:11434/api/generate",
    json={
        "model": "qwen3-coder:7b",
        "prompt": prefix,
        "suffix": suffix,
        "stream": False,
        "options": {"temperature": 0.2, "num_predict": 200},
    },
    timeout=60.0,
)
print(resp.json()["response"])
# 기대: rows = [r for r in reader]
커스텀 template용 raw mode·python
# 자체 형식 가진 fine-tuned 모델에서 chat template 우회
custom_prompt = "<|im_start|>user\nHello<|im_end|>\n<|im_start|>assistant\n"

resp = httpx.post(
    "http://localhost:11434/api/generate",
    json={
        "model": "my-custom-model",
        "prompt": custom_prompt,
        "raw": True,           # Ollama template skip
        "stream": False,
    },
)
print(resp.json()["response"])

External links

Exercise

/api/generatesuffix 필드 사용한 작은 complete(prefix, suffix, model) 함수 구현. 진짜 Python 코드 세 개 (list comprehension, try/except, 함수 본문) 가운데 채우는 데 써. Suffix가 도움 될 때랑 작은 모델 헷갈리게 할 때 메모.

Progress

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

댓글 0

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

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