Course EN
← back to chapter

Q&A de documento completo

Pega un documento y pregúntale — el texto completo va en el prompt, sin retrieval, sin chunking. Pon tu pregunta en una línea que empiece con `Q:`; todo lo demás es el documento. Para cualquier cosa que quepa en la ventana de contexto, esto le gana a RAG: no hay nada que perder.

Showcase — Q&A de documento completo

Pega un documento y pregúntale — el texto completo va en el prompt, sin retrieval, sin chunking. Pon tu pregunta en una línea que empiece con Q:; todo lo demás es el documento. Para cualquier cosa que quepa en la ventana de contexto, esto le gana a RAG: no hay nada que perder.

Córrelo

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

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

Qué hay aquí

  • backend/ai_openai.py / backend/ai_gemini.py — separan el input en documento y pregunta Q:, luego responden con el documento completo en contexto.
  • frontend/app/page.tsx — caja de documento + pregunta, respuesta a la salida.

Detenlo

docker compose down

Ejecútalo en tu máquina

Descarga el proyecto como ZIP y córrelo con Docker. Levanta un backend FastAPI y un frontend Next.js en localhost:3000.

Descargar 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

Escribe algo, elige un proveedor y ejecuta el mismo código de Código contra la API real. Requiere iniciar sesión.


  

Los mismos módulos que ejecuta el botón Run. El proyecto completo (frontend, Dockerfile, compose) está en el ZIP, pestaña 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 ""

Archivos del proyecto

  • .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