Add batch generation function using sonnet-4.6

This commit is contained in:
2026-04-05 02:25:24 -06:00
parent dec6211f7c
commit baa6db5aa2
2 changed files with 42 additions and 0 deletions
+3
View File
@@ -62,3 +62,6 @@ upload-history.json
# Git worktrees # Git worktrees
.worktrees/ .worktrees/
# Scratch/smoke tests (not committed)
scratch/
+39
View File
@@ -2,6 +2,13 @@ from dataclasses import dataclass
from typing import Optional from typing import Optional
import json import json
import httpx
from ..config import settings
from .cost_tracker import cost_tracker
BATCH_MODEL = "anthropic/claude-sonnet-4.6"
REQUIRED_FIELDS = { REQUIRED_FIELDS = {
"name", "age", "voice_suggestion", "location", "identity", "name", "age", "voice_suggestion", "location", "identity",
"situation", "reason_calling", "opening_line", "secret_want", "situation", "reason_calling", "opening_line", "secret_want",
@@ -88,3 +95,35 @@ def build_batch_prompt(ctx: dict) -> str:
lines.append("") lines.append("")
lines.append(f"Generate {ctx['caller_count']} callers. Output JSON only, no prose.") lines.append(f"Generate {ctx['caller_count']} callers. Output JSON only, no prose.")
return BATCH_SYSTEM_PROMPT + "\n\n" + "\n".join(lines) return BATCH_SYSTEM_PROMPT + "\n\n" + "\n".join(lines)
async def generate_batch(ctx: dict) -> list[CallerIdentity]:
"""Call sonnet-4.6 with the batch prompt, parse + voice-resolve the response."""
prompt = build_batch_prompt(ctx)
async with httpx.AsyncClient(timeout=120.0) as client:
resp = await client.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={"Authorization": f"Bearer {settings.openrouter_api_key}"},
json={
"model": BATCH_MODEL,
"messages": [{"role": "user", "content": prompt}],
"response_format": {"type": "json_object"},
"max_tokens": 8000,
"temperature": 0.9,
},
)
resp.raise_for_status()
data = resp.json()
content = data["choices"][0]["message"]["content"]
usage = data.get("usage", {})
cost_tracker.record_llm_call(
category="background_gen",
model=BATCH_MODEL,
usage_data=usage,
)
callers = parse_batch_response(content)
for c in callers:
c.voice_resolved = resolve_voice(c.voice_suggestion, ctx["voice_roster"])
return callers