내용 업데이트: 2026-06-09
개요
함수 호출은 모델이 사용자가 정의한 함수를 호출할 수 있도록 하며, 외부 시스템과의 상호작용을 가능하게 합니다. 모델은 함수를 직접 실행하지 않고, 함수 이름과 인수를 반환하며, 이 값들을 여러분의 코드가 실행하고 결과를 모델에 다시 반환합니다.전체 예제
import json
from openai import OpenAI
client = OpenAI(api_key="sk-xxx", base_url="https://api.crazyrouter.com/v1")
# 1. Define tools
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather information for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g.: New York"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["city"]
}
}
},
{
"type": "function",
"function": {
"name": "search_products",
"description": "Search for products",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search keyword"},
"max_price": {"type": "number", "description": "Maximum price"}
},
"required": ["query"]
}
}
}
]
# 2. Mock function implementations
def get_weather(city, unit="celsius"):
return {"city": city, "temperature": 22, "unit": unit, "condition": "Sunny"}
def search_products(query, max_price=None):
return [{"name": f"{query} Product A", "price": 99}, {"name": f"{query} Product B", "price": 199}]
# 3. Conversation loop
messages = [{"role": "user", "content": "What's the weather like in New York today? Also search for umbrellas"}]
response = client.chat.completions.create(
model="gpt-5.5",
messages=messages,
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
# 4. Handle tool calls
if message.tool_calls:
messages.append(message)
for tool_call in message.tool_calls:
func_name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
if func_name == "get_weather":
result = get_weather(**args)
elif func_name == "search_products":
result = search_products(**args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result, ensure_ascii=False)
})
# 5. Get final response
final = client.chat.completions.create(
model="gpt-5.5",
messages=messages,
tools=tools
)
print(final.choices[0].message.content)
특정 함수 호출 강제하기
response = client.chat.completions.create(
model="gpt-5.5",
messages=messages,
tools=tools,
tool_choice={"type": "function", "function": {"name": "get_weather"}}
)
지원 모델
이 페이지의 OpenAI 호환 함수 호출 예시에서는 다음 검증된 기준 모델을 먼저 사용하세요:gpt-5.5
gpt-5.5 도구 호출은 Crazyrouter 프로덕션에서 2026년 3월 23일에 검증되었습니다. 다른 벤더의 경우, 요청 형식과 지원 사항이 다를 수 있으므로 우선 해당 벤더의 전용 페이지를 확인하세요.