# M05 Lab — Chatbot API Scaffold

This scaffold provides the HTTP server skeleton. You add the AI integration.

## Project Setup

```bash
mkdir -p ~/workshop/m05-chatbot
cd ~/workshop/m05-chatbot
```

## app.py — Starter

```python
"""AI-Powered Chatbot API — M05 Lab."""

import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

app = FastAPI(title="AI Chatbot API", version="0.1.0")


# --- Data Models ---

class ChatRequest(BaseModel):
    prompt: str


class ChatResponse(BaseModel):
    response: str


class AnalyzeRequest(BaseModel):
    text: str


class AnalyzeResponse(BaseModel):
    topics: list[str]
    summary: str


# --- Step 1: Basic Chat Endpoint ---

@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
    """
    TODO: Implement this endpoint.
    1. Get the user's prompt from request.prompt
    2. Call your LLM API with the prompt
    3. Return the LLM's response

    Use environment variables for API keys:
    - ANTHROPIC_API_KEY, OPENAI_API_KEY, or GOOGLE_API_KEY
    """
    # Your code here — use AI to help implement this
    raise HTTPException(status_code=501, detail="Not implemented yet")


# --- Step 2: Prompt Chaining (Analyze) ---

@app.post("/analyze", response_model=AnalyzeResponse)
async def analyze(request: AnalyzeRequest):
    """
    TODO: Implement prompt chaining.
    1. Call LLM: "Extract the top 3 topics from this text: {request.text}"
    2. Call LLM: "Summarize this text focusing on these topics: {topics}"
    3. Return both the topics list and the summary

    This demonstrates prompt chaining — output of step 1 feeds into step 2.
    """
    raise HTTPException(status_code=501, detail="Not implemented yet")


# --- Health Check ---

@app.get("/health")
async def health():
    return {"status": "ok"}
```

## requirements.txt

```
fastapi>=0.100.0
uvicorn[standard]>=0.23.0
pydantic>=2.0.0
openai>=1.0.0          # or anthropic, google-generativeai
httpx>=0.24.0
```

## Setup

```bash
# First activate venv: source ~/workshop/venv/bin/activate
pip install -r requirements.txt
uvicorn app:app --reload --port 8001
# Test: curl http://localhost:8001/health
```

## Using AI to Implement

Use OpenCode or VS Code Chat to implement the TODO sections:

```bash
opencode run "Read app.py. Implement the /chat endpoint. Use the OpenAI API
(openai package). Read the API key from the OPENAI_API_KEY environment variable.
Handle errors: API timeouts, invalid responses, missing API key."
```
