
한 줄 정의:
LLM이 “도구(함수/외부 API)”를 직접 호출해야 할 때, 무엇을 호출하고 어떤 인자로 호출할지 모델이 스스로 결정해 구조화된 호출 요청을 만들어 주는 메커니즘이다. 덕분에 모델 답변이 말뿐인 텍스트를 넘어, 실제 DB 조회·결제·검색·자동화 같은 실행으로 연결된다.
name, description, parameters(JSON Schema)auto(모델이 선택), 특정 함수 강제, 호출 금지 등 정책get_weather + { city:"Seoul", unit:"c" })[
{
"name": "get_weather",
"description": "도시의 현재 날씨를 조회",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" },
"unit": { "type": "string", "enum": ["c", "f"] }
},
"required": ["city"]
}
}
]
get_weather({"city":"Seoul","unit":"c"}){ "temp_c": 24, "condition": "Cloudy" }포인트: LLM은 무슨 함수 + 어떤 인자를 JSON으로 제안할 뿐, 실제 실행은 앱이 한다.
tools = [{
"name": "book_hotel",
"description": "도시, 날짜로 호텔 예약",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"check_in": {"type": "string", "format": "date"},
"nights": {"type": "integer", "minimum": 1}
},
"required": ["city", "check_in", "nights"]
}
}]
res1 = llm.chat(messages=[{"role":"user","content":"다음 주말 부산 1박 숙소 잡아줘"}],
tools=tools, tool_choice="auto")
if "tool_call" in res1:
call = res1["tool_call"]
result = run_local(call["name"], call["arguments"]) # 실제 API 호출
res2 = llm.chat(messages=[
{"role":"user","content":"다음 주말 부산 1박 숙소 잡아줘"},
res1, # tool_call 메시지
{"role":"tool","name":call["name"],"content":json.dumps(result)}
], tools=tools)
print(res2["content"])