Using the CrewAI CLI within an existing FastAPI application

Hey all,

I’m building a slack bot application using FastAPI, slack-bolt, etc. I want to enable crews within the application but I find that utilizing the CLI is very nice and offers a lot of benefits.

Has anyone found a good pattern for building crews within a FastAPI app other than manually creating them?

Thanks!

It looks like I should have just asked the CrewAI GPT

Building crews within a FastAPI application while leveraging the CrewAI framework is absolutely possible and can be a highly effective way to integrate AI orchestration into your app. However, using the CrewAI CLI inside an existing application has limitations, especially since the CLI is designed for standalone projects.

Here’s a structured approach to achieve your goals:


Plan:

  1. Use the CrewAI Framework Directly:

    • Instead of using the CLI to create and manage crews, you can directly use CrewAI’s Python API.
    • This ensures that crews remain tightly coupled with your FastAPI application without requiring separate requirements.txt or repositories.
  2. Testing and Training with CLI:

    • While the CLI is helpful for testing and training, you can adapt its capabilities by creating testing endpoints in your FastAPI app or running Python scripts for testing locally.
    • If you must use the CLI for testing, consider using symbolic links or shared directories to maintain a unified dependency structure.
  3. Integration into FastAPI:

    • Define your agents.yaml and tasks.yaml configurations programmatically (or read them from YAML files stored in your app).
    • Use these configurations to dynamically create and execute crews.
  4. Unified Dependency Management:

    • Ensure the requirements.txt for your FastAPI app includes all dependencies for CrewAI.
    • Avoid separate environments by installing all necessary packages into a single virtual environment.

Implementation Details

1. Initialize CrewAI in FastAPI

Set up CrewAI within your FastAPI project. Here’s an example structure:

app/
├── main.py  # FastAPI application entry point
├── crews/
│   ├── agents.yaml
│   ├── tasks.yaml
│   ├── crew_manager.py  # Module to handle CrewAI logic
├── requirements.txt

2. Code Example for crew_manager.py

import os
from crewai import Agent, Task, Crew, Process

# Optional: Use environment variables for API keys
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"

def create_crew():
    # Define agents
    researcher = Agent(
        role='Researcher',
        goal='Conduct thorough research on {topic}',
        verbose=True,
        memory=True,
        backstory='A diligent researcher passionate about uncovering insights.',
    )

    writer = Agent(
        role='Writer',
        goal='Write a compelling article about {topic}',
        verbose=True,
        memory=True,
        backstory='A creative writer who simplifies complex topics.',
    )

    # Define tasks
    research_task = Task(
        description=(
            "Research the topic and gather key points."
            "Your report should include the pros and cons."
        ),
        expected_output='A detailed summary of the topic.',
        agent=researcher,
    )

    write_task = Task(
        description=(
            "Write an article based on the research."
            "Make it engaging and informative."
        ),
        expected_output='A 500-word article in markdown format.',
        agent=writer,
    )

    # Define crew
    crew = Crew(
        agents=[researcher, writer],
        tasks=[research_task, write_task],
        process=Process.sequential,
    )

    return crew

def kickoff_crew(inputs):
    crew = create_crew()
    return crew.kickoff(inputs=inputs)

3. Expose Crews in FastAPI

from fastapi import FastAPI
from crews.crew_manager import kickoff_crew

app = FastAPI()

@app.post("/kickoff/")
async def kickoff_crew_endpoint(inputs: dict):
    result = kickoff_crew(inputs)
    return {"result": result}

Testing and Training

  1. Testing Within the Application:

    • You can use /kickoff/ endpoint to test your crews.
    • Send requests with different inputs to simulate training scenarios.
  2. Adapting CLI-Like Features:

    • If you prefer CLI-like operations for training/testing, you can create a command-line script in your app:
    python -m app.scripts.train_crew --topic "AI in Healthcare"
    
  3. Avoid Duplicate Environments:

    • Since you’re integrating CrewAI directly, ensure the app’s environment includes all CrewAI dependencies.
    • This eliminates the need for separate requirements.txt files.

Pros of This Approach:

  • Unified Dependency Management: All dependencies are managed within the FastAPI app.
  • Tight Integration: Crews are integrated into your app logic, ensuring seamless execution.
  • Testing Flexibility: Use both FastAPI endpoints and CLI-like scripts for testing and training.

Cons:

  • CLI testing may require adaptation (e.g., creating scripts or endpoints).
  • Requires structured code to manage CrewAI’s configurations dynamically.

This method avoids the need for separate requirements.txt or repositories and aligns perfectly with the integration of crews into a FastAPI application. Let me know if you need assistance setting up any specific part!

1 Like

Thank you for doing this. This is what I was looking for.

1 Like