도구 호출 및 구조화된 출력
엄격한 JSON Schema 도구 정의와 보장된 구조화 응답을 통해 LLM을 내부 API, 데이터베이스 및 외부 워크플로에 안전하게 연결할 수 있습니다.
함수 / 도구 호출 루프
- 사용자 메시지와 함께
tools(JSON Schema 파라미터 정의)를 전송합니다. - 응답의
message.tool_calls(또는 Anthropic Messages의tool_use블록)를 확인합니다. - 애플리케이션에서 해당 함수를 로컬로 실행하고,
tool_call_id를 참조하는role: "tool"결과를 메시지 배열에 추가합니다. - 업데이트된 메시지 배열을 다시 전송하여 근거에 기반한 최종 응답을 받습니다.
python
import json, os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["VANIX_API_KEY"],
base_url="https://api.vanix.io/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Retrieve live shipment status by order ID.",
"parameters": {
"type": "object",
"properties": {
"order_id": {"type": "string", "description": "Order ID such as ORD-9042"}
},
"required": ["order_id"],
"additionalProperties": False,
},
"strict": True,
},
}
]
resp = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Where is order ORD-9042?"}],
tools=tools,
tool_choice="auto",
)
for call in resp.choices[0].message.tool_calls or []:
print(call.function.name, json.loads(call.function.arguments))엄격한 구조화 출력 (json_schema)
불안정한 정규식 파싱 없이 스키마를 완벽히 준수하는 JSON 출력을 보장할 수 있습니다:
bash
curl https://api.vanix.io/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $VANIX_API_KEY" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "user", "content": "Classify ticket: Billing invoice PDF returns 403 after login."}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "ticket_triage",
"strict": true,
"schema": {
"type": "object",
"properties": {
"category": {"type": "string", "enum": ["billing", "auth", "api", "other"]},
"severity": {"type": "string", "enum": ["p1", "p2", "p3"]},
"summary": {"type": "string"}
},
"required": ["category", "severity", "summary"],
"additionalProperties": false
}
}
}
}'Strict 모드 체크리스트
OpenAI 스키마에서 "strict": true를 활성화하면 모든 객체 계층에 "additionalProperties": false를 선언하고 모든 키를 "required" 배열에 명시해야 합니다.