[Daily morning study] AI Agent์™€ Tool Use ๊ฐœ๋…

#daily morning study

Image


AI Agent๋ž€?

LLM์„ ๋‹จ์ˆœํžˆ ์งˆ๋ฌธ-์‘๋‹ต ๋„๊ตฌ๋กœ ์“ฐ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์Šค์Šค๋กœ ๊ณ„ํš์„ ์„ธ์šฐ๊ณ  ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ–‰๋™ํ•˜๋Š” ์‹œ์Šคํ…œ์„ Agent๋ผ๊ณ  ํ•œ๋‹ค.

์ „ํ†ต์ ์ธ LLM ์‚ฌ์šฉ์€ ์ž…๋ ฅ โ†’ ์ถœ๋ ฅ ํ•œ ๋ฒˆ์œผ๋กœ ๋๋‚œ๋‹ค. Agent๋Š” ์—ฌ๊ธฐ์„œ ๋” ๋‚˜์•„๊ฐ€ ๋‹ค์Œ ๋ฃจํ”„๋ฅผ ๋ˆ๋‹ค.

  1. ๋ชฉํ‘œ๋ฅผ ๋ฐ›๋Š”๋‹ค
  2. ์–ด๋–ค ๋„๊ตฌ(Tool)๋ฅผ ์จ์•ผ ํ• ์ง€ ํŒ๋‹จํ•œ๋‹ค
  3. ๋„๊ตฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๋Š”๋‹ค
  4. ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋‹ค์Œ ํ–‰๋™์„ ๊ฒฐ์ •ํ•œ๋‹ค
  5. ๋ชฉํ‘œ๊ฐ€ ๋‹ฌ์„ฑ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค

Agent์˜ ๊ตฌ์„ฑ ์š”์†Œ

๊ตฌ์„ฑ ์š”์†Œ์—ญํ• 
LLM (Brain)์ถ”๋ก , ๊ณ„ํš, ์˜์‚ฌ๊ฒฐ์ •
Memory์ด์ „ ๋Œ€ํ™”/ํ–‰๋™ ์ด๋ ฅ ์ €์žฅ
Tools์™ธ๋ถ€ API ํ˜ธ์ถœ, ๊ฒ€์ƒ‰, ์ฝ”๋“œ ์‹คํ–‰ ๋“ฑ
Planning๋ชฉํ‘œ๋ฅผ ํ•˜์œ„ ์ž‘์—…์œผ๋กœ ๋ถ„ํ•ด

Memory๋Š” ํฌ๊ฒŒ ๋‘ ์ข…๋ฅ˜๋‹ค.

  • ๋‹จ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ: ํ˜„์žฌ ๋Œ€ํ™” ์ปจํ…์ŠคํŠธ (context window ๋‚ด)
  • ์žฅ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ: ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‚˜ ๋ฒกํ„ฐ ์Šคํ† ์–ด์— ์˜๊ตฌ ์ €์žฅ

Tool Use (Function Calling)

Tool Use๋Š” LLM์ด ์ž์‹ ์ด ์ง์ ‘ ๋‹ต์„ ๋งŒ๋“œ๋Š” ๋Œ€์‹ , ์™ธ๋ถ€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด๋‹ค. OpenAI๋Š” โ€œFunction Callingโ€, Anthropic Claude๋Š” โ€œTool Useโ€๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์ž‘๋™ ์ˆœ์„œ

  1. ์‚ฌ์šฉ์ž๊ฐ€ ์งˆ๋ฌธ์„ ๋ณด๋‚ธ๋‹ค
  2. LLM์ด โ€œ์ด ์งˆ๋ฌธ์€ ์™ธ๋ถ€ ๋„๊ตฌ๊ฐ€ ํ•„์š”ํ•˜๋‹คโ€๊ณ  ํŒ๋‹จํ•œ๋‹ค
  3. LLM์ด ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ, ์–ด๋–ค ์ธ์ž๋กœ ํ˜ธ์ถœํ• ์ง€ JSON ํ˜•ํƒœ๋กœ ์‘๋‹ตํ•œ๋‹ค
  4. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹ค์ œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค
  5. ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ LLM์—๊ฒŒ ๋„˜๊ธด๋‹ค
  6. LLM์ด ์ตœ์ข… ์‘๋‹ต์„ ์ƒ์„ฑํ•œ๋‹ค

Anthropic Tool Use ์˜ˆ์‹œ

import anthropic

client = anthropic.Anthropic()

tools = [
    {
        "name": "get_weather",
        "description": "ํŠน์ • ๋„์‹œ์˜ ํ˜„์žฌ ๋‚ ์”จ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "๋‚ ์”จ๋ฅผ ์กฐํšŒํ•  ๋„์‹œ๋ช…"
                }
            },
            "required": ["city"]
        }
    }
]

response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "์„œ์šธ ๋‚ ์”จ ์•Œ๋ ค์ค˜"}]
)

if response.stop_reason == "tool_use":
    tool_use = next(b for b in response.content if b.type == "tool_use")
    tool_name = tool_use.name    # "get_weather"
    tool_input = tool_use.input  # {"city": "์„œ์šธ"}

    weather_result = call_weather_api(tool_input["city"])

    final_response = client.messages.create(
        model="claude-opus-4-7",
        max_tokens=1024,
        tools=tools,
        messages=[
            {"role": "user", "content": "์„œ์šธ ๋‚ ์”จ ์•Œ๋ ค์ค˜"},
            {"role": "assistant", "content": response.content},
            {
                "role": "user",
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": tool_use.id,
                        "content": weather_result
                    }
                ]
            }
        ]
    )

ReAct ํŒจํ„ด

ReAct = Reasoning + Acting. Agent๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ํ–‰๋™ํ•˜๋Š”์ง€๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๊ตฌ์กฐํ™”ํ•œ ํŒจํ„ด์ด๋‹ค.

Thought: ์‚ฌ์šฉ์ž๊ฐ€ ์„œ์šธ ๋‚ ์”จ๋ฅผ ๋ฌป๊ณ  ์žˆ๋‹ค. get_weather ๋„๊ตฌ๋ฅผ ์จ์•ผ ํ•œ๋‹ค.
Action: get_weather(city="์„œ์šธ")
Observation: {"temperature": 18, "condition": "๋ง‘์Œ"}
Thought: ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•˜๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.
Answer: ์„œ์šธ์˜ ํ˜„์žฌ ๋‚ ์”จ๋Š” ๋ง‘์Œ์ด๊ณ , ๊ธฐ์˜จ์€ 18๋„์ž…๋‹ˆ๋‹ค.

Thought โ†’ Action โ†’ Observation ์‚ฌ์ดํด์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ตœ์ข… ๋‹ต์— ๋„๋‹ฌํ•œ๋‹ค. LLM์ด ์ค‘๊ฐ„ ์ถ”๋ก  ๊ณผ์ •์„ ํ…์ŠคํŠธ๋กœ ์ƒ์„ฑํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ๋ณต์žกํ•œ ๋ฌธ์ œ๋ฅผ ๋‹จ๊ณ„์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.


์ž์ฃผ ์“ฐ์ด๋Š” Tool ์œ ํ˜•

  • ์›น ๊ฒ€์ƒ‰: ์ตœ์‹  ์ •๋ณด ์กฐํšŒ
  • ์ฝ”๋“œ ์‹คํ–‰ (Code Interpreter): Python ์ฝ”๋“œ ์‹คํ–‰ ํ›„ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
  • ํŒŒ์ผ ์‹œ์Šคํ…œ: ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ
  • ์™ธ๋ถ€ API ํ˜ธ์ถœ: ๋‚ ์”จ, ์ฃผ์‹, DB ์ฟผ๋ฆฌ ๋“ฑ
  • ๋ฒกํ„ฐ ์Šคํ† ์–ด ๊ฒ€์ƒ‰: RAG ํŒŒ์ดํ”„๋ผ์ธ์—์„œ ๊ด€๋ จ ๋ฌธ์„œ ๊ฒ€์ƒ‰

Multi-Agent ์‹œ์Šคํ…œ

๋‹จ์ผ Agent ๋Œ€์‹  ์—ฌ๋Ÿฌ Agent๊ฐ€ ํ˜‘๋ ฅํ•˜๋Š” ๊ตฌ์กฐ๋‹ค.

Orchestrator Agent
    โ”œโ”€โ”€ Research Agent   (์›น ๊ฒ€์ƒ‰ ์ „๋‹ด)
    โ”œโ”€โ”€ Coding Agent     (์ฝ”๋“œ ์ž‘์„ฑ ์ „๋‹ด)
    โ””โ”€โ”€ Review Agent     (์ฝ”๋“œ ๋ฆฌ๋ทฐ ์ „๋‹ด)

๊ฐ Agent๊ฐ€ ํŠน์ • ์—ญํ• ์— ์ง‘์ค‘ํ•˜๋ฏ€๋กœ ๋ณต์žกํ•œ ์ž‘์—…์„ ๋ถ„์—… ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ ์€ Agent ๊ฐ„ ํ†ต์‹  ๋น„์šฉ, ์˜ค๋ฅ˜ ์ „ํŒŒ ๊ฐ€๋Šฅ์„ฑ, ๋””๋ฒ„๊น… ์–ด๋ ค์›€์ด๋‹ค.


Agent์˜ ํ•œ๊ณ„

  • Hallucination: Tool์„ ์จ์•ผ ํ•  ์ƒํ™ฉ์—์„œ ๊พธ๋ฉฐ๋‚ธ ๋‹ต์„ ๊ทธ๋ƒฅ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ๋ฌดํ•œ ๋ฃจํ”„: ๋ชฉํ‘œ๋ฅผ ๋‹ฌ์„ฑ ๋ชปํ•˜๋ฉด ๋„๊ตฌ ํ˜ธ์ถœ์„ ๋์—†์ด ๋ฐ˜๋ณตํ•œ๋‹ค
  • ๋น„์šฉ: Tool ํ˜ธ์ถœ๋งˆ๋‹ค LLM API๊ฐ€ ํ•œ ๋ฒˆ ๋” ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ํ† ํฐ ๋น„์šฉ์ด ๋ˆ„์ ๋œ๋‹ค
  • ๋ณด์•ˆ: ์™ธ๋ถ€ API๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•  ๋•Œ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค

์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ๋Œ€ ๋ฐ˜๋ณต ํšŸ์ˆ˜ ์ œํ•œ(max_iterations), Tool ์ ‘๊ทผ ๊ถŒํ•œ ์ œํ•œ, Human-in-the-loop(์ค‘๊ฐ„ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ๋žŒ์ด ๊ฒ€ํ† ํ•˜๋Š” ๋‹จ๊ณ„) ๊ฐ™์€ ์•ˆ์ „์žฅ์น˜๋ฅผ ๊ฑด๋‹ค.