함수 호출
모델이 애플리케이션의 구조화된 작업을 호출할 수 있게 합니다.
개요
함수 호출(또는 "도구 사용")을 통해 모델에 함수를 설명하면, 모델이 구조화된 JSON 파라미터로 해당 함수를 호출할 수 있습니다. 이를 통해 모델이 외부 시스템, 데이터베이스, API와 상호작용할 수 있습니다.
도구 정의
요청에 JSON Schema 정의가 포함된 tools 배열을 전달하세요:
TypeScript
| 1 | const completion = await client.chat.completions.create({ |
| 2 | model: 'gpt-4o', |
| 3 | messages: [{ role: 'user', content: '도쿄의 날씨가 어떤가요?' }], |
| 4 | tools: [ |
| 5 | { |
| 6 | type: 'function', |
| 7 | function: { |
| 8 | name: 'get_weather', |
| 9 | description: '지정한 위치의 현재 날씨를 가져옵니다', |
| 10 | parameters: { |
| 11 | type: 'object', |
| 12 | properties: { |
| 13 | location: { type: 'string', description: '도시 이름' }, |
| 14 | unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }, |
| 15 | }, |
| 16 | required: ['location'], |
| 17 | }, |
| 18 | }, |
| 19 | }, |
| 20 | ], |
| 21 | }); |
도구 호출 처리
모델이 함수 호출을 결정하면 응답에 tool_calls 배열이 포함됩니다. 함수를 실행하고 결과를 다시 전송하세요:
TypeScript
| 1 | const toolCall = completion.choices[0].message.tool_calls[0]; |
| 2 | const args = JSON.parse(toolCall.function.arguments); |
| 3 | |
| 4 | // 함수 실행 |
| 5 | const weather = await getWeather(args.location, args.unit); |
| 6 | |
| 7 | // 결과 전송 |
| 8 | const followUp = await client.chat.completions.create({ |
| 9 | model: 'gpt-4o', |
| 10 | messages: [ |
| 11 | { role: 'user', content: '도쿄의 날씨가 어떤가요?' }, |
| 12 | completion.choices[0].message, |
| 13 | { |
| 14 | role: 'tool', |
| 15 | tool_call_id: toolCall.id, |
| 16 | content: JSON.stringify(weather), |
| 17 | }, |
| 18 | ], |
| 19 | }); |
지원 모델
"함수 호출" 기능을 가진 모델이 이 기능을 지원합니다. GPT-4o, Claude Sonnet/Opus, Gemini 모델 등이 포함됩니다. 모델 페이지에서 "Tools" 기능으로 필터링하세요.