diff --git a/crewai-python/01_single_agent.py b/crewai-python/01_single_agent.py new file mode 100644 index 0000000000..0f747522c7 --- /dev/null +++ b/crewai-python/01_single_agent.py @@ -0,0 +1,25 @@ +from crewai import LLM, Agent, Crew, Task + +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/crewai-python/02_research_and_writer_crew.py b/crewai-python/02_research_and_writer_crew.py new file mode 100644 index 0000000000..7c677f61a2 --- /dev/null +++ b/crewai-python/02_research_and_writer_crew.py @@ -0,0 +1,53 @@ +from crewai import LLM, Agent, Crew, Process, Task + +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/crewai-python/03_explicit_context.py b/crewai-python/03_explicit_context.py new file mode 100644 index 0000000000..98a5ac024e --- /dev/null +++ b/crewai-python/03_explicit_context.py @@ -0,0 +1,54 @@ +from crewai import LLM, Agent, Crew, Task + +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/crewai-python/04_agent_with_tools.py b/crewai-python/04_agent_with_tools.py new file mode 100644 index 0000000000..94a5721e28 --- /dev/null +++ b/crewai-python/04_agent_with_tools.py @@ -0,0 +1,58 @@ +from crewai import LLM, Agent, Crew, Task +from crewai_tools import ScrapeWebsiteTool + +llm = LLM(model="gemini/gemini-2.5-pro") + +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/crewai-python/README.md b/crewai-python/README.md new file mode 100644 index 0000000000..442f102fb9 --- /dev/null +++ b/crewai-python/README.md @@ -0,0 +1,58 @@ +# Coordinating Teams of AI Agents with CrewAI in Python + +Sample code for the Real Python tutorial +[CrewAI in Python: Coordinating Teams of AI Agents](https://realpython.com/crewai-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` | Get Started With CrewAI in Python | +| `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. diff --git a/crewai-python/requirements.txt b/crewai-python/requirements.txt new file mode 100644 index 0000000000..821a46020b --- /dev/null +++ b/crewai-python/requirements.txt @@ -0,0 +1,149 @@ +aiofiles==24.1.0 +aiohappyeyeballs==2.7.1 +aiohttp==3.14.1 +aiosignal==1.4.0 +aiosqlite==0.21.0 +annotated-doc==0.0.4 +annotated-types==0.7.0 +anyio==4.14.1 +appdirs==1.4.4 +attrs==26.1.0 +backoff==2.2.1 +bcrypt==5.0.0 +beautifulsoup4==4.13.5 +build==1.5.0 +cel-python==0.5.0 +certifi==2026.6.17 +cffi==2.0.0 +charset-normalizer==3.4.7 +chromadb==1.1.1 +click==8.4.2 +crewai==1.15.1 +crewai-cli==1.15.1 +crewai-core==1.15.1 +crewai-tools==1.15.1 +cryptography==49.0.0 +defusedxml==0.7.1 +deprecation==2.1.0 +distro==1.9.0 +docstring-parser==0.18.0 +durationpy==0.10 +et-xmlfile==2.0.0 +filelock==3.29.4 +flatbuffers==25.12.19 +frozenlist==1.8.0 +fsspec==2026.6.0 +google-auth==2.55.1 +google-genai==1.65.0 +google-re2==1.1.20251105 +googleapis-common-protos==1.75.0 +grpcio==1.81.1 +h11==0.16.0 +hf-xet==1.5.1 +httpcore==1.0.9 +httptools==0.8.0 +httpx==0.28.1 +httpx-sse==0.4.3 +huggingface-hub==1.21.0 +idna==3.18 +importlib-resources==7.1.0 +instructor==1.15.4 +jinja2==3.1.6 +jiter==0.14.0 +jmespath==1.1.0 +json-repair==0.25.3 +json5==0.10.0 +jsonref==1.1.0 +jsonschema==4.26.0 +jsonschema-specifications==2025.9.1 +kubernetes==36.0.2 +lance-namespace==0.9.0 +lance-namespace-urllib3-client==0.9.0 +lancedb==0.30.0 +lark==1.3.1 +linkify-it-py==2.1.0 +lxml==6.1.1 +markdown-it-py==4.2.0 +markupsafe==3.0.3 +mcp==1.26.0 +mdit-py-plugins==0.6.1 +mdurl==0.1.2 +mmh3==5.2.1 +multidict==6.7.1 +numpy==2.5.0 +oauthlib==3.3.1 +onnxruntime==1.27.0 +openai==2.44.0 +openpyxl==3.1.5 +opentelemetry-api==1.42.1 +opentelemetry-exporter-otlp-proto-common==1.42.1 +opentelemetry-exporter-otlp-proto-grpc==1.42.1 +opentelemetry-exporter-otlp-proto-http==1.42.1 +opentelemetry-proto==1.42.1 +opentelemetry-sdk==1.42.1 +opentelemetry-semantic-conventions==0.63b1 +orjson==3.11.9 +overrides==7.7.0 +packaging==26.2 +pdfminer-six==20260107 +pdfplumber==0.11.10 +pendulum==3.2.0 +pillow==12.3.0 +platformdirs==4.10.0 +portalocker==2.7.0 +posthog==5.4.0 +propcache==0.5.2 +protobuf==6.33.6 +pyarrow==24.0.0 +pyasn1==0.6.3 +pyasn1-modules==0.4.2 +pybase64==1.4.3 +pycparser==3.0 +pydantic==2.12.5 +pydantic-core==2.41.5 +pydantic-settings==2.10.1 +pygments==2.20.0 +pyjwt==2.13.0 +pymupdf==1.26.7 +pypdfium2==5.11.0 +pypika==0.51.1 +pyproject-hooks==1.2.0 +python-dateutil==2.9.0.post0 +python-docx==1.2.0 +python-dotenv==1.2.2 +python-multipart==0.0.32 +pytube==15.0.0 +pyyaml==6.0.3 +referencing==0.37.0 +regex==2026.1.15 +requests==2.34.2 +requests-oauthlib==2.0.0 +rich==14.3.4 +rpds-py==2026.6.3 +shellingham==1.5.4 +six==1.17.0 +sniffio==1.3.1 +soupsieve==2.8.4 +sse-starlette==3.4.5 +starlette==1.3.1 +tenacity==9.1.4 +textual==8.2.8 +tiktoken==0.12.0 +tokenizers==0.23.1 +tomli==2.0.2 +tomli-w==1.1.0 +tqdm==4.68.3 +typer==0.25.1 +typing-extensions==4.16.0 +typing-inspection==0.4.2 +tzdata==2026.2 +uc-micro-py==2.0.0 +urllib3==2.7.0 +uv==0.11.26 +uvicorn==0.49.0 +uvloop==0.22.1 +watchfiles==1.2.0 +websocket-client==1.9.0 +websockets==16.0 +yarl==1.24.2 +youtube-transcript-api==1.2.4