diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py new file mode 100644 index 0000000000..98acee857a --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/01_single_agent.py @@ -0,0 +1,25 @@ +from crewai import Agent, Task, Crew, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +travel_agent = Agent( + role="Travel Advisor", + goal="Provide helpful travel recommendations", + backstory=( + "You're an experienced travel advisor who has visited" + " over fifty countries and specializes in budget-friendly" + " adventure travel." + ), + llm=llm, + verbose=True, +) + +task = Task( + description="Suggest three budget-friendly destinations in Southeast Asia", + expected_output="A short list of three destinations with brief descriptions", + agent=travel_agent, +) + +crew = Crew(agents=[travel_agent], tasks=[task]) +result = crew.kickoff() +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py new file mode 100644 index 0000000000..04e1aaffef --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/02_research_and_writer_crew.py @@ -0,0 +1,53 @@ +from crewai import Agent, Task, Crew, Process, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +researcher = Agent( + role="Senior Research Analyst", + goal="Find comprehensive information about a given topic", + backstory=( + "You're a seasoned research analyst with a knack for" + " uncovering the most relevant and accurate information." + " You're known for your thorough and well-organized research." + ), + llm=llm, +) + +writer = Agent( + role="Content Writer", + goal="Write clear, engaging content based on research findings", + backstory=( + "You're an experienced writer who excels at transforming" + " complex research into accessible, well-structured articles" + " that readers enjoy." + ), + llm=llm, +) + +research_task = Task( + description="Research the latest trends in renewable energy technology", + expected_output=( + "A detailed list of the top five renewable energy trends" + " with explanations" + ), + agent=researcher, +) + +writing_task = Task( + description="Write a short article based on the research findings", + expected_output=( + "A well-structured article of about three hundred words" + " on renewable energy trends" + ), + agent=writer, +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + process=Process.sequential, + verbose=True, +) + +result = crew.kickoff() +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py new file mode 100644 index 0000000000..28fe576400 --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/03_explicit_context.py @@ -0,0 +1,54 @@ +from crewai import Agent, Task, Crew, LLM + +llm = LLM(model="gemini/gemini-2.5-flash") + +researcher = Agent( + role="Senior Research Analyst", + goal="Find comprehensive information about a given topic", + backstory=( + "You're a seasoned research analyst with a knack for" + " uncovering the most relevant and accurate information." + " You're known for your thorough and well-organized research." + ), + llm=llm, +) + +writer = Agent( + role="Content Writer", + goal="Write clear, engaging content based on research findings", + backstory=( + "You're an experienced writer who excels at transforming" + " complex research into accessible, well-structured articles" + " that readers enjoy." + ), + llm=llm, +) + +research_task = Task( + description=( + "Research the current state of electric vehicle adoption worldwide" + ), + expected_output=( + "A bullet-point list of key statistics and trends in EV adoption" + ), + agent=researcher, +) + +writing_task = Task( + description=( + "Using the provided research, write a concise summary article" + " about electric vehicle adoption" + ), + expected_output="A two hundred word article summarizing EV adoption trends", + agent=writer, + context=[research_task], +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + verbose=True, +) + +result = crew.kickoff() +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py new file mode 100644 index 0000000000..cc33d20131 --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/04_agent_with_tools.py @@ -0,0 +1,58 @@ +from crewai import Agent, Task, Crew, LLM +from crewai_tools import ScrapeWebsiteTool + +llm = LLM(model="gemini/gemini-2.5-flash") + +scrape_tool = ScrapeWebsiteTool() + +researcher = Agent( + role="Python Release Analyst", + goal="Find the latest Python release information", + backstory=( + "You're a developer advocate who tracks Python releases" + " and summarizes what's new for the community." + ), + tools=[scrape_tool], + llm=llm, +) + +writer = Agent( + role="Tech Blogger", + goal="Write concise release summaries for a developer audience", + backstory=( + "You write clear, engaging blog posts that help developers" + " stay up to date with the Python ecosystem." + ), + llm=llm, +) + +research_task = Task( + description=( + "Scrape https://www.python.org/downloads/ and report" + " the latest stable Python version number and its release date" + ), + expected_output="The latest Python version number and release date", + agent=researcher, +) + +writing_task = Task( + description=( + "Write a one-paragraph announcement based on the Python" + " release information provided by the research task" + ), + expected_output=( + "A concise one hundred word announcement covering the" + " latest Python version number and release date" + ), + agent=writer, + context=[research_task], +) + +crew = Crew( + agents=[researcher, writer], + tasks=[research_task, writing_task], + verbose=True, +) + +result = crew.kickoff() +print(result.raw) diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md b/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md new file mode 100644 index 0000000000..5e72c9805a --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/README.md @@ -0,0 +1,58 @@ +# Coordinating Teams of AI Agents with CrewAI in Python + +Sample code for the Real Python tutorial +[Coordinating Teams of AI Agents with CrewAI in Python](https://realpython.com/coordinating-teams-of-ai-agents-with-crewai-in-python/). + +## Requirements + +- Python 3.10 to 3.13 (CrewAI does not support 3.14+) +- A free [Google AI Studio](https://aistudio.google.com/) API key for Gemini + +## Setup + +Create and activate a virtual environment, then install the dependencies: + +```console +$ python -m venv venv +$ source venv/bin/activate # On Windows: .\venv\Scripts\activate +$ python -m pip install -r requirements.txt +``` + +Set your Gemini API key as an environment variable: + +```console +$ export GEMINI_API_KEY="your-gemini-api-key-here" +``` + +On Windows PowerShell: + +```pscon +PS> $ENV:GEMINI_API_KEY = "your-gemini-api-key-here" +``` + +## Running the Examples + +Each script corresponds to one section of the tutorial: + +| File | Tutorial Section | +|------|------------------| +| `01_single_agent.py` | Start Using CrewAI to Create Agent Teams | +| `02_research_and_writer_crew.py` | Build Your First Multi-Agent Team | +| `03_explicit_context.py` | Control Task Dependencies Explicitly | +| `04_agent_with_tools.py` | Expand Agent Capabilities With Tools | + +Run any script with: + +```console +$ python 01_single_agent.py +``` + +## Notes + +- The `verbose=True` flag prints detailed agent reasoning logs — useful for + learning, but noisy for production. +- If CrewAI complains about a missing `OPENAI_API_KEY` (e.g., when memory + or certain tools are enabled), set it to any non-empty string to suppress + the error. +- On first run, CrewAI may show a "Would you like to view your execution + traces?" prompt that auto-dismisses after twenty seconds. \ No newline at end of file diff --git a/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt b/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt new file mode 100644 index 0000000000..d75623b71e --- /dev/null +++ b/coordinating-teams-of-ai-agents-with-crewai-in-python/requirements.txt @@ -0,0 +1 @@ +crewai[tools,google-genai] \ No newline at end of file