Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/services/captures.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ async def refine_capture(
row.transcript_raw or "",
flags,
model_size=model_size,
language=row.language,
)

row.transcript_refined = refined
Expand Down
52 changes: 49 additions & 3 deletions backend/services/refinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,48 @@ def from_dict(cls, data: dict | None) -> "RefinementFlags":
For example, "run npm install then cd into src slash components and edit index dot tsx" yields "Run npm install then cd into src/components and edit index.tsx.\""""


def build_refinement_prompt(flags: RefinementFlags) -> str:
"""Assemble the system prompt for a given flag combination."""
# Whisper reports the transcript language as an ISO-639-1 style code (or
# None when detection ran on its own). The refinement prompt needs the
# human-readable name so the constraint reads naturally to the model.
_LANGUAGE_NAMES = {
"zh": "Chinese",
"en": "English",
"ja": "Japanese",
"ko": "Korean",
"de": "German",
"fr": "French",
"ru": "Russian",
"pt": "Portuguese",
"es": "Spanish",
"it": "Italian",
}

_PRESERVE_LANGUAGE_KNOWN = """Output language — the transcript you are about to read is in {language}. Your entire output MUST be written in {language}.

The instructions and few-shot examples in this prompt are in English; that is prompt plumbing, not the output language. Never translate the transcript. A {language} transcript stays {language}, the speaker's word choices included."""

_PRESERVE_LANGUAGE_UNKNOWN = """Output language — your entire output MUST be written in the same language as the transcript itself.

The instructions and few-shot examples in this prompt are in English; that is prompt plumbing, not the output language. Never translate the transcript, no matter which language it arrives in."""


def build_refinement_prompt(flags: RefinementFlags, language: str | None = None) -> str:
"""Assemble the system prompt for a given flag combination.

``language`` is the Whisper language code of the transcript (e.g. "zh")
when known, else None. Either way the prompt pins the output to the
transcript's own language: without an explicit constraint, the English
instructions and English few-shot examples below train small refinement
models to silently translate non-English transcripts into English (#603).
"""
sections = [_BASE_INSTRUCTIONS]

if language:
language_name = _LANGUAGE_NAMES.get(language.lower(), language)
sections.append(_PRESERVE_LANGUAGE_KNOWN.format(language=language_name))
else:
sections.append(_PRESERVE_LANGUAGE_UNKNOWN)

if flags.smart_cleanup:
sections.append(_SMART_CLEANUP)
if flags.self_correction:
Expand Down Expand Up @@ -269,9 +307,17 @@ async def refine_transcript(
transcript: str,
flags: RefinementFlags,
model_size: str | None = None,
language: str | None = None,
) -> tuple[str, str]:
"""Run the transcript through the LLM with the built system prompt.

Args:
transcript: Raw STT output to clean up.
flags: Refinement behaviour toggles.
model_size: Optional refinement model override.
language: Whisper language code of the transcript (e.g. "zh"), when
known. Pins the refined output to the transcript's language.

Returns:
(refined_text, llm_model_size) — so callers can persist which model
produced the refinement.
Expand All @@ -283,7 +329,7 @@ async def refine_transcript(
# to reason about obvious STT garbage (see ``collapse_repetitive_artifacts``).
cleaned_input = collapse_repetitive_artifacts(transcript)

system_prompt = build_refinement_prompt(flags)
system_prompt = build_refinement_prompt(flags, language)
text = await backend.generate(
prompt=cleaned_input,
system=system_prompt,
Expand Down
44 changes: 44 additions & 0 deletions backend/tests/test_refinement_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""The refinement prompt must pin output language to the transcript's language.

Regression tests for #603: the refinement system prompt is written in English
with English-only few-shot examples, and without an explicit language
constraint small refinement models silently translate non-English transcripts
into English. These tests pin the prompt-level guardrails (no model needed).
"""

from backend.services.refinement import RefinementFlags, build_refinement_prompt


def test_known_language_pins_output_language():
prompt = build_refinement_prompt(RefinementFlags(), language="zh")
assert "Chinese" in prompt
assert "MUST be written in Chinese" in prompt
assert "Never translate the transcript" in prompt


def test_unknown_language_falls_back_to_same_language_rule():
prompt = build_refinement_prompt(RefinementFlags())
assert "same language as the transcript" in prompt
assert "Never translate the transcript" in prompt


def test_language_rule_coexists_with_flag_sections():
prompt = build_refinement_prompt(
RefinementFlags(smart_cleanup=True, self_correction=True, preserve_technical=True),
language="ko",
)
assert "Korean" in prompt
# The behaviour sections are still present alongside the language rule.
assert "disfluencies" in prompt.lower()
assert "correction cue" in prompt.lower()
assert "dot" in prompt # preserve_technical symbol mapping survives


def test_unmapped_language_code_passes_through():
prompt = build_refinement_prompt(RefinementFlags(), language="sv")
assert "MUST be written in sv" in prompt


def test_language_code_is_case_insensitive():
prompt = build_refinement_prompt(RefinementFlags(), language="ZH")
assert "MUST be written in Chinese" in prompt