[Daily morning study] AI Agent์ Tool Use ๊ฐ๋
#daily morning study
AI Agent๋?
LLM์ ๋จ์ํ ์ง๋ฌธ-์๋ต ๋๊ตฌ๋ก ์ฐ๋ ๊ฒ ์๋๋ผ, ๋ชฉํ๋ฅผ ๋ฌ์ฑํ๊ธฐ ์ํด ์ค์ค๋ก ๊ณํ์ ์ธ์ฐ๊ณ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ฉฐ ๋ฐ๋ณต์ ์ผ๋ก ํ๋ํ๋ ์์คํ ์ Agent๋ผ๊ณ ํ๋ค.
์ ํต์ ์ธ LLM ์ฌ์ฉ์ ์ ๋ ฅ โ ์ถ๋ ฅ ํ ๋ฒ์ผ๋ก ๋๋๋ค. Agent๋ ์ฌ๊ธฐ์ ๋ ๋์๊ฐ ๋ค์ ๋ฃจํ๋ฅผ ๋๋ค.
- ๋ชฉํ๋ฅผ ๋ฐ๋๋ค
- ์ด๋ค ๋๊ตฌ(Tool)๋ฅผ ์จ์ผ ํ ์ง ํ๋จํ๋ค
- ๋๊ตฌ๋ฅผ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋๋ค
- ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ๋ค์ ํ๋์ ๊ฒฐ์ ํ๋ค
- ๋ชฉํ๊ฐ ๋ฌ์ฑ๋ ๋๊น์ง ๋ฐ๋ณตํ๋ค
Agent์ ๊ตฌ์ฑ ์์
| ๊ตฌ์ฑ ์์ | ์ญํ |
|---|---|
| LLM (Brain) | ์ถ๋ก , ๊ณํ, ์์ฌ๊ฒฐ์ |
| Memory | ์ด์ ๋ํ/ํ๋ ์ด๋ ฅ ์ ์ฅ |
| Tools | ์ธ๋ถ API ํธ์ถ, ๊ฒ์, ์ฝ๋ ์คํ ๋ฑ |
| Planning | ๋ชฉํ๋ฅผ ํ์ ์์ ์ผ๋ก ๋ถํด |
Memory๋ ํฌ๊ฒ ๋ ์ข ๋ฅ๋ค.
- ๋จ๊ธฐ ๋ฉ๋ชจ๋ฆฌ: ํ์ฌ ๋ํ ์ปจํ ์คํธ (context window ๋ด)
- ์ฅ๊ธฐ ๋ฉ๋ชจ๋ฆฌ: ์ธ๋ถ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๋ฒกํฐ ์คํ ์ด์ ์๊ตฌ ์ ์ฅ
Tool Use (Function Calling)
Tool Use๋ LLM์ด ์์ ์ด ์ง์ ๋ต์ ๋ง๋๋ ๋์ , ์ธ๋ถ ํจ์๋ฅผ ํธ์ถํด์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์ปค๋์ฆ์ด๋ค. OpenAI๋ โFunction Callingโ, Anthropic Claude๋ โTool Useโ๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์๋ ์์
- ์ฌ์ฉ์๊ฐ ์ง๋ฌธ์ ๋ณด๋ธ๋ค
- LLM์ด โ์ด ์ง๋ฌธ์ ์ธ๋ถ ๋๊ตฌ๊ฐ ํ์ํ๋คโ๊ณ ํ๋จํ๋ค
- LLM์ด ์ด๋ค ํจ์๋ฅผ, ์ด๋ค ์ธ์๋ก ํธ์ถํ ์ง JSON ํํ๋ก ์๋ตํ๋ค
- ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ค์ ํจ์๋ฅผ ์คํํ๋ค
- ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ LLM์๊ฒ ๋๊ธด๋ค
- 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(์ค๊ฐ ๊ฒฐ๊ณผ๋ฅผ ์ฌ๋์ด ๊ฒํ ํ๋ ๋จ๊ณ) ๊ฐ์ ์์ ์ฅ์น๋ฅผ ๊ฑด๋ค.