Course ES
← back to chapter

Whole-doc Q&A

Paste a document and ask about it — the entire text goes in the prompt, no retrieval, no chunking. Put your question on a line starting with `Q:`; everything else is the document. For anything that fits the context window, this beats RAG: nothing to miss.

Showcase — Whole-doc Q&A

Paste a document and ask about it — the entire text goes in the prompt, no retrieval, no chunking. Put your question on a line starting with Q:; everything else is the document. For anything that fits the context window, this beats RAG: nothing to miss.

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 — split the input into document and Q: question, then answer with the whole document in context.
  • frontend/app/page.tsx — document + question box, answer out.

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 whole-doc-qa.zip

unzip whole-doc-qa.zip
cd whole-doc-qa
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 1 (OpenAI): whole-document Q&A, no retrieval.

Paste a document and ask about it — the entire text goes in the prompt. Put your
question on a line starting with 'Q:' (usually at the end); everything else is
the document. For anything that fits the context window, this beats RAG: nothing
to chunk, nothing to miss.
"""
from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-5.4-nano"


def run(text: str) -> str:
    body = text.strip()
    if "Q:" in body:
        idx = body.rfind("Q:")
        document, question = body[:idx].strip(), body[idx + 2:].strip()
    else:
        document, question = body, "Summarize this document in a few sentences."
    if not document:
        return "Paste a document, then put your question on a line starting with 'Q:'."
    response = _client.responses.create(
        model=_MODEL,
        instructions=f"Answer using ONLY this document. If it names sections, cite the relevant one.\n\n{document}",
        input=[{"role": "user", "content": question}],
    )
    return response.output_text

backend/ai_gemini.py

"""Showcase 1 (Gemini): whole-document Q&A, no retrieval.

Same "paste doc + 'Q:' question" flow; the whole document goes in the system
instruction.
"""
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"


def run(text: str) -> str:
    body = text.strip()
    if "Q:" in body:
        idx = body.rfind("Q:")
        document, question = body[:idx].strip(), body[idx + 2:].strip()
    else:
        document, question = body, "Summarize this document in a few sentences."
    if not document:
        return "Paste a document, then put your question on a line starting with 'Q:'."
    response = _client.models.generate_content(
        model=_MODEL,
        contents=[types.Content(role="user", parts=[types.Part(text=question)])],
        config=types.GenerateContentConfig(
            system_instruction=f"Answer using ONLY this document. If it names sections, cite the relevant one.\n\n{document}",
        ),
    )
    return response.text or ""

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