[Daily morning study] AI Agent์ Tool Use ๊ฐ๋
#daily morning study
AI Agent๋
AI Agent๋ ๋จ์ํ ์ง๋ฌธ์ ๋ตํ๋ ๊ฒ์ ๋์ด, ๋ชฉํ๋ฅผ ๋ฌ์ฑํ๊ธฐ ์ํด ์ค์ค๋ก ๊ณํ์ ์ธ์ฐ๊ณ ํ๋(action)์ ์ํํ๋ ์์คํ ์ด๋ค.
์ผ๋ฐ LLM ํธ์ถ๊ณผ์ ์ฐจ์ด:
| ๊ตฌ๋ถ | ์ผ๋ฐ LLM | AI Agent |
|---|---|---|
| ์ ๋ ฅ | ํ๋กฌํํธ | ๋ชฉํ(goal) |
| ์ถ๋ ฅ | ํ ์คํธ ์๋ต | ํ๋ ๊ฒฐ๊ณผ |
| ๋ฐ๋ณต | ๋จ๋ฐ์ฑ | ๋ฃจํ(loop) ๋ฐ๋ณต |
| ๋๊ตฌ ์ฌ์ฉ | ๋ถ๊ฐ | ๊ฐ๋ฅ (Tool Use) |
Agent๋ โ์๊ฐ โ ํ๋ โ ๊ด์ฐฐ โ ๋ค์ ์๊ฐโ ์ ์ฌ์ดํด์ ๋ฐ๋ณตํ๋ฉด์ ๋ชฉํ์ ๋๋ฌํ๋ค.
ReAct ํจํด
Agent์ ๋ํ์ ์ธ ๋์ ๋ฐฉ์์ด ReAct(Reasoning + Acting) ํจํด์ด๋ค.
Thought: ํ์ฌ ์ํฉ์ ๋ถ์ํ๊ณ ๋ฌด์์ ํด์ผ ํ ์ง ํ๋จ
Action: ๋๊ตฌ๋ฅผ ํธ์ถํ๊ฑฐ๋ ํ๋ ์ํ
Observation: ํ๋ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ
Thought: ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ๋ค์ ๋จ๊ณ ํ๋จ
...
Answer: ์ต์ข
๋ต ๋์ถ
์์ โ โ์์ธ ๋ ์จ ์๋ ค์คโ๋ฅผ ์ฒ๋ฆฌํ๋ Agent:
Thought: ํ์ฌ ์์ธ ๋ ์จ๋ฅผ ๋ชจ๋ฅด๋ ๋ ์จ API๋ฅผ ํธ์ถํด์ผ๊ฒ ๋ค
Action: weather_tool(city="Seoul")
Observation: {"temp": 28, "condition": "๋ง์"}
Thought: ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์์ผ๋ ๋ต๋ณ์ ์์ฑํ ์ ์๋ค
Answer: ์์ธ์ ํ์ฌ 28๋์ด๋ฉฐ ๋ง์ต๋๋ค
Tool Use (๋๊ตฌ ์ฌ์ฉ)
Tool Use๋ LLM์ด ์ธ๋ถ ํจ์๋ API๋ฅผ ํธ์ถํ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฅ์ด๋ค.
๋ชจ๋ธ์ ํ ์คํธ๋ฅผ ์ง์ ์์ฑํ๋ ๋์ , โ์ด ๋๊ตฌ๋ฅผ ์ด ์ธ์๋ก ํธ์ถํ๋ผโ๋ ๊ตฌ์กฐํ๋ ์ถ๋ ฅ์ ๋ด๋ณด๋ธ๋ค. ๊ทธ๋ฌ๋ฉด ์ฝ๋๊ฐ ์ค์ ๋ก ํจ์๋ฅผ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ๋ชจ๋ธ์ ๋๊ธด๋ค.
๊ธฐ๋ณธ ํ๋ฆ
1. ์ฌ์ฉ์ ๋ฉ์์ง + ๋๊ตฌ ๋ชฉ๋ก โ LLM
2. LLM์ด ๋๊ตฌ ํธ์ถ ๊ฒฐ์ โ tool_call ์ถ๋ ฅ
3. ํธ์คํธ ์ฝ๋๊ฐ ์ค์ ํจ์ ์คํ
4. ์คํ ๊ฒฐ๊ณผ โ LLM์ ๋ค์ ์ ๋ฌ
5. LLM์ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ์ต์ข
์๋ต ์์ฑ
๋๊ตฌ ์ ์ ์์ (Anthropic API)
{
"name": "get_weather",
"description": "ํน์ ๋์์ ํ์ฌ ๋ ์จ๋ฅผ ๊ฐ์ ธ์จ๋ค",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "๋ ์จ๋ฅผ ์กฐํํ ๋์ ์ด๋ฆ"
}
},
"required": ["city"]
}
}
๋ชจ๋ธ์ด ๋๊ตฌ๋ฅผ ์ ํํ๋ ๊ธฐ์ค
LLM์ ์ง๋ฌธ์ ์๋์ ๋๊ตฌ์ description์ ๋งค์นญํด์ ํธ์ถ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ค. ๊ทธ๋์ description์ ์ ์์ฑํ๋ ๊ฒ ์ค์ํ๋ค.
Agent์ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ
Agent๊ฐ ๋ณต์กํ ์์ ์ ์ฒ๋ฆฌํ๋ ค๋ฉด ์ํ(state)๋ฅผ ๊ธฐ์ตํด์ผ ํ๋ค.
| ๋ฉ๋ชจ๋ฆฌ ์ข ๋ฅ | ์ค๋ช | ์์ |
|---|---|---|
| In-context memory | LLM์ ์ปจํ ์คํธ ์๋์ฐ ์์ ์๋ ์ ๋ณด | ๋ํ ํ์คํ ๋ฆฌ |
| External memory | ์ธ๋ถ DB๋ ๋ฒกํฐ ์คํ ์ด | RAG ์์คํ |
| Episodic memory | ๊ณผ๊ฑฐ ์ํผ์๋ ๊ธฐ๋ก | โ์ง๋๋ฒ์ A๋ฅผ ํ์ผ๋โฆโ |
| Procedural memory | ์ด๋ป๊ฒ ํ๋ํ ์ง์ ๋ํ ์ง์ | ์์คํ ํ๋กฌํํธ |
๋จ๊ธฐ ์์ ์ ์ปจํ ์คํธ ์๋์ฐ๋ง์ผ๋ก ์ฒ๋ฆฌ ๊ฐ๋ฅํ์ง๋ง, ์ฅ๊ธฐ ์์ ์ด๋ ๋์ฉ๋ ๋ฐ์ดํฐ๋ ์ธ๋ถ ๋ฉ๋ชจ๋ฆฌ(๋ฒกํฐ DB ๋ฑ)๋ฅผ ๋ถ์ฌ์ผ ํ๋ค.
๋ฉํฐ ์์ด์ ํธ ์์คํ
๋ณต์กํ ์์ ์ ํ๋์ Agent๊ฐ ๋ชจ๋ ์ฒ๋ฆฌํ๋ ๋์ ์ฌ๋ฌ Agent๋ฅผ ์กฐํฉํ๋ ๋ฐฉ์์ผ๋ก ํด๊ฒฐํ๋ค.
Orchestrator Agent
โโโ Research Agent (๊ฒ์ ๋ด๋น)
โโโ Code Agent (์ฝ๋ ์์ฑ ๋ด๋น)
โโโ Review Agent (๊ฒํ ๋ด๋น)
๊ตฌ์ฑ ํจํด
- Sequential: Agent A์ ์ถ๋ ฅ์ด Agent B์ ์ ๋ ฅ์ผ๋ก ์ ๋ฌ (ํ์ดํ๋ผ์ธ)
- Parallel: ์ฌ๋ฌ Agent๊ฐ ๋์์ ๋ณ๋ ฌ ์คํ
- Hierarchical: Orchestrator๊ฐ ํ์ Agent๋ค์ ์งํ
๋ฉํฐ ์์ด์ ํธ๋ ์ญํ ๋ถ๋ฆฌ๋ก ๊ฐ Agent๊ฐ ๋ ์ง์ค๋ ์์ ์ ์ํํ ์ ์๊ณ , ์ถ๋ ฅ ํ์ง์ด ์ฌ๋ผ๊ฐ๋ค. ๋ฐ๋ฉด Agent ๊ฐ ํต์ ๋น์ฉ๊ณผ ์ค๋ฅ ์ ํ ์ํ์ด ์๋ค.
์ค์ ๊ตฌํ ์์ (Python, Anthropic SDK)
import anthropic
import json
client = anthropic.Anthropic()
tools = [
{
"name": "get_stock_price",
"description": "ํน์ ์ฃผ์์ ํ์ฌ ๊ฐ๊ฒฉ์ ๊ฐ์ ธ์จ๋ค",
"input_schema": {
"type": "object",
"properties": {
"ticker": {"type": "string", "description": "์ฃผ์ ํฐ์ปค ์ฌ๋ณผ (์: AAPL)"}
},
"required": ["ticker"]
}
}
]
def get_stock_price(ticker: str) -> dict:
# ์ค์ ๋ก๋ ์ธ๋ถ API ํธ์ถ
prices = {"AAPL": 189.5, "GOOGL": 175.2}
return {"ticker": ticker, "price": prices.get(ticker, 0)}
messages = [{"role": "user", "content": "์ ํ ์ฃผ์ ํ์ฌ๊ฐ๊ฐ ์ผ๋ง์ผ?"}]
while True:
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
messages=messages
)
if response.stop_reason == "end_turn":
print(response.content[0].text)
break
# ๋๊ตฌ ํธ์ถ ์ฒ๋ฆฌ
tool_use = next(b for b in response.content if b.type == "tool_use")
result = get_stock_price(**tool_use.input)
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": json.dumps(result)
}]
})
Agent ์ค๊ณ ์ ๊ณ ๋ ค์ฌํญ
๋ฃจํ ์ข ๋ฃ ์กฐ๊ฑด์ ๋ช ํํ ์ ์ํด์ผ ํ๋ค. ๋ฌดํ ๋ฃจํ์ ๋น ์ง์ง ์๋๋ก ์ต๋ ๋ฐ๋ณต ํ์๋ ํ์์์์ ์ค์ ํ๋ค.
์ค๋ฅ ์ฒ๋ฆฌ: ๋๊ตฌ ํธ์ถ์ด ์คํจํ์ ๋ Agent๊ฐ ์ด๋ป๊ฒ ๋์ํ ์ง ๊ฒฐ์ ํด์ผ ํ๋ค. ์ฌ์๋, ๋์ ๋๊ตฌ ์ฌ์ฉ, ์๋ฌ ๋ฉ์์ง ๋ฐํ ๋ฑ.
ํ ๋ฃจ์๋ค์ด์ ๋ฐฉ์ง: Agent๊ฐ ๋๊ตฌ๋ฅผ ํธ์ถํ์ง ์๊ณ ๊ฐ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋ด๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๋๊ตฌ ๊ฒฐ๊ณผ ์์ด๋ ํ์ ์ ์ธ ๋ต์ ๋ด๋ฆฌ์ง ์๋๋ก ํ๋กฌํํธ ์ค๊ณ๊ฐ ํ์ํ๋ค.
๋น์ฉ ๊ด๋ฆฌ: Agent ๋ฃจํ๊ฐ ๊ธธ์ด์ง์๋ก API ํธ์ถ ํ์์ ํ ํฐ์ด ๋์ด๋๋ค. ๋จ๊ณ๋ณ ๋น์ฉ ์ถ์ ์ด ํ์ํ๋ค.