FC 함수 호출
함수 호출(Function Calling)은 모델이 필요할 때 사용자가 정의한 함수의 실행을 요청하고, 함수 결과를 기반으로 계속 답변할 수 있게 합니다.
4ALL API에서 GPT, Claude, Gemini 시리즈 모두 표준 OpenAI tools 프로토콜을 지원하므로, 하나의 코드로 여러 모델에서 사용할 수 있습니다.
기본 흐름
- 요청에서
tools로 함수 시그니처를 선언합니다; - 모델이
finish_reason: "tool_calls"와 함수명, JSON 파라미터를 반환합니다; - 함수를 실행하고 결과를
role: "tool"메시지로 전달합니다; - 모델이 결과를 바탕으로 최종 답변을 제공합니다.
예시
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "查询指定城市当前天气", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}]
resp = client.chat.completions.create( model="gpt-5.5", messages=messages, tools=tools)call = resp.choices[0].message.tool_calls[0]# 执行函数后回传:messages.append(resp.choices[0].message)messages.append({ "role": "tool", "tool_call_id": call.id, "content": '{"temp": 31, "condition": "sunny"}',})final = client.chat.completions.create(model="gpt-5.5", messages=messages, tools=tools)주의사항
- 파라미터는 모델이 생성한 JSON 문자열이므로, 반드시 검증 후 실행하고 명령어나 SQL에 직접 삽입하지 마십시오;
- 모델이 한 번에 여러
tool_calls를 반환할 수 있으므로, 각각 실행하고tool_call_id에 맞게 결과를 전달하십시오; tool_choice: "required"로 강제 호출할 수 있으며,"none"으로 비활성화할 수 있습니다;- 스트리밍 모드에서는 함수 파라미터가 증분 방식으로 전달되므로, 완전히 조합한 후 파싱해야 합니다. 스트리밍 출력을 참조하십시오.