Course ES
← back to chapter

Content pipeline

Multi-agent orchestration (wk19) with an output safety gate (wk16): writer drafts, critic sharpens, writer revises, moderation clears it. The shape of a real content-generation feature.

Showcase — Content pipeline

Multi-agent orchestration (wk19) with an output safety gate (wk16): writer drafts, critic sharpens, writer revises, moderation clears it. The shape of a real content-generation feature.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000. Gemini: PROVIDER=gemini docker compose up --build.

What's where

  • backend/ai_openai.py / backend/ai_gemini.py — writer/critic pipeline + moderation.
  • frontend/app/page.tsx — topic box, cleared copy.

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download content-pipeline.zip

unzip content-pipeline.zip
cd content-pipeline
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Showcase 2 (OpenAI): a content pipeline — multi-agent + safety.

Give it a topic and get publishable copy: a writer drafts, a critic sharpens it,
the writer revises, and a moderation check clears the result before it's returned.
Multi-agent orchestration (week 19) with a safety gate on the output (week 16) —
the shape of an actual content-generation feature.
"""
from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-5.4-nano"

_WRITER = "You are a marketing copywriter. Write a short, engaging paragraph on the topic."
_CRITIC = "You are an editor. Give 2-3 specific fixes to make the copy sharper and more concrete."


def _agent(system: str, user: str) -> str:
    return _client.responses.create(model=_MODEL, instructions=system, input=[{"role": "user", "content": user}]).output_text


def _flagged(text: str) -> bool:
    return _client.moderations.create(model="omni-moderation-latest", input=text).results[0].flagged


def run(topic: str) -> str:
    t = topic.strip()
    if not t:
        return "Give a topic to write about (e.g. 'a new noise-cancelling headphone')."
    if _flagged(t):
        return "[topic blocked by moderation]"
    draft = _agent(_WRITER, t)
    critique = _agent(_CRITIC, f"TOPIC: {t}\n\nDRAFT:\n{draft}")
    final = _agent(_WRITER, f"TOPIC: {t}\n\nEditor's fixes:\n{critique}\n\nRewrite the paragraph applying them.")
    if _flagged(final):
        return "[output withheld by moderation]"
    return f"{final}\n\n(passed the writer → critic → revise → moderation pipeline)"

backend/ai_gemini.py

"""Showcase 2 (Gemini): a content pipeline — multi-agent + safety.

Same writer → critic → revise → moderate flow, on Gemini.
"""
import os

from google import genai
from google.genai import types

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

_MODEL = "gemini-3.1-flash-lite"

_WRITER = "You are a marketing copywriter. Write a short, engaging paragraph on the topic."
_CRITIC = "You are an editor. Give 2-3 specific fixes to make the copy sharper and more concrete."


def _agent(system: str, user: str) -> str:
    r = _client.models.generate_content(
        model=_MODEL, contents=[types.Content(role="user", parts=[types.Part(text=user)])],
        config=types.GenerateContentConfig(system_instruction=system),
    )
    return r.text or ""


def _flagged(text: str) -> bool:
    r = _client.models.generate_content(
        model=_MODEL, contents=[types.Content(role="user", parts=[types.Part(text=text)])],
        config=types.GenerateContentConfig(system_instruction=(
            "Reply 'flag' if this text is hateful, harassing, sexual, violent, or dangerous; else 'ok'. One word.")),
    )
    return "flag" in (r.text or "").lower()


def run(topic: str) -> str:
    t = topic.strip()
    if not t:
        return "Give a topic to write about (e.g. 'a new noise-cancelling headphone')."
    if _flagged(t):
        return "[topic blocked by moderation]"
    draft = _agent(_WRITER, t)
    critique = _agent(_CRITIC, f"TOPIC: {t}\n\nDRAFT:\n{draft}")
    final = _agent(_WRITER, f"TOPIC: {t}\n\nEditor's fixes:\n{critique}\n\nRewrite the paragraph applying them.")
    if _flagged(final):
        return "[output withheld by moderation]"
    return f"{final}\n\n(passed the writer → critic → revise → moderation pipeline)"

Project files

  • .gitignore
  • README.es.md
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json