Skip to content

Commit 271cef6

Browse files
FarahAbdomartin-martinbrendawelesbzaczynski
authored
Add materials for "Coordinating Teams of AI Agents with CrewAI in Python" (#775)
* Add materials for CrewAI tutorial * Apply Black formatting * Apply Ruff formatting * Update coordinating-teams-of-ai-agents-with-crewai-in-python/README.md Co-authored-by: Martin Breuss <martin-martin@users.noreply.github.com> * Update coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt Co-authored-by: Martin Breuss <martin-martin@users.noreply.github.com> * Update README.md * Rename the folder to reflect the keyphrase * Pin requirements for a reproducible environment * Bump the model to avoid hallucinations and sort import statements --------- Co-authored-by: Martin Breuss <martin-martin@users.noreply.github.com> Co-authored-by: brendaweles <160772586+brendaweles@users.noreply.github.com> Co-authored-by: Bartosz Zaczyński <bartosz.zaczynski@gmail.com>
1 parent 2bc0fdb commit 271cef6

6 files changed

Lines changed: 397 additions & 0 deletions

File tree

crewai-python/01_single_agent.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from crewai import LLM, Agent, Crew, Task
2+
3+
llm = LLM(model="gemini/gemini-2.5-flash")
4+
5+
travel_agent = Agent(
6+
role="Travel Advisor",
7+
goal="Provide helpful travel recommendations",
8+
backstory=(
9+
"You're an experienced travel advisor who has visited"
10+
" over fifty countries and specializes in budget-friendly"
11+
" adventure travel."
12+
),
13+
llm=llm,
14+
verbose=True,
15+
)
16+
17+
task = Task(
18+
description="Suggest three budget-friendly destinations in Southeast Asia",
19+
expected_output="A short list of three destinations with brief descriptions",
20+
agent=travel_agent,
21+
)
22+
23+
crew = Crew(agents=[travel_agent], tasks=[task])
24+
result = crew.kickoff()
25+
print(result.raw)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from crewai import LLM, Agent, Crew, Process, Task
2+
3+
llm = LLM(model="gemini/gemini-2.5-flash")
4+
5+
researcher = Agent(
6+
role="Senior Research Analyst",
7+
goal="Find comprehensive information about a given topic",
8+
backstory=(
9+
"You're a seasoned research analyst with a knack for"
10+
" uncovering the most relevant and accurate information."
11+
" You're known for your thorough and well-organized research."
12+
),
13+
llm=llm,
14+
)
15+
16+
writer = Agent(
17+
role="Content Writer",
18+
goal="Write clear, engaging content based on research findings",
19+
backstory=(
20+
"You're an experienced writer who excels at transforming"
21+
" complex research into accessible, well-structured articles"
22+
" that readers enjoy."
23+
),
24+
llm=llm,
25+
)
26+
27+
research_task = Task(
28+
description="Research the latest trends in renewable energy technology",
29+
expected_output=(
30+
"A detailed list of the top five renewable energy trends"
31+
" with explanations"
32+
),
33+
agent=researcher,
34+
)
35+
36+
writing_task = Task(
37+
description="Write a short article based on the research findings",
38+
expected_output=(
39+
"A well-structured article of about three hundred words"
40+
" on renewable energy trends"
41+
),
42+
agent=writer,
43+
)
44+
45+
crew = Crew(
46+
agents=[researcher, writer],
47+
tasks=[research_task, writing_task],
48+
process=Process.sequential,
49+
verbose=True,
50+
)
51+
52+
result = crew.kickoff()
53+
print(result.raw)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from crewai import LLM, Agent, Crew, Task
2+
3+
llm = LLM(model="gemini/gemini-2.5-flash")
4+
5+
researcher = Agent(
6+
role="Senior Research Analyst",
7+
goal="Find comprehensive information about a given topic",
8+
backstory=(
9+
"You're a seasoned research analyst with a knack for"
10+
" uncovering the most relevant and accurate information."
11+
" You're known for your thorough and well-organized research."
12+
),
13+
llm=llm,
14+
)
15+
16+
writer = Agent(
17+
role="Content Writer",
18+
goal="Write clear, engaging content based on research findings",
19+
backstory=(
20+
"You're an experienced writer who excels at transforming"
21+
" complex research into accessible, well-structured articles"
22+
" that readers enjoy."
23+
),
24+
llm=llm,
25+
)
26+
27+
research_task = Task(
28+
description=(
29+
"Research the current state of electric vehicle adoption worldwide"
30+
),
31+
expected_output=(
32+
"A bullet-point list of key statistics and trends in EV adoption"
33+
),
34+
agent=researcher,
35+
)
36+
37+
writing_task = Task(
38+
description=(
39+
"Using the provided research, write a concise summary article"
40+
" about electric vehicle adoption"
41+
),
42+
expected_output="A two hundred word article summarizing EV adoption trends",
43+
agent=writer,
44+
context=[research_task],
45+
)
46+
47+
crew = Crew(
48+
agents=[researcher, writer],
49+
tasks=[research_task, writing_task],
50+
verbose=True,
51+
)
52+
53+
result = crew.kickoff()
54+
print(result.raw)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from crewai import LLM, Agent, Crew, Task
2+
from crewai_tools import ScrapeWebsiteTool
3+
4+
llm = LLM(model="gemini/gemini-2.5-pro")
5+
6+
scrape_tool = ScrapeWebsiteTool()
7+
8+
researcher = Agent(
9+
role="Python Release Analyst",
10+
goal="Find the latest Python release information",
11+
backstory=(
12+
"You're a developer advocate who tracks Python releases"
13+
" and summarizes what's new for the community."
14+
),
15+
tools=[scrape_tool],
16+
llm=llm,
17+
)
18+
19+
writer = Agent(
20+
role="Tech Blogger",
21+
goal="Write concise release summaries for a developer audience",
22+
backstory=(
23+
"You write clear, engaging blog posts that help developers"
24+
" stay up to date with the Python ecosystem."
25+
),
26+
llm=llm,
27+
)
28+
29+
research_task = Task(
30+
description=(
31+
"Scrape https://www.python.org/downloads/ and report"
32+
" the latest stable Python version number and its release date"
33+
),
34+
expected_output="The latest Python version number and release date",
35+
agent=researcher,
36+
)
37+
38+
writing_task = Task(
39+
description=(
40+
"Write a one-paragraph announcement based on the Python"
41+
" release information provided by the research task"
42+
),
43+
expected_output=(
44+
"A concise one hundred word announcement covering the"
45+
" latest Python version number and release date"
46+
),
47+
agent=writer,
48+
context=[research_task],
49+
)
50+
51+
crew = Crew(
52+
agents=[researcher, writer],
53+
tasks=[research_task, writing_task],
54+
verbose=True,
55+
)
56+
57+
result = crew.kickoff()
58+
print(result.raw)

crewai-python/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Coordinating Teams of AI Agents with CrewAI in Python
2+
3+
Sample code for the Real Python tutorial
4+
[CrewAI in Python: Coordinating Teams of AI Agents](https://realpython.com/crewai-python/).
5+
6+
## Requirements
7+
8+
- Python 3.10 to 3.13 (CrewAI does not support 3.14+)
9+
- A free [Google AI Studio](https://aistudio.google.com/) API key for Gemini
10+
11+
## Setup
12+
13+
Create and activate a virtual environment, then install the dependencies:
14+
15+
```console
16+
$ python -m venv venv
17+
$ source venv/bin/activate # On Windows: .\venv\Scripts\activate
18+
$ python -m pip install -r requirements.txt
19+
```
20+
21+
Set your Gemini API key as an environment variable:
22+
23+
```console
24+
$ export GEMINI_API_KEY="your-gemini-api-key-here"
25+
```
26+
27+
On Windows PowerShell:
28+
29+
```pscon
30+
PS> $ENV:GEMINI_API_KEY = "your-gemini-api-key-here"
31+
```
32+
33+
## Running the Examples
34+
35+
Each script corresponds to one section of the tutorial:
36+
37+
| File | Tutorial Section |
38+
|------|------------------|
39+
| `01_single_agent.py` | Get Started With CrewAI in Python |
40+
| `02_research_and_writer_crew.py` | Build Your First Multi-Agent Team |
41+
| `03_explicit_context.py` | Control Task Dependencies Explicitly |
42+
| `04_agent_with_tools.py` | Expand Agent Capabilities With Tools |
43+
44+
Run any script with:
45+
46+
```console
47+
$ python 01_single_agent.py
48+
```
49+
50+
## Notes
51+
52+
- The `verbose=True` flag prints detailed agent reasoning logs — useful for
53+
learning, but noisy for production.
54+
- If CrewAI complains about a missing `OPENAI_API_KEY` (e.g., when memory
55+
or certain tools are enabled), set it to any non-empty string to suppress
56+
the error.
57+
- On first run, CrewAI may show a "Would you like to view your execution
58+
traces?" prompt that auto-dismisses after twenty seconds.

0 commit comments

Comments
 (0)