Codestral FIM(채우기) API
Codestral FIM(채우기) API
"코드 앞부분은 있는데 중간만 채워줘" — 이게 FIM(Fill-in-the-Middle) 이에요. Codestral 의 FIM 엔드포인트는 바로 이 시나리오를 위한 API죠. 채팅 API와 비슷해 보이지만 suffix 개념이 핵심으로 들어가요.
POST /v1/fim/completions
베이스 URL은 https://api.mistral.ai이고, FIM 전용 경로는 /v1/fim/completions예요. 다음처럼 curl로 호출할 수 있어요.
curl https://api.mistral.ai/v1/fim/completions \
-X POST \
-H 'Authorization: Bearer YOUR_APIKEY_HERE' \
-H 'Content-Type: application/json' \
-d '{ "prompt": "def" }'
여기서 prompt는 커서 앞에 있는 코드, suffix는 커서 뒤에 남은 코드예요. 모델이 그 사이를 채워요.
Python SDK 예시
from mistralai import Mistral
import os
with Mistral(api_key=os.getenv("MISTRAL_API_KEY", "")) as mistral:
res = mistral.fim.complete(
model="codestral-latest",
prompt="def",
suffix="return a+b",
stream=False,
)
print(res)
스트리밍이 필요하면 stream=True로 바꾸면 토큰이 서버 전송 이벤트(SSE)로 흘러들어와요. 응답이 끝나면 data: [DONE] 메시지가 오죠.
파라미터 요약
| 파라미터 | 설명 |
|---|---|
model |
FIM에 쓸 모델 ID (기본 codestral-2404, 보통 codestral-latest) |
prompt |
커서 앞의 코드 |
suffix |
커서 뒤의 코드 |
temperature |
0.0~0.7 권장 |
stream |
부분 진행을 스트리밍할지 여부 |
더 알아보기
- 모델 상세: docs.mistral.ai/models/codestral-25-08
- Python/TS SDK 예시: github.com/mistralai/client-python