1 項(xiàng)目地址
https://github.com/joaomdmoura/crewAI
2 功能
通過設(shè)置多個(gè)智能體耀销,協(xié)同解決問題析显,以處理復(fù)雜任務(wù)锌畸;這種方法的實(shí)現(xiàn)方式是將一個(gè)任務(wù)的輸出作為另一個(gè)任務(wù)的輸入亡呵。它的優(yōu)勢在于小而有效怔锌,原理直觀易懂,而且所需的調(diào)用代碼也相當(dāng)簡單虱颗。
很多工作需要多次交互才能完成椎眯,不同角色 的 Agent 可設(shè)置成不同模型,不同輔助工具再姑,非常好用萌抵。
3 代碼分析
當(dāng)前版本 python 代碼 800 多行,但幾乎是我看過最簡單好用的多工具組合策略元镀。
4 模型
除了 openai绍填,還可以支持本地搭建的模型 ollama。
5 安裝
$ pip install crewai
6 代碼
6.1 示例
import os
from crewai import Agent, Task, Crew, Process
os.environ["OPENAI_API_KEY"] = "YOUR KEY"
from langchain.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()
# Define your agents with roles and goals
researcher = Agent(
role='Senior Research Analyst',
goal='Uncover cutting-edge developments in AI and data science in',
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting
actionable insights.""",
verbose=True,
allow_delegation=False,
tools=[search_tool]
)
writer = Agent(
role='Tech Content Strategist',
goal='Craft compelling content on tech advancements',
backstory="""You are a renowned Content Strategist, known for
your insightful and engaging articles.
You transform complex concepts into compelling narratives.""",
verbose=True,
allow_delegation=True,
)
# Create tasks for your agents
task1 = Task(
description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024.
Identify key trends, breakthrough technologies, and potential industry impacts.
Your final answer MUST be a full analysis report""",
agent=researcher
)
task2 = Task(
description="""Using the insights provided, develop an engaging blog
post that highlights the most significant AI advancements.
Your post should be informative yet accessible, catering to a tech-savvy audience.
Make it sound cool, avoid complex words so it doesn't sound like AI.
Your final answer MUST be the full blog post of at least 4 paragraphs.""",
agent=writer
)
# Instantiate your crew with a sequential process
crew = Crew(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=2, # You can set it to 1 or 2 to different logging levels
)
# Get your crew to work!
result = crew.kickoff()
print("######################")
print(result)
6.2 代碼分析
在上述代碼中栖疑,定義了兩個(gè)智能體:分析師和內(nèi)容創(chuàng)作者讨永。他們共同協(xié)作,完成了一篇關(guān)于預(yù)測 AI 2014 發(fā)展趨勢的博文遇革。
值得一提的是卿闹,在這里使用 Langchain 來調(diào)用 DuckDuckGo 搜索引擎揭糕,以便分析師智能體收集相關(guān)信息。具體來說锻霎,首先著角,分析師智能體會(huì)利用收集到的信息生成分析結(jié)果;然后旋恼,這些分析結(jié)果將被傳遞給內(nèi)容創(chuàng)作者智能體吏口,由他來撰寫博文。
6.3 注意事項(xiàng)
模型默認(rèn)調(diào)用 OpenAI GPT-4冰更,測試花費(fèi)比較高产徊,如果介意,請?jiān)?Agent 中設(shè)置成便宜的版本