[LLM] Function Call (with Gemini)

[์›๋ณธ ๋งํฌ]

Function Call์€ OpenAI์—์„œ ์ฒ˜์Œ ๊ณต๊ฐœํ•œ LLM ๊ธฐ๋ฐ˜ ๊ฐœ๋ฐœ ๋ฐฉ๋ฒ•๋ก  ์ค‘ ํ•˜๋‚˜๋‹ค.

LLM์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ธฐ๋Šฅ์„ ๊ฐœ๋ฐœํ•˜๋ ค๊ณ  ํ•˜๋‹ค๋ณด๋ฉด, ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์ด ์ž์œ ๋กœ์šด ๋ฌธ์ž์—ด์ด๋ผ์„œ ๊ฐœ๋ฐœ์— ๋‚œํ•ญ์„ ๊ฒช์„ ์ˆ˜ ์žˆ๋‹ค. Function Call์€ ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ๊ตฌ์กฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฒƒ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.




๊ธฐ๋ณธ ๊ฐœ๋…

Function Call์˜ ๊ธฐ๋ณธ ๊ฐœ๋…์€ ๋ณ„๊ฒŒ ์—†๋‹ค.
LLM์˜ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๊ตฌ์กฐํ™”๋œ ํ˜•ํƒœ๋กœ ์†Œํ†ตํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

Flow๋Š” ๋Œ€๋žต ์ด๋ ‡๋‹ค.

  1. Function Call๋กœ ํ•จ์ˆ˜๋“ค์„ ์ •์˜ํ•ด์„œ LLM์— ์š”์ฒญํ•˜๋ฉด
  2. LLM์ด ๋ฌธ๋งฅ์— ๋งž๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ํƒํ•˜๊ณ , ํ•จ์ˆ˜์˜ ์ •์˜์— ๋งž์ถฐ์„œ ๊ตฌ์กฐํ™”๋œ ํ˜•ํƒœ๋กœ ๊ฐ’์„ ๋„˜๊ฒจ์ค€๋‹ค. ์ด๊ฑธ ํ•จ์ˆ˜์™€ ์ธ์ž๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
  3. ์ด ์ƒํƒœ์—์„œ ์ •๋ณด๊ฐ€ ์ถฉ๋ถ„ํ•˜๋‹ค๋ฉด ์„ ํƒ๋œ ํ•จ์ˆ˜์™€ ์ธ์ž๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋Œ€๋กœ ์“ฐ๊ณ  ๋๋‚ด๊ฑฐ๋‚˜
  4. 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