This project demonstrates an advanced AI system built with CrewAI Flow that intelligently routes between research and conversation modes. The system can conduct comprehensive academic research using multiple databases and maintain natural conversations with users.
This is an intelligent routing system that:
- Analyzes user intent - Determines whether a message needs research or conversation
- Conducts deep research - Searches academic databases (arXiv, Nature, IEEE, PubMed) for scholarly papers
- Maintains conversations - Provides natural, contextual responses for non-research queries
- Preserves context - Tracks conversation history across interactions
- Smart Intent Routing: Automatically classifies user messages as research or conversation
- Academic Research: Searches multiple scholarly databases with proper citations
- Conversation Memory: Maintains message history throughout the session
- Custom Tools: Integrated deep research capabilities via Firecrawl API
- Flow Orchestration: Uses CrewAI Flow for complex multi-agent workflows
First, install CrewAI globally:
pip install crewai[tools]
Generate a new CrewAI flow project:
crewai create flow your_flow_name
cd your_flow_name
Ensure you have Python >=3.10 <3.14 installed. This project uses UV for dependency management.
Install UV if you haven't already:
pip install uv
Install project dependencies:
crewai install
Create a .env
file in your project root and add your API keys:
OPENAI_API_KEY=your_openai_api_key_here
FIRECRAWL_API_KEY=your_firecrawl_api_key_here
Required API Keys:
- OpenAI API Key: For LLM functionality and intent routing
- Firecrawl API Key: For academic research and paper searching
The main flow (DeepResearchFlow
) uses CrewAI's @persist()
decorator and orchestrates these key components:
Message
: Tracks role, content, and timestamp for conversation historyRouterIntent
: Structures LLM routing decisions with intent classification and research query generationSource
: Represents research sources with URL, title, and relevant contentSearchResult
: Contains comprehensive research summary with inline citations and source listFlowState
: Maintains user message, conversation history, research queries, and results
-
Starting Flow (
@start() starting_flow
)- Initializes the flow with user message
- Adds user message to conversation history
- Triggers the routing process
-
Intent Router (
@router(starting_flow) routing_intent
)- Uses GPT-4.1 mini with
response_format=RouterIntent
for structured output - Analyzes user messages and conversation history
- Classifies intent as "research" or "conversation" using detailed criteria
- Generates optimized research queries when research intent is detected
- Returns routing decision for flow orchestration
- Uses GPT-4.1 mini with
-
Research Handler (
@listen("research") handle_research
)- Creates specialized
Deep Research Specialist
agent - Uses
DeepResearchPaper()
tool for academic database searches - Executes research with
response_format=SearchResult
for structured output - Requires comprehensive summaries with inline URL citations:
(https://example.com/source)
- Returns complete research results with sources list
- Creates specialized
-
Conversation Handler (
@listen("conversation") follow_up_conversation
)- Uses GPT-4.1 mini with higher temperature (0.7) for natural responses
- Maintains conversation context and flow
- Provides helpful responses while guiding toward research opportunities
- Adds assistant responses to message history
- DeepResearchPaper: Searches academic databases and returns exactly 5 research papers with full content and proper citations
- RouterIntent: Uses Pydantic models for structured LLM responses with
user_intent
,research_query
, andreasoning
fields - SearchResult: Enforces structured research output with
research_summary
andsources_list
fields - Response Format Validation: All LLM calls use
response_format
parameter for type-safe outputs
@persist()
: Enables flow state persistence across sessions@start()
: Marks the entry point method (starting_flow
)@router()
: Creates conditional routing based on LLM decisions@listen()
: Defines event-driven handlers for "research" and "conversation" intents
- Message History: Persistent tracking using
List[Message]
with role, content, and timestamp - Context Awareness: All LLM prompts include conversation history for context-aware responses
- State Management: Centralized state handling through
FlowState
class
- Router LLM: GPT-4.1 mini with temperature=0.1 for consistent routing decisions
- Conversation LLM: GPT-4.1 mini with temperature=0.7 for natural, varied responses
- Research Agent: Specialized agent with verbose=True for detailed research execution
- Inline Citations: Every fact must be followed by source URL in parentheses format:
(URL)
- Comprehensive Summaries: Organized by topics/themes with cohesive narrative structure
- Source Documentation: Complete source list with URL, title, and relevant content
Ensure you have Python >=3.10 <3.14 installed on your system.
- Clone this repository or use it as a template
- Install dependencies:
crewai install
- Configure your
.env
file with required API keys - Run the system:
crewai run
Starting from a fresh CrewAI flow, here's how to adapt it for this research system:
-
Update Flow State (
FlowState
class):- Add message history tracking
- Include research query handling
- Add intent classification fields
-
Implement Intent Routing:
- Create router method with LLM classification
- Define research vs conversation criteria
- Add proper prompt engineering
-
Add Research Capabilities:
- Integrate research tools (Firecrawl API)
- Create specialized research agent
- Implement structured output formatting
-
Build Conversation System:
- Add conversation handler
- Implement context awareness
- Include message history management
src/crewai_flow_workshop1/main.py
: Main flow logic and orchestrationsrc/crewai_flow_workshop1/tools/deep_research_paper.py
: Research tool implementationpyproject.toml
: Project configuration and dependencies
The main.py implementation relies on these key imports:
from crewai.flow import Flow, listen, start, router, persist
from pydantic import BaseModel, Field
from typing import Literal, List, Optional
from datetime import datetime
from crewai import LLM, Agent
import json
- CrewAI Flow:
Flow
,@listen
,@start
,@router
,@persist
decorators - Pydantic Models: Type-safe data structures with
BaseModel
andField
- Type Hints:
Literal
,List
,Optional
for strict typing - LLM Integration: Direct
LLM
class usage with structured responses - Agent Creation: Dynamic agent creation with specialized roles and tools
Execute the flow from the project root:
crewai run
The system will:
- Start with the default message: "help me researching on the latest trends in ai"
- Add the user message to conversation history with timestamp
- Route the message through GPT-4.1 mini intent classification
- Either conduct deep research (with academic database search) or provide conversational response
- Maintain persistent message history for context across interactions
The main.py file also includes utility functions:
kickoff()
: Initializes and runs theDeepResearchFlow
with tracing enabledplot()
: Generates a visual representation of the flow structure- Direct execution: Running
python main.py
callskickoff()
for immediate flow execution
User: "What are the latest studies on transformer architectures in 2024?"
System: → Routes to research → Returns academic papers with citations
User: "Hello, how does this system work?"
System: → Routes to conversation → Provides explanation and guidance
Previous: Discussion about AI ethics
User: "Can you find more studies about bias in machine learning?"
System: → Routes to research with context → Returns relevant bias studies
- Missing API Keys: Ensure both
OPENAI_API_KEY
andFIRECRAWL_API_KEY
are set in your.env
file - Python Version: Verify you're using Python >=3.10 <3.14
- Dependencies: Run
crewai install
to ensure all packages are properly installed - Research Timeout: If research queries timeout, try more specific search terms
- Use
crewai run
for full execution - Check logs for detailed flow execution information
- Modify the default query in
FlowState
for different starting points - Adjust research limits and timeouts in the tool configuration
For support, questions, or feedback:
- CrewAI Documentation: docs.crewai.com
- CrewAI GitHub: github.com/joaomdmoura/crewai
- CrewAI Discord: Join our Discord
- CrewAI Chat: Chat with our docs
Let's create intelligent systems together with the power and simplicity of CrewAI Flow!