[LLM Agents] Multi AI Agent System with crewAI
Multi AI Agent System with crewAI
CrewAI Framework๋ฅผ ํตํด Multi-Agent ์์ฑํ๊ณ ๋ค์ค Task๋ฅผ Agent๋ค๊ณผ ์ํธ ์ฐ๊ณํด ๋ชฉํ์ ๊ฐ๊น์ด ๋ต์ ๊ตฌํ๋ค.
2. AI Agents
(1) Agents ์ญํ
agent๊ฐ task๋ฅผ ์ํํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ answer๋ก ๋ฐํํ๋ค. ๊ฒฐ๊ณผ๊ฐ ์ ๋๋ก ๋์ง ์์ ๋ agent๋ ๋ค์ task๋ฅผ ์ํํ ์ ์๋ค.
(2) Multi-Agent Systems
- concept(task)๋ฅผ ๊ฐ๋จํ ๊ตฌ์กฐ๋ก ๋ถํด
- system(prompt)๋ฅผ ํจ๊ป ์ฌ์ฉํ ํจํด ์ ๊ณต
- ์์ด์ ํธ๊ฐ ์ฌ์ฉํ ์ ์๋ ๋๊ตฌ / ๊ธฐ์ ์ ๊ณต
- ์์ด์ ํธ๋ฅผ ์ํด ์ฌ์ฉํ ์ค๋น๊ฐ ๋ ๋๊ตฌ / ๊ธฐ์ ์ ๊ณต
- ์์ด์ ํธ๋ฅผ ์ํด ์ฌ์ฉํ ์ ์๋ ๋ชจ๋ธ ์ ๊ณต
- ์์ด์ ํธ๋ฅผ ์ ์ํ๊ธฐ ์ํ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ์ ์ํ๋ ๋ชจ๋ธ ์ ๊ณต
3. Create agents to research and write an article
L2 : ๋ฆฌ์์น ๋ฐ ๊ธฐ์ ๋ธ๋ก๊ทธ ์์ฑ agent ์์ฑ
๋ฉํฐ ์์ด์ ํธ ์์คํ ์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ์๊ฐํ๊ณ crewAI ํ๋ ์์ํฌ์ ๋ํ ๊ฐ์๋ฅผ ์ป๊ฒ ๋ฉ๋๋ค.
#!pip install -q crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29
# Warning control
import warnings
warnings.filterwarnings('ignore')
# import
from crewai import Agent, Task, Crew
LLM agents๋ OpenAI์ gpt-3.5-turbo๋ฅผ ์ฌ์ฉํฉ๋๋ค. crewAI๋ ๋ค๋ฅธ ์ธ๊ธฐ ์๋ LLM ๋ชจ๋ธ์ agent๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค
Optional Note
1. [Hugging Face (HuggingFaceHub endpoint)]
from langchain_community.llms import HuggingFaceHub
llm = HuggingFaceHub(
repo_id="HuggingFaceH4/zephyr-7b-beta",
huggingfacehub_api_token="<HF_TOKEN_HERE>",
task="text-generation",
)
- [Mistral API]
OPENAI_API_KEY=your-mistral-api-key
OPENAI_API_BASE=https://api.mistral.ai/v1
OPENAI_MODEL_NAME="mistral-small"
- [Cohere]
from langchain_community.chat_models import ChatCohere
# Initialize language model
os.environ["COHERE_API_KEY"] = "your-cohere-api-key"
llm = ChatCohere()
Agents ์์ฑํ๊ธฐยถ
- ์์ด์ ํธ๋ฅผ ์ ์ํ๊ณ
role
,goal
,backstory
๋ฅผ ์ ๊ณตํฉ๋๋ค. - LLMs๋ ์ญํ ์ ํ ๋ ๋ ์ ์ํ๋๋ค๋ ๊ฒ์ ์ ์ ์์์ต๋๋ค.
Agent: Plannerยถ
Note: _multiple strings_์ ์ฌ์ฉํ๋ ๊ฒ๊ณผ _triple quote docstring_์ ์ฌ์ฉํ๋ ๊ฒ์ ์ฐจ์ด์ ์, whitespaces์ newline characters๋ฅผ ์ถ๊ฐํ์ง ์์๋ ๋๊ธฐ ๋๋ฌธ์ LLM์ ์ ๋ฌํ๊ธฐ์ ๋ ์ ํฌ๋งทํ ๋จ.
varname = "line 1 of text"
"line 2 of text"
versus the triple quote docstring:
varname = """line 1 of text
line 2 of text
"""
planner = Agent(
role="Content Planner",
goal="{topic}์ ๋ํ ์ฌ์ค์ ์ด๊ณ ๋งค๋ ฅ์ ์ธ ์ฝํ
์ธ ๋ฅผ ๊ณํํฉ๋๋ค.",
backstory="๋๋ ๋ธ๋ก๊ทธ ๊ธ์ ๊ณํํ๊ณ ์์ต๋๋ค. "
"์ฃผ์ ๋ {topic}์
๋๋ค. "
"๊ด๊ฐ์ด ๋ฌด์ธ๊ฐ๋ฅผ ๋ฐฐ์ฐ๊ณ "
"์ ๋ณด์ ๊ธฐ๋ฐํ ๊ฒฐ์ ์ ๋ด๋ฆด ์ ์๋๋ก "
"๋์์ฃผ๋ ์ ๋ณด๋ฅผ ์์งํฉ๋๋ค. "
"๋์ ์์
์ ์ด ์ฃผ์ ์ ๋ํ ๊ธ์ ์ฐ๋ "
"์ฝํ
์ธ ์๊ฐ์ ๊ธฐ๋ฐ์ด ๋ฉ๋๋ค.",
allow_delegation=False,
verbose=True
)
#### Agent: Writer
writer = Agent(
role="์ปจํ
์ธ ์๊ฐ",
goal="ํต์ฐฐ๋ ฅ ์๊ณ ์ฌ์ค์ ์ธ ๊ธ์ ์ฐ์ธ์."
"opinion piece {topic}์ ๋ํ ์๊ฒฌ์ ์ฐ์ธ์. ",
backstory="๋๋ ์๊ฐ๋ก์ ๊ธ์ ์ฐ๊ณ ์์ต๋๋ค. "
"์๋ก์ด opinion piece๋ฅผ ์ฃผ์ ๋ก ์ฐ์ธ์: {topic}. "
"๋๋ ์ปจํ
์ธ ํ๋๋์ ์์
์ ๊ธฐ๋ฐ์ผ๋ก ๊ธ์ ์ฐ๊ณ ์์ต๋๋ค. "
"๋๋ ์์๋ผ์ธ์ ์ฃผ์ ๋ชฉํ์ ๋ฐฉํฅ์ ๋ฐ๋ฅด๋ฉฐ, "
"์ปจํ
์ธ ํ๋๋๊ฐ ์ ๊ณตํ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก "
"๊ฐ๊ด์ ์ด๊ณ ์ค๋ฆฝ์ ์ธ ํต์ฐฐ๋ ฅ์ ์ ๊ณตํฉ๋๋ค. "
"๋๋ ์ฃผ์ฅ์ด ์๋ ๊ฐ๊ด์ ์ธ ์ฃผ์ฅ์ ์ ๊ณตํ๊ณ , "
"์ปจํ
์ธ ํ๋๋๊ฐ ์ ๊ณตํ ์ ๋ณด๋ก ๊ทธ๊ฒ์ ๋ท๋ฐ์นจํฉ๋๋ค. "
"๋๋ ์ฃผ์ฅ์ด ๊ฐ๊ด์ ์ธ ์ฃผ์ฅ๊ณผ ๋์กฐ๋๋ ๊ฒฝ์ฐ "
"๋์ ์๊ฒฌ์ ์ธ์ ํฉ๋๋ค.",
allow_delegation=False,
verbose=True
)
#### Agent : Editor
editor = Agent(
role="ํธ์ง์",
goal="ํธ์ง์๋ ์ฃผ์ด์ง ๋ธ๋ก๊ทธ ๊ธ์ "
"๊ธ์ฐ๊ธฐ ์คํ์ผ์ ๋ง๊ฒ ํธ์งํฉ๋๋ค.",
backstory="๋๋ ์ปจํ
์ธ ์๊ฐ๋ก๋ถํฐ ๋ธ๋ก๊ทธ ๊ธ์ ๋ฐ๋ ํธ์ง์์
๋๋ค. "
"๋์ ๋ชฉํ๋ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ๊ฒํ ํ์ฌ "
"์ ๋๋ฆฌ์ฆ์ ์ต๊ณ ์ ์ค์ฒ ๋ฐฉ๋ฒ์ ๋ฐ๋ฅด๊ณ , "
"์๊ฒฌ์ด๋ ์ฃผ์ฅ์ ์ ๊ณตํ ๋ ๊ท ํ์๋ ๊ด์ ์ ์ ๊ณตํ๋ฉฐ, "
"๊ฐ๋ฅํ ๊ฒฝ์ฐ ์ฃผ์ ๋
ผ๋์ด๋๋ ์ฃผ์ ๋ ์๊ฒฌ์ ํผํ๋ ๊ฒ์
๋๋ค.",
allow_delegation=False,
verbose=True
)
#### Agent : Translator
translator = Agent(
role="๋ฒ์ญ๊ฐ",
goal="๋ฒ์ญ๊ฐ๋ ์ฃผ์ด์ง ๊ธ์ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฒ์ญํฉ๋๋ค.",
backstory="๋๋ ํธ์ง์๋ก๋ถํฐ ๋ธ๋ก๊ทธ ๊ธ์ ๋ฐ๋ ๋ฒ์ญ๊ฐ์
๋๋ค. "
"๋์ ๋ชฉํ๋ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ฅผ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฒ์ญํ๋ ๊ฒ์
๋๋ค. "
"๋ฒ์ญ์ ์๋ณธ์ ์๋ฏธ๋ฅผ ์ต๋ํ ์ ์งํ๋ฉด์ "
"๋
์๊ฐ ์ฝ๊ฒ ์ดํดํ ์ ์๋๋ก ํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.",
allow_delegation=False,
verbose=True
)
Task ์์ฑ
- Task๋ฅผ ์ ์ํ๊ณ ,
description
,expected_output
๊ทธ๋ฆฌ๊ณagent
๋ฅผ ์ ๊ณตํ์ธ์.
#### Task: Plan
plan = Task(
description=(
"1. {topic}์ ๋ํ ์ต์ ํธ๋ ๋, ์ฃผ์ ํ๋ ์ด์ด ๋ฐ ์ฃผ๋ชฉํ ๋งํ ๋ด์ค๋ฅผ ์ฐ์ ์์๋ก ์ง์ ํฉ๋๋ค.\n"
"2. ๊ด๊ฐ์ ์๋ณํ๊ณ , ๊ทธ๋ค์ ๊ด์ฌ์ฌ์ ๊ณ ํต ํฌ์ธํธ๋ฅผ ๊ณ ๋ คํฉ๋๋ค.\n"
"3. ์๊ฐ, ์ฃผ์ ํฌ์ธํธ ๋ฐ ํ๋ ์์ฒญ์ ํฌํจํ ์์ธํ ์ฝํ
์ธ ๊ฐ์๋ฅผ ๊ฐ๋ฐํฉ๋๋ค.\n"
"4. SEO ํค์๋ ๋ฐ ๊ด๋ จ ๋ฐ์ดํฐ ๋๋ ์์ค๋ฅผ ํฌํจํฉ๋๋ค."
),
expected_output="์ดํดํ๊ธฐ ์ฝ๊ณ ํจ๊ณผ์ ์ธ ๊ธ์ ์์ฑํ๊ธฐ ์ํ ๊ณํ์๋ฅผ ์ ๊ณตํฉ๋๋ค. "
"์์๋ผ์ธ, ๊ด๊ฐ ๋ถ์, SEO ํค์๋ ๋ฐ ๋ฆฌ์์ค๋ฅผ ํฌํจํฉ๋๋ค.",
agent=planner,
)
#### Task : Write
write = Task(
description=(
"1. {topic}์ ๋ํ ๋งค๋ ฅ์ ์ธ ๋ธ๋ก๊ทธ ๊ธ์ ์์ฑํ์ธ์.\n"
"2. SEO ํค์๋๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํตํฉํ์ธ์.\n"
"3. ์น์
/๋ถ์ ๋ชฉ์ ๋งค๋ ฅ์ ์ธ ๋ฐฉ์์ผ๋ก "
"์ ์ ํ๊ฒ ๋ช
๋ช
๋์ด ์์ต๋๋ค.\n"
"4. ๊ธ์ ๋งค๋ ฅ์ ์ธ ์๊ฐ, ํต์ฐฐ๋ ฅ์๋ ๋ณธ๋ฌธ, "
"์์ฝ์ ์ธ ๊ฒฐ๋ก ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.\n"
"5. ๋ง์ถค๋ฒ ์ค๋ฅ ๋ฐ ๋ธ๋๋ ์์ฑ๊ณผ ์ผ์นํ๋์ง "
"๊ฒํ ํ์ธ์."
),
expected_output="markdown ํ์์ผ๋ก ์์ฑ๋ ์ ์ฐ์ฌ์ง ๋ธ๋ก๊ทธ ๊ธ์ ์ ๊ณตํฉ๋๋ค. ",
agent=writer,
)
#### Task : Edit
edit = Task(
description=("์ฃผ์ด์ง ๋ธ๋ก๊ทธ ๊ธ์ ๊ต์ ํ์ฌ "
"๋ฌธ๋ฒ ์ค๋ฅ๋ฅผ ํ์ธํ๊ณ "
"๋ธ๋๋ ์๊ฒฌ๊ณผ ์ผ์นํ๋์ง ํ์ธํ์ธ์."),
expected_output="markdown ํ์์ ์ ์ฐ์ฌ์ง ๋ธ๋ก๊ทธ ๊ธ, "
"๊ฐ ์น์
์ 2~3 ๋จ๋ฝ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.",
agent=editor
)
#### Task : Translate
translate = Task(
description=("์ฃผ์ด์ง ๋ธ๋ก๊ทธ ๊ธ์ ํ๊ธ๋ก ๋ฒ์ญํฉ๋๋ค."
"๊ตฌ์ด์ฒด์ ๊ฒฝ์ด์ฒด๋ฅผ ์์ด์ ๋ฒ์ญํ์ธ์."),
expected_output="ํ๊ตญ์ด๋ก ๋ฒ์ญ๋ ๋ธ๋ก๊ทธ ๊ธ์ ์ ๊ณตํฉ๋๋ค.",
agent=translator
)
Crew ์์ฑํ๊ธฐ
- crew Agents ์์ฑํ๊ธฐ
- ์์ด์ ํธ๊ฐ ์ํํ ์์
์ ์ ๋ฌํฉ๋๋ค.
- ์ฐธ๊ณ : ์ด ๊ฐ๋จํ ์์ ์์, ์์ ์ ์์ฐจ์ ์ผ๋ก ์ํ๋ฉ๋๋ค(์ฆ, ์๋ก ์์กด์ ์ด๋ฏ๋ก ์์ ๋ชฉ๋ก์ _order_๊ฐ ์ค์ํฉ๋๋ค).
verbose=2
๋ ์คํ์ ๋ชจ๋ ๋ก๊ทธ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
crew = Crew(
agents=[planner, writer, editor, translator],
tasks=[plan, write, edit, translate],
verbose=2
)
Crew ์คํํ๊ธฐยถ
result = crew.kickoff(inputs={"topic": "Artificial Intelligence"})
from IPython.display import Markdown
Markdown(result)
AI์ ํ๋๋ฅผ ํ๋ฉฐ: ํ์ฌ์ ์ถ์ธ์ ์ฃผ์ ํ๋ ์ด์ด๋คยถ
AI๋ ๊ฑด๊ฐ๊ด๋ฆฌ, ์ฌ์ด๋ฒ ๋ณด์, ๊ธ์ต ๋ฑ์์ ์ฃผ์ ์ํ์๊ฐ ๋๋ฉฐ ์ค์ํ ๋ฐ์ ์ ์ด๋ฃจ๊ณ ์์ต๋๋ค. ๊ฑด๊ฐ๊ด๋ฆฌ ๋ถ์ผ์์ AI๋ ์ง๋จ๊ณผ ํ์ ๊ด๋ฆฌ๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ๋ง๋ค๊ณ ์์ต๋๋ค. ๊ธ์ต ๋ถ์ผ์์๋ AI ์๊ณ ๋ฆฌ์ฆ์ด ๊ฑฐ๋ ์ ๋ต๊ณผ ์ํ ๊ด๋ฆฌ๋ฅผ ์ฌ์กฐ์งํ๊ณ ์์ต๋๋ค. ์ฌ์ด๋ฒ ๋ณด์ ๋ถ์ผ์์๋ AI๊ฐ ์ค์๊ฐ์ผ๋ก ์ํ์ ๊ฐ์งํ๊ณ ์ํํ๋ ๊ฐ๋ ฅํ ํ์ด ๋๊ณ ์์ต๋๋ค.
AI ํ๋ช ์ ์ฃผ๋ํ๊ณ ์๋ ์ฃผ์ ํ๋ ์ด์ด๋ค๋ก๋ Google, IBM, Microsoft, Amazon์ด ์์ต๋๋ค. ๊ทธ๋ค์ ๊ด๋ฒ์ํ ์ฐ๊ตฌ์ AI์ ํ์ ์ ์ธ ์์ฉ์ ๊ฒฝ๊ณ๋ฅผ ๋ฐ์ด๋๊ณ ์์ต๋๋ค. ์ต๊ทผ์ ์ฃผ๋ชฉํ ๋งํ ์ฌ๋ก๋ก๋ ๊ตฌ๊ธ์ AI ํ๋ก๊ทธ๋จ์ธ AlphaGo๊ฐ ๋ฐ๋์์ ์ธ๊ฐ์ ๋ฅ๊ฐํ ๊ฒ์ด ์์ต๋๋ค. ์ด๋ ์ด์ ์๋ ๋ถ๊ฐ๋ฅํ๋ค๊ณ ์ฌ๊ฒจ์ก๋ ์ผ์ ๋๋ค.
AI์ ํจ๊ป ๋ฏธ๋๋ฅผ ํ์ด๋๊ฐ ๊ฒยถ
AI์ ํ์ฌ ์์ฉ ํ๋ก๊ทธ๋จ์ ์ธ์์ ์ด์ง๋ง, ๊ทธ ๋ฏธ๋์ ๊ฐ๋ฅ์ฑ์ ๋ ๋๋ผ์ธ ๊ฒ์ ๋๋ค. AI์ ๋น ๋ฅธ ๋ฐ์ ์ ๊ธฐ๊ณ๊ฐ ์ค์ค๋ก ํ์ตํ๊ณ , ์ ์ํ๋ฉฐ, ๋ ์์ ์ผ๋ก ๊ฒฐ์ ์ ๋ด๋ฆด ์ ์๋ ๋ฏธ๋๋ฅผ ์ฝ์ํ๊ณ ์์ต๋๋ค. ๊ทธ๋ฌ๋, AI์ ๋ณต์ก์ฑ๊ณผ ๊ฐ์ธ ์ ๋ณด ๋ณดํธ์ ๋ณด์๊ณผ ๊ด๋ จ๋ ์ฐ๋ ค๋ ์ฌ์ ํ ์ ํจํ ๋ฌธ์ ์ ์ ๋๋ค. AI ํ๊ฒฝ์ ํ์ํ๋ฉด์, AI์ ์ ์ฌ๋ ฅ์ ํ์ฉํ๊ณ ์ด๋ฌํ ๋์ ์ ๊ทน๋ณตํ๋ ์ฌ์ด์ ๊ท ํ์ ์ก๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
ํ๋์ ์ด๊ตฌํ๋คยถ
๊ณ์ํด์ ์งํํ๋ AI ๋ถ์ผ์ ๋ํด ์์๊ฐ์ธ์. AI์ ๋ฏธ๋๋ ์๋ก์ด ์ถ์ธ, ์์ฉ ํ๋ก๊ทธ๋จ, ๋ํ๊ตฌ๊ฐ ์ ๊ธฐ์ ์ผ๋ก ๋ฑ์ฅํ๋ฉฐ, ์์ฒญ๋ ๊ฐ๋ฅ์ฑ์ ์๊ณ ์์ต๋๋ค. ๊ธฐ์ ์ ํธ๊ฐ, AI ์ฐ๊ตฌ์, ๋น์ฆ๋์ค ์ ๋ฌธ๊ฐ๋ก์, ์ฌ๋ฌ๋ถ์ ํต์ฐฐ๋ ฅ๊ณผ ๊ฒฝํ์ AI ๋ํ๋ฅผ ํ์๋กญ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
๊ธฐ์ตํ์ธ์, ์ธ๊ณต์ง๋ฅ์ ๋จ์ํ ์ถ์ธ๊ฐ ์๋๋๋ค. ๊ทธ๊ฒ์ ํ๋ช ์ ๋๋ค. ๊ทธ ์ผ๋ถ๊ฐ ๋๊ธฐ ์ํด ์ค๋น๊ฐ ๋์ จ๋์?
์ฌ์ฉ๋ ํค์๋: ์ธ๊ณต์ง๋ฅ, AI ์ถ์ธ, ๊ฑด๊ฐ๊ด๋ฆฌ์์์ AI, ๊ธ์ต์์์ AI, ์ฌ์ด๋ฒ ๋ณด์์์์ AI, Google์ AlphaGo, AI์ ์ฃผ์ ํ๋ ์ด์ด๋ค.
์ถ์ฒ: AI ์ฐ๊ตฌ ๋ ผ๋ฌธ, ๊ธฐ์ ๋ด์ค ์น์ฌ์ดํธ, AI ์ ๋, Google, IBM, Microsoft, Amazon์ ๊ณต์ AI ์น์ฌ์ดํธ.