Course EN
← back to chapter

Buscador de aguja

Un muro de notas de release y de ops ya está en el prompt; pide un dato enterrado y el modelo lo saca — la prueba de "aguja en un pajar" con la que se miden los modelos de contexto largo. Prueba "¿quién estaba de guardia en el incidente 4419?" o "¿qué flag desactiva la telemetría?".

Showcase — Buscador de aguja

Un muro de notas de release y de ops ya está en el prompt; pide un dato enterrado y el modelo lo saca — la prueba de "aguja en un pajar" con la que se miden los modelos de contexto largo. Prueba "¿quién estaba de guardia en el incidente 4419?" o "¿qué flag desactiva la telemetría?".

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/haystack.py — el documento largo (compartido por ambos proveedores).
  • backend/ai_openai.py / backend/ai_gemini.py — responden una pregunta a partir del pajar completo.
  • frontend/app/page.tsx — caja de pregunta, respuesta con el detalle exacto.

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 needle-finder.zip

unzip needle-finder.zip
cd needle-finder
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 3 (OpenAI): find a needle in a long document.

A wall of release notes and ops notes goes into the prompt; you ask for one
buried fact and the model pulls it out. This is the "needle in a haystack" test
that long-context models are measured on — no retrieval, the whole document is
just there.
"""
from openai import OpenAI

from haystack import HAYSTACK

_client = OpenAI()

_MODEL = "gpt-5.4-nano"


def run(question: str) -> str:
    q = question.strip()
    if not q:
        return "Ask about a fact in the document (e.g. 'who was on call for incident 4419?')."
    response = _client.responses.create(
        model=_MODEL,
        instructions=(
            "Answer the question using ONLY the document below. Quote the exact "
            "detail. If it isn't in the document, say so.\n\n" + HAYSTACK
        ),
        input=[{"role": "user", "content": q}],
    )
    return response.output_text

backend/ai_gemini.py

"""Showcase 3 (Gemini): find a needle in a long document.

Same haystack, same buried-fact lookup, on Gemini.
"""
import os

from google import genai
from google.genai import types

from haystack import HAYSTACK

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

_MODEL = "gemini-3.1-flash-lite"


def run(question: str) -> str:
    q = question.strip()
    if not q:
        return "Ask about a fact in the document (e.g. 'who was on call for incident 4419?')."
    response = _client.models.generate_content(
        model=_MODEL,
        contents=[types.Content(role="user", parts=[types.Part(text=q)])],
        config=types.GenerateContentConfig(
            system_instruction=(
                "Answer the question using ONLY the document below. Quote the exact "
                "detail. If it isn't in the document, say so.\n\n" + HAYSTACK
            ),
        ),
    )
    return response.text or ""

Archivos del proyecto

  • .gitignore
  • README.es.md
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/haystack.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