Course ES
← back to chapter

Few-shot style

Teach a style in the prompt instead of fine-tuning for it. A few example pairs in the conversation get you most of the way to a custom voice — no training, no cost, instant iteration. The thing to try BEFORE you fine-tune.

Showcase — Few-shot style

Teach a style in the prompt instead of fine-tuning for it. A few example pairs in the conversation get you most of the way to a custom voice — no training, no cost, instant iteration. The thing to try BEFORE you fine-tune.

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 — few-shot example pairs + your line.
  • frontend/app/page.tsx — line in, styled line 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 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

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 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 ""

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