[LLM] Function Call (with Gemini)
Function Call์ OpenAI์์ ์ฒ์ ๊ณต๊ฐํ LLM ๊ธฐ๋ฐ ๊ฐ๋ฐ ๋ฐฉ๋ฒ๋ก ์ค ํ๋๋ค.
LLM์ ๊ธฐ๋ฐ์ผ๋ก ๊ธฐ๋ฅ์ ๊ฐ๋ฐํ๋ ค๊ณ ํ๋ค๋ณด๋ฉด, ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ด ์์ ๋ก์ด ๋ฌธ์์ด์ด๋ผ์ ๊ฐ๋ฐ์ ๋ํญ์ ๊ฒช์ ์ ์๋ค. Function Call์ ์ด๋ฌํ ๊ณผ์ ์ ๊ตฌ์กฐํํ๊ธฐ ์ํด ๋์จ ๊ฒ์ด๋ผ ํ ์ ์๋ค.
๊ธฐ๋ณธ ๊ฐ๋
Function Call์ ๊ธฐ๋ณธ ๊ฐ๋
์ ๋ณ๊ฒ ์๋ค.
LLM์ ์์ฒญ๊ณผ ์๋ต์ ํจ์์ฒ๋ผ ๊ตฌ์กฐํ๋ ํํ๋ก ์ํตํ๊ฒ ํ๋ ๊ฒ์ด๋ค.
Flow๋ ๋๋ต ์ด๋ ๋ค.
- Function Call๋ก ํจ์๋ค์ ์ ์ํด์ LLM์ ์์ฒญํ๋ฉด
- LLM์ด ๋ฌธ๋งฅ์ ๋ง๋ ํจ์๋ฅผ ์ ํํ๊ณ , ํจ์์ ์ ์์ ๋ง์ถฐ์ ๊ตฌ์กฐํ๋ ํํ๋ก ๊ฐ์ ๋๊ฒจ์ค๋ค. ์ด๊ฑธ ํจ์์ ์ธ์๋ผ๊ณ ๋ถ๋ฅธ๋ค.
- ์ด ์ํ์์ ์ ๋ณด๊ฐ ์ถฉ๋ถํ๋ค๋ฉด ์ ํ๋ ํจ์์ ์ธ์๊ฐ์ ์ฌ์ฉํด์ ๊ทธ๋๋ก ์ฐ๊ณ ๋๋ด๊ฑฐ๋
- LLM์ ํจ์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ ์กฐ๋ฆฝํด์ ๋ค์ ์์ฒญ์ ๋ณด๋ผ ์๋ ์๋ค.
ํ๊ณ
MCP์ ๋ค๋ฅด๊ฒ Function Call์ ํ์คํ๋ ์ธํฐํ์ด์ค ๊ฐ์๊ฒ ์๋๋ค.
๊ทธ๋์ Function Calling์ ๊ธฐ๋ฅ ํํ๋ ์
๋ ฅ/์ถ๋ ฅ ํ์์ LLM ์ ๊ณต์ฌ๋ง๋ค ๋ค๋ฅผ ์ ์๋ค.
์ฌ๊ธฐ์๋ Gemini์ ์์๋ง ๋ค์ด๋ณด๊ฒ ๋ค.
์ฌ์ฉ๋ก (Gemini 2 Flash)
์ฌ๊ธฐ์๋ Gemini๋ฅผ ๊ธฐ๋ฐ์ผ๋ก Function Call์ ์๋ ํํ๋ฅผ ํ๋ฒ ํ์ด๋ณด๊ฒ ๋ค.
Gemini ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด Gemini์ Function Call์ ์ด๋ฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค.
์ฌ์ฉ ์ธ์ด๋ ํ์ด์ฌ์ด๋ค.
ํ์ ๋ชจ๋๋ถํฐ ๊น์์ฃผ๊ณ
uv add google google.genai

์ฝ๋๋ฅผ ์์ฑํด๋ณด์.
from google.genai import types
from google import genai
API_KEY = "ํค๊ฐ"
# Define a function that the model can call to control smart lights
set_light_values_declaration = {
"name": "set_light_values",
"description": "Sets the brightness and color temperature of a light.",
"parameters": {
"type": "object",
"properties": {
"brightness": {
"type": "integer",
"description": "Light level from 0 to 100. Zero is off and 100 is full brightness",
},
"color_temp": {
"type": "string",
"enum": ["daylight", "cool", "warm"],
"description": "Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.",
},
},
"required": ["brightness", "color_temp"],
},
}
# This is the actual function that would be called based on the model's suggestion
def set_light_values(brightness: int, color_temp: str) -> dict[str, int | str]:
"""Set the brightness and color temperature of a room light. (mock API).
Args:
brightness: Light level from 0 to 100. Zero is off and 100 is full brightness
color_temp: Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.
Returns:
A dictionary containing the set brightness and color temperature.
"""
return {"brightness": brightness, "colorTemperature": color_temp}
<br>
# Generation Config with Function Declaration
tools = types.Tool(function_declarations=[set_light_values_declaration])
config = types.GenerateContentConfig(tools=[tools])
# Configure the client
client = genai.Client(api_key=API_KEY)
# Define user prompt
contents = [
types.Content(
role="user", parts=[types.Part(text="Turn the lights down to a romantic level")]
)
]
# Send request with function declarations
response = client.models.generate_content(
model="gemini-2.0-flash", config=config, contents=contents
)
# Extract tool call details
tool_call = response.candidates[0].content.parts[0].function_call
print(tool_call)
ํ๋์ฉ ๋ฏ์ด๋ณด์๋ฉด
์ด๊ฒ ์ฐ๋ฆฌ๊ฐ Gemini์ ์๊ตฌํ๋ ํจ์์ ์ ์๋ค.
ํจ์ ์ด๋ฆ, ์ค๋ช
, ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ๋ค.
์ค๋ช
์ ์ฝ์ด์ ํด๋น ํจ์๊ฐ ์๊ตฌํ๋ ๊ฒ์ LLM์ด ์ธ์งํ ๊ฒ์ด๊ณ , ํ๋ผ๋ฏธํฐ์ ์ ์์ ๋ง์ถฐ์ ๊ฐ๋ค์ ๋๊ฒจ์ค ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์ ํจ์์ ์ ์์ ํจ๊ป ์์ฒญ์ ๋ณด๋ธ๋ค.
๋ญ๋ง์ ์ธ ๋ฐ๊ธฐ์ ์จ๋๋ฅผ ์๋ ค๋ฌ๋ผ๊ณ ํ๋ค.
๊ทธ๋ฌ๋ฉด ๊ทธ ์๊ตฌ์ฌํญ์ ๋ง์ถฐ์ ์ฉ๋์ ๋ง๋ ํจ์๋ฅผ ์์์ ์ ํํ๊ณ , ๊ทธ์ ๋ง๋ ์ธ์๊ฐ๋ ๋ฃ์ด์ ์๋ต์ผ๋ก ๋ณด๋ด์ฃผ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๋ฉด
์ด๋ฐ ์์ผ๋ก ์๋ต์ด ์จ๋ค.
์ดํ์๋ ์ ๋นํ ๋น์ทํ๊ฒ ์ ํธ ํจ์ ๋ง๋ค์ด์ ํธ์ถํ๊ณ ๊ทธ ์๋ต์ ๊ฐ๊ณตํ๋ ์์ผ๋ก ์ฐ๋ฉด ๋๋ค.
# Extract tool call details
tool_call = response.candidates[0].content.parts[0].function_call
print(tool_call)
if tool_call.name == "set_light_values":
result = set_light_values(**tool_call.args)
print(f"Function execution result: {result}")

์ด๋ฐ ์์ผ๋ก ๋ง์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์ดํ์ ์ปจํ ์คํธ๋ฅผ ์ด์ด๊ฐ๊ณ ์ถ๋ค๋ฉด ์ ์๋ต๊ฐ์ ์ด์ฉํด์ ๋ ๋ญ๊ฐ๋ฅผ ํ๋ฉด ๋๋ค.
์ฐธ์กฐ
https://huggingface.co/docs/hugs/guides/function-calling
https://dev.to/fotiecodes/function-calling-vs-model-context-protocol-mcp-what-you-need-to-know-4nbo
https://www.promptingguide.ai/applications/function_calling
https://ai.google.dev/gemini-api/docs/function-calling?hl=ko&example=meeting#python_1