LM Studio API 사용법
LM Studio에서 모델을 받아 로드한 뒤 Developer의 서버를 시작하세요. 다음 예시는 같은 컴퓨터의 기본 포트 1234에서 채팅을 호출합니다.
확인일
Developer에서 로컬 서버 시작하기
- Developer 화면에서 Start server를 켭니다. 화면이 보이지 않으면 Settings에서 Developer 모드를 확인합니다.
- 표시된 서버 주소와 포트를 확인하세요. 이 예제의 주소는
http://127.0.0.1:1234입니다. - 호출할 모델을 Chat 또는 Developer에서 로드합니다.
목록에서 실제 모델 ID 찾기
다음 주소를 실행합니다. Windows PowerShell에서 curl을 쓸 때는 curl.exe를 사용하세요.
curl http://127.0.0.1:1234/v1/models응답의 data 배열에서 사용할 모델의 id를 복사합니다. 다른 포트를 설정했다면 아래 예제에도 같은 값을 넣습니다.
OpenAI 호환 채팅 요청 보내기
아래 MODEL_ID를 방금 확인한 실제 값으로 바꾸고, 사용 중인 터미널에 맞는 예제를 실행합니다.
macOS·Linux 터미널
curl http://127.0.0.1:1234/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "MODEL_ID",
"messages": [{"role": "user", "content": "Write one sentence about spring."}]
}'Windows PowerShell
$body = @{
model = 'MODEL_ID'
messages = @(@{
role = 'user'
content = 'Write one sentence about spring.'
})
} | ConvertTo-Json -Depth 4
$result = Invoke-RestMethod -Method Post `
-Uri 'http://127.0.0.1:1234/v1/chat/completions' `
-ContentType 'application/json' -Body $body
$result.choices[0].message.content응답의 choices[0].message.content에 모델 답변이 들어갑니다. 별도 클라이언트를 연결할 때는 기본 URL을 http://127.0.0.1:1234/v1로 지정합니다. 연결이 거부되면 LM Studio의 서버 상태와 포트를 확인하세요.