Skip to content

Instantly share code, notes, and snippets.

@cabe56
Last active May 8, 2025 15:08
Show Gist options
  • Save cabe56/f87241ef673c05979b6a5e41d16abd20 to your computer and use it in GitHub Desktop.
Save cabe56/f87241ef673c05979b6a5e41d16abd20 to your computer and use it in GitHub Desktop.
Flexible AI Agents

Problem

When you're using visual workflow editors like Retool or n8n:

  1. You can't import just any dependency to your python/js scripts
  2. You can't use IDEs like Cursor to auto generate code while referencing documentation, MCP servers, etc

Solution

  1. Code up any kind of workflow in python
  2. Import any dependency you want to use
  3. Call LLMs or build agents using any API you want
  4. Deploy as standalone endpoint to Modal

You can go back and forth from a visual editor to full fledged python codebase. If you think a workflow can be done easily with the visual editor, go ahead and glue those blocks together. If you identify a node that needs to be smarter, you can drop in to Cursor and vibe code your logic/agent/workflow and deploy in few steps. The resulting URL can be added to your workflow as a simple HTTP node.

Setup

  1. You need to have a Modal account
  2. Run the script by running modal server agent.py

Script

agent.py does 3 things:

  1. Define your agent/workflow
  2. Define the instance deployed to Modal
  3. Define FastAPI endpoint (https://modal.com/docs/examples/basic_web)
import os
import modal
from fastapi import FastAPI, Request, Query
from crewai import Agent, Task, Crew
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv(override=True)
# --- Agent ---
# Setup the agent, this uses Crew as it has simple API but you could use any framework or no framework.
summarizer_agent = Agent(
role='Expert Text Summarizer',
goal='Summarize the provided text concisely and accurately.',
backstory='You are an expert in distilling information and providing short, clear summaries of text.',
verbose=True,
allow_delegation=False
)
summarization_task = Task(
description='Extract the main word from this text: "{text_to_summarize}"',
expected_output='A single word.',
agent=summarizer_agent
)
summarizer = Crew(
agents=[summarizer_agent],
tasks=[summarization_task],
verbose=True
)
# --- Modal App Setup (using modal.App) ---
app = modal.App("crewai-summarizer")
# Define the image, installing necessary packages
modal_image = (
modal.Image.debian_slim(python_version="3.11")
.pip_install_from_requirements("requirements.txt")
# Pass the key to the container env
.env({
"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"),
"OPENAI_MODEL_NAME": os.getenv("OPENAI_MODEL_NAME")
})
# If you want to create more files to keep codebase tidy, just reference them by name here
# .add_local_python_source("my_module")
)
web_app = FastAPI()
# --- FastAPI Endpoint ---
@web_app.get("/summarize")
async def summarize(request: Request, text: str = Query(...)):
result = summarizer.kickoff(inputs={"text_to_summarize": text})
return {"summary": result}
# --- Modal Web Endpoint Function ---
# This function RETURNS the FastAPI app and has BOTH decorators
@app.function(image=modal_image, secrets=[modal.Secret.from_dotenv()])
@modal.asgi_app() # Correct decorator for serving the FastAPI app
def fastapi_app():
"""Mounts the FastAPI app for web serving via Modal."""
return web_app
modal
fastapi
crewai
openai
python-dotenv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment