Course EN
← back to chapter

Estilo few-shot

Enseña un estilo en el prompt en lugar de hacer fine-tuning para lograrlo. Unos cuantos pares de ejemplo en la conversación te acercan casi por completo a una voz a la medida — sin entrenamiento, sin costo, iteración instantánea. Lo que hay que probar ANTES de hacer fine-tuning.

Showcase — Estilo few-shot

Enseña un estilo en el prompt en lugar de hacer fine-tuning para lograrlo. Unos cuantos pares de ejemplo en la conversación te acercan casi por completo a una voz a la medida — sin entrenamiento, sin costo, iteración instantánea. Lo que hay que probar ANTES de hacer fine-tuning.

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 — pares de ejemplo few-shot + tu línea.
  • frontend/app/page.tsx — línea a la entrada, línea con estilo 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 few-shot-style.zip

unzip few-shot-style.zip
cd few-shot-style
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 2 (OpenAI): the alternative to fine-tuning — few-shot.

Before you fine-tune for a style, try teaching it in the prompt. A handful of
example pairs in the conversation gets you most of the way to a custom voice with
zero training, zero cost, and instant iteration. Type a line and watch few-shot
examples bend the model into pirate speak.
"""
from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-5.4-nano"

# The "training set" — but it lives in the prompt, not in a fine-tune.
_EXAMPLES = [
    ("hello there", "Ahoy there, matey!"),
    ("where is the treasure?", "Arr, where be the treasure buried?"),
    ("I am hungry", "Me belly be growlin' for grub!"),
]


def run(text: str) -> str:
    line = text.strip()
    if not line:
        return "Type a line to translate into pirate speak."
    messages = []
    for user, assistant in _EXAMPLES:
        messages += [{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]
    messages.append({"role": "user", "content": line})
    response = _client.responses.create(
        model=_MODEL,
        instructions="Rewrite the user's line in pirate speak, matching the style of the examples.",
        input=messages,
    )
    return response.output_text

backend/ai_gemini.py

"""Showcase 2 (Gemini): the alternative to fine-tuning — few-shot.

Same few-shot style transfer, on Gemini, using multi-turn history as the examples.
"""
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"

_EXAMPLES = [
    ("hello there", "Ahoy there, matey!"),
    ("where is the treasure?", "Arr, where be the treasure buried?"),
    ("I am hungry", "Me belly be growlin' for grub!"),
]


def run(text: str) -> str:
    line = text.strip()
    if not line:
        return "Type a line to translate into pirate speak."
    contents = []
    for user, assistant in _EXAMPLES:
        contents.append(types.Content(role="user", parts=[types.Part(text=user)]))
        contents.append(types.Content(role="model", parts=[types.Part(text=assistant)]))
    contents.append(types.Content(role="user", parts=[types.Part(text=line)]))
    response = _client.models.generate_content(
        model=_MODEL, contents=contents,
        config=types.GenerateContentConfig(
            system_instruction="Rewrite the user's line in pirate speak, matching the style of the examples.",
        ),
    )
    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