Function Calling API๋ฅผ ์ฌ์ฉํ์ฌ ์ปค์คํ ํจ์๋ฅผ ์์ฑํ AI ๋ชจ๋ธ์ ์ ๊ณตํ ์ ์์ต๋๋ค. ๋ชจ๋ธ์ ์ด๋ฌํ ํจ์๋ฅผ ์ง์ ํธ์ถํ์ง ์์ง๋ง ๋์ ํจ์ ์ด๋ฆ๊ณผ ์ถ์ฒ ์ธ์๋ฅผ ์ง์ ํ๋ ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ ์ถ๋ ฅ์ ์์ฑํฉ๋๋ค.
์ด ์ถ๋ ฅ์ ํตํด ์ธ๋ถ API ๋๋ ์ ๋ณด ์์คํ (์: ๋ฐ์ดํฐ๋ฒ ์ด์ค, ๊ณ ๊ฐ ๊ด๊ณ ๊ด๋ฆฌ ์์คํ , ๋ฌธ์ ์ ์ฅ์)์ ํธ์ถํ ์ ์์ต๋๋ค. LLM์์ ์๋ต ํ์ง์ ๊ฐ์ ํ๋ ๋ฐ ๊ฒฐ๊ณผ API ์ถ๋ ฅ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํจ์ ํธ์ถ์ ๊ดํ ๊ฐ๋ ์ ๋ฌธ์๋ ํจ์ ํธ์ถ์ ์ฐธ๊ณ ํ์ธ์.
์ง์๋๋ ๋ชจ๋ธ:
๋ชจ๋ธ | ๋ฒ์ |
---|---|
Gemini 2.0 Flash-Lite | gemini-2.0-flash-lite-001 |
Gemini 2.0 Flash | gemini-2.0-flash-001 |
์ ํ์ฌํญ:
- ์์ฒญ์ ์ ๊ณตํ ์ ์๋ ์ต๋ ํจ์ ์ ์ธ ์๋ 128๊ฐ์ ๋๋ค.
์์ ๊ตฌ๋ฌธ
ํจ์ ํธ์ถ API ์์ฒญ์ ๋ณด๋ด๋ ๊ตฌ๋ฌธ์ ๋๋ค.
curl
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \ -d '{ "contents": [{ ... }], "tools": [{ "function_declarations": [ { ... } ] }] }'
Python
gemini_model = GenerativeModel( MODEL_ID, generation_config=generation_config, tools=[ Tool( function_declarations=[ FunctionDeclaration( ... ) ] ) ], )
ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก
๊ตฌํ ์ธ๋ถ์ ๋ณด๋ ์์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
FunctionDeclaration
OpenAPI 3.0 ์ฌ์์ ๋ฐ๋ผ ๋ชจ๋ธ์์ JSON ์ ๋ ฅ์ ์์ฑํ ์ ์๋ ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ | |
---|---|
|
ํธ์ถํ๋ ค๋ ํจ์์ ์ด๋ฆ์ ๋๋ค. ๋ฌธ์ ๋๋ ๋ฐ์ค๋ก ์์ํด์ผ ํ๋ฉฐ a~z, A~Z, 0~9์ด๊ฑฐ๋ ๋ฐ์ค, ์ ๋๋ ๋์๋ฅผ ํฌํจํ ์ ์๊ณ ์ต๋ 64์ ๊ธธ์ด์ ๋๋ค. |
|
(์ ํ์ฌํญ) ํจ์ ์ค๋ช ๋ฐ ์ฉ๋์ ๋๋ค. ๋ชจ๋ธ์ ์ด๋ฅผ ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ํธ์ถํ ์ง ์ฌ๋ถ์ ๋ฐฉ๋ฒ์ ๊ฒฐ์ ํฉ๋๋ค. ์ต์์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ผ๋ ค๋ฉด ์ค๋ช ์ ํฌํจํ๋ ๊ฒ์ด ์ข์ต๋๋ค. |
|
(์ ํ์ฌํญ) ํจ์ ํ๋ผ๋ฏธํฐ๋ฅผ OpenAPI JSON ์คํค๋ง ๊ฐ์ฒด ํ์์ธ OpenAPI 3.0 ์ฌ์์ผ๋ก ์ค๋ช ํฉ๋๋ค. |
|
(์ ํ์ฌํญ) ํจ์์ ์ถ๋ ฅ์ OpenAPI JSON ์คํค๋ง ๊ฐ์ฒด ํ์์ธ OpenAPI 3.0 ์ฌ์์ผ๋ก ์ค๋ช ํฉ๋๋ค. |
์์ธํ ๋ด์ฉ์ ํจ์ ํธ์ถ์ ์ฐธ์กฐํ์ธ์.
Schema
OpenAPI 3.0 ์คํค๋ง ์ฌ์์ ๋ฐ๋ผ ํจ์ ํธ์ถ์์ ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ ๋ฐ์ดํฐ์ ํ์์ ์ ์ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ | |
---|---|
์ ํ |
Enum. ๋ฐ์ดํฐ ์ ํ์ ๋๋ค. ๋ค์ ์ค ํ๋์ฌ์ผ ํฉ๋๋ค.
|
description |
(์ ํ์ฌํญ) ๋ฐ์ดํฐ์ ๋ํ ์ค๋ช ์ ๋๋ค. |
enum |
(์ ํ์ฌํญ) enum ํ์์ ๊ฐ๋ฅํ ์์ ์ ํ ์์ ๊ฐ์ ๋๋ค. |
items |
(์ ํ์ฌํญ)
|
properties |
(์ ํ์ฌํญ)
|
required |
(์ ํ์ฌํญ)
|
nullable |
(์ ํ์ฌํญ) ๊ฐ์ด |
FunctionCallingConfig
FunctionCallingConfig
๋ ๋ชจ๋ธ์ ๋์์ ์ ์ดํ๊ณ ํธ์ถํ ํจ์ ์ ํ์ ๊ฒฐ์ ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ | |
---|---|
|
(์ ํ์ฌํญ)
|
|
(์ ํ์ฌํญ) ํธ์ถํ ํจ์ ์ด๋ฆ์
๋๋ค. |
functionCall
functionDeclaration.name
์ ๋ํ๋ด๋ ๋ฌธ์์ด ๋ฐ ํ๋ผ๋ฏธํฐ์ ํด๋น ๊ฐ์ด ํฌํจ๋ ๊ตฌ์กฐํ๋ JSON ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ๋ชจ๋ธ์์ ๋ฐํ๋ ์์ธก๋ functionCall
์
๋๋ค.
ํ๋ผ๋ฏธํฐ | |
---|---|
|
ํธ์ถํ๋ ค๋ ํจ์์ ์ด๋ฆ์ ๋๋ค. |
|
JSON ๊ฐ์ฒด ํ์์ ํจ์ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ๋๋ค. ํ๋ผ๋ฏธํฐ ์ธ๋ถ์ ๋ณด๋ ํจ์ ํธ์ถ์ ์ฐธ๊ณ ํ์ธ์. |
functionResponse
FunctionDeclaration.name
์ ๋ํ๋ด๋ ๋ฌธ์์ด์ด ํฌํจ๋ FunctionCall
์ ๊ฒฐ๊ณผ ์ถ๋ ฅ์
๋๋ค. ํจ์์ ์ถ๋ ฅ์ด ํฌํจ๋ ๊ตฌ์กฐํ๋ JSON ๊ฐ์ฒด๋ ํฌํจ๋๋ฉฐ ์ด๋ฅผ ๋ชจ๋ธ์ ์ปจํ
์คํธ๋ก ์ฌ์ฉํฉ๋๋ค. ์ฌ๊ธฐ์๋ ๋ชจ๋ธ ์์ธก์ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ FunctionCall
์ ๊ฒฐ๊ณผ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ | |
---|---|
|
ํธ์ถํ๋ ค๋ ํจ์์ ์ด๋ฆ์ ๋๋ค. |
|
JSON ๊ฐ์ฒด ํ์์ ํจ์ ์๋ต์ ๋๋ค. |
์์
ํจ์ ์ ์ธ ์ ์ก
๋ค์์ ์ฟผ๋ฆฌ ๋ฐ ํจ์ ์ ์ธ์ ๋ชจ๋ธ์ ์ ์กํ๋ ๊ธฐ๋ณธ ์์์ ๋๋ค.
REST
์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ค์์ ๋ฐ๊ฟ๋๋ค.
- PROJECT_ID: ํ๋ก์ ํธ ID์ ๋๋ค.
- LOCATION: ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฆฌ์
- MODEL_ID: ์ฒ๋ฆฌ ์ค์ธ ๋ชจ๋ธ์ ID
- ROLE: ๋ฉ์์ง๋ฅผ ๋ง๋๋ ํญ๋ชฉ์ ID
- TEXT: ๋ชจ๋ธ์ ์ ์กํ ํ๋กฌํํธ
- NAME: ํธ์ถํ๋ ค๋ ํจ์์ ์ด๋ฆ
- DESCRIPTION: ํจ์ ์ค๋ช ๋ฐ ์ฉ๋
- ๋ค๋ฅธ ํ๋์ ๋ํด์๋ ํ๋ผ๋ฏธํฐ ๋ชฉ๋ก ํ ์ด๋ธ์ ์ฐธ๊ณ ํ์ธ์.
HTTP ๋ฉ์๋ ๋ฐ URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:generateContent
JSON ์์ฒญ ๋ณธ๋ฌธ:
{ "contents": [{ "role": "ROLE", "parts": [{ "text": "TEXT" }] }], "tools": [{ "function_declarations": [ { "name": "NAME", "description": "DESCRIPTION", "parameters": { "type": "TYPE", "properties": { "location": { "type": "TYPE", "description": "DESCRIPTION" } }, "required": [ "location" ] } } ] }] }
์์ฒญ์ ๋ณด๋ด๋ ค๋ฉด ๋ค์ ์ต์ ์ค ํ๋๋ฅผ ์ ํํฉ๋๋ค.
curl
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:generateContent"
PowerShell
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:generateContent" | Select-Object -Expand Content
curl ๋ช ๋ น์ด ์์
PROJECT_ID=myproject
LOCATION=us-central1
MODEL_ID=gemini-2.0-flash-001
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \
-d '{
"contents": [{
"role": "user",
"parts": [{
"text": "What is the weather in Boston?"
}]
}],
"tools": [{
"functionDeclarations": [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616"
}
},
"required": [
"location"
]
}
}
]
}]
}'
Python์ฉ ์์ฑํ AI SDK
Node.js
Java
Go
REST(OpenAI)
OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Function Calling API๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Vertex AI ๋ชจ๋ธ ํธ์ถ์ ์ฐธ์กฐํ์ธ์.
์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ค์์ ๋ฐ๊ฟ๋๋ค.
- PROJECT_ID: ํ๋ก์ ํธ ID์ ๋๋ค.
- LOCATION: ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฆฌ์
- MODEL_ID: ์ฒ๋ฆฌ ์ค์ธ ๋ชจ๋ธ์ ID
HTTP ๋ฉ์๋ ๋ฐ URL:
POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions
JSON ์์ฒญ ๋ณธ๋ฌธ:
{ "model": "google/MODEL_ID", "messages": [ { "role": "user", "content": "What is the weather in Boston?" } ], "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" } }, "required": ["location"] } } } ] }
์์ฒญ์ ๋ณด๋ด๋ ค๋ฉด ๋ค์ ์ต์ ์ค ํ๋๋ฅผ ์ ํํฉ๋๋ค.
curl
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions"
PowerShell
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Python(OpenAI)
OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Function Calling API๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Vertex AI ๋ชจ๋ธ ํธ์ถ์ ์ฐธ์กฐํ์ธ์.
FunctionCallingConfig
๋ฅผ ์ฌ์ฉํ์ฌ ํจ์ ์ ์ธ ์ ์ก
๋ค์์ FunctionCallingConfig
๋ฅผ ๋ชจ๋ธ์ ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ๋ ์์์
๋๋ค.
functionCallingConfig
๋ ๋ชจ๋ธ ์ถ๋ ฅ์ด ํญ์ ํน์ ํจ์ ํธ์ถ์ด ๋๋๋ก ํ์ธํฉ๋๋ค. ๊ตฌ์ฑ ๋ฐฉ๋ฒ:
- ํธ์ถ ํจ์
mode
๋ฅผANY
๋ก ์ค์ ํฉ๋๋ค. allowed_function_names
์์ ์ฌ์ฉํ ํจ์ ์ด๋ฆ์ ์ง์ ํฉ๋๋ค.allowed_function_names
๊ฐ ๋น์ด ์์ผ๋ฉด ์ ๊ณต๋ ํจ์ ์ค ํ๋๊ฐ ๋ฐํ๋ ์ ์์ต๋๋ค.
REST
PROJECT_ID=myproject
LOCATION=us-central1
MODEL_ID=gemini-2.0-flash-001
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${LOCATION}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \
-d '{
"contents": [{
"role": "user",
"parts": [{
"text": "Do you have the White Pixel 8 Pro 128GB in stock in the US?"
}]
}],
"tools": [{
"functionDeclarations": [
{
"name": "get_product_sku",
"description": "Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc",
"parameters": {
"type": "object",
"properties": {
"product_name": {"type": "string", "description": "Product name"}
}
}
},
{
"name": "get_store_location",
"description": "Get the location of the closest store",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "Location"}
},
}
}
]
}],
"toolConfig": {
"functionCallingConfig": {
"mode":"ANY",
"allowedFunctionNames": ["get_product_sku"]
}
},
"generationConfig": {
"temperature": 0.95,
"topP": 1.0,
"maxOutputTokens": 8192
}
}'
Python์ฉ ์์ฑํ AI SDK
Node.js
Go
REST(OpenAI)
OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Function Calling API๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Vertex AI ๋ชจ๋ธ ํธ์ถ์ ์ฐธ์กฐํ์ธ์.
์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ค์์ ๋ฐ๊ฟ๋๋ค.
- PROJECT_ID: ํ๋ก์ ํธ ID์ ๋๋ค.
- LOCATION: ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฆฌ์
- MODEL_ID: ์ฒ๋ฆฌ ์ค์ธ ๋ชจ๋ธ์ ID
HTTP ๋ฉ์๋ ๋ฐ URL:
POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions
JSON ์์ฒญ ๋ณธ๋ฌธ:
{ "model": "google/MODEL_ID", "messages": [ { "role": "user", "content": "What is the weather in Boston?" } ], "tools": [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "OBJECT", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616" } }, "required": ["location"] } } } ], "tool_choice": "auto" }
์์ฒญ์ ๋ณด๋ด๋ ค๋ฉด ๋ค์ ์ต์ ์ค ํ๋๋ฅผ ์ ํํฉ๋๋ค.
curl
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions"
PowerShell
์์ฒญ ๋ณธ๋ฌธ์ request.json
ํ์ผ์ ์ ์ฅํ๊ณ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํฉ๋๋ค.
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/endpoints/openapi/chat/completions" | Select-Object -Expand Content
Python(OpenAI)
OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Function Calling API๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ OpenAI ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ Vertex AI ๋ชจ๋ธ ํธ์ถ์ ์ฐธ์กฐํ์ธ์.
๋ค์ ๋จ๊ณ
์์ธํ ๋ฌธ์๋ ๋ค์์ ์ฐธ์กฐํ์ธ์.