Summarize tone: re-summarize in a chosen voice (backend)

POST /reprocess?job=summarize&tone=<voice> persists the voice on the job
row (queue carries only ids, so a sweep re-enqueue keeps it), the LLM
prompt appends 'write every field in a <tone> tone — the tone colors
the wording, never the facts', and the resulting summary records its
tone (SummaryOut.tone). Plain re-summarize clears a previous tone.
Migration tone0000000001 (summaries.tone, processing_jobs.tone).
78 passed, 1 skipped; ruff clean.
This commit is contained in:
avi 2026-09-15 14:24:36 -05:00
commit b63f49241d
11 changed files with 82 additions and 13 deletions

View file

@ -112,7 +112,8 @@ class SummaryResult:
class LlmProvider(Protocol):
name: str
async def summarize(self, transcript: str, *, title: str | None = None) -> SummaryResult: ...
async def summarize(self, transcript: str, *, title: str | None = None,
tone: str | None = None) -> SummaryResult: ...
def get_transcription_provider(settings: Settings) -> TranscriptionProvider | None:

View file

@ -21,12 +21,21 @@ SYSTEM_PROMPT = (
MAX_TRANSCRIPT_CHARS = 12_000
def build_user_message(transcript: str, title: str | None) -> str:
def build_user_message(transcript: str, title: str | None,
tone: str | None = None) -> str:
text = transcript[:MAX_TRANSCRIPT_CHARS]
if len(transcript) > MAX_TRANSCRIPT_CHARS:
text += f"\n\n[truncated from {len(transcript)} chars]"
head = f'Title: "{title}"\n\n' if title else ""
return head + "Transcript:\n" + text
msg = head + "Transcript:\n" + text
if tone:
# Same JSON contract and same fidelity rules — only the voice changes.
msg += (
f"\n\nWrite every field of the JSON in a {tone} tone of voice. "
"Stay faithful to the transcript: the tone colors the wording, "
"never the facts."
)
return msg
def parse_summary(data: object, model: str) -> SummaryResult:

View file

@ -38,7 +38,8 @@ class OllamaProvider:
async with httpx.AsyncClient(timeout=self.timeout_s) as client:
yield client
async def summarize(self, transcript: str, *, title: str | None = None) -> SummaryResult:
async def summarize(self, transcript: str, *, title: str | None = None,
tone: str | None = None) -> SummaryResult:
payload = {
"model": self.model,
"stream": False,
@ -51,7 +52,8 @@ class OllamaProvider:
"options": {"num_ctx": 8192},
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": build_user_message(transcript, title)},
{"role": "user",
"content": build_user_message(transcript, title, tone)},
],
}
try:

View file

@ -42,7 +42,8 @@ class OpenAICompatProvider:
async with httpx.AsyncClient(timeout=self.timeout_s) as client:
yield client
async def summarize(self, transcript: str, *, title: str | None = None) -> SummaryResult:
async def summarize(self, transcript: str, *, title: str | None = None,
tone: str | None = None) -> SummaryResult:
headers = (
{"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
)
@ -52,7 +53,8 @@ class OpenAICompatProvider:
"response_format": {"type": "json_object"},
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": build_user_message(transcript, title)},
{"role": "user",
"content": build_user_message(transcript, title, tone)},
],
}
try:

View file

@ -437,12 +437,14 @@ async def run_summarize(ctx: dict, recording_id: str) -> None:
rec.processing_status = ProcessingStatus.processing
await session.commit() # visible before the long LLM call
try:
result = await provider.summarize(text, title=rec.title)
result = await provider.summarize(text, title=rec.title,
tone=job.tone)
except AIError as e:
await _fail(session, rec, job, str(e), ctx, e)
await session.commit()
return
await store_summary(session, rec, result.to_dict(), provider.name, result.model)
await store_summary(session, rec, result.to_dict(), provider.name,
result.model, tone=job.tone)
job.status = JobStatus.succeeded
job.stage = None
job.progress = 100
@ -506,6 +508,7 @@ async def store_summary(
content: dict,
provider_name: str,
model: str,
tone: str | None = None,
) -> None:
existing = (
await session.scalars(
@ -531,6 +534,7 @@ async def store_summary(
version=(max_version or 0) + 1,
provider=provider_name,
model=model,
tone=tone,
content=content,
edited_by_user=False,
)