도구 호출과 정해진 형식
tools와 response_format
도구 호출
tools에 모델이 부를 수 있는 함수를 적어요. 모델이 도구를 부르면 tool_calls가 오고, 실행한 결과를 tool 역할의 메시지로 다시 보내면 이어서 답해요.
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "도시의 지금 날씨를 알려 줘요",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
answer = client.chat.completions.create(
model="anthropic/claude-opus-5.5",
messages=[{"role": "user", "content": "서울 날씨 어때?"}],
tools=tools,
)
call = answer.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)- 도구를 받는 모델은 모델별 지원에서 볼 수 있어요. 받지 않는 모델에 보내면
tools_not_supported오류가 나요. tool_choice로 꼭 부르게 하거나(required), 부르지 않게(none) 할 수 있어요. 꼭 부르게 하는 것을 받지 않는 모델에는forced_tool_not_supported오류로 알려 드려요.
정해진 형식으로 받기
response_format에 JSON 스키마를 주면 그 모양으로만 답해요.
answer = client.chat.completions.create(
model="anthropic/claude-opus-5.5",
messages=[{"role": "user", "content": "서울과 부산의 인구를 알려 줘"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "cities",
"schema": {
"type": "object",
"properties": {
"cities": {"type": "array", "items": {
"type": "object",
"properties": {"name": {"type": "string"}, "population": {"type": "integer"}},
"required": ["name", "population"],
}},
},
"required": ["cities"],
},
},
},
)모델이 받지 않는 형식이면 response_format_not_supported 오류로 알려 드려요. 이럴 때는 형식을 받는 모델로 바꾸거나 원하는 모양을 프롬프트에 적어 주세요.