Why: the pilot in data/ross showed that Ross Cameron's daily recaps, joined to
the SIP tape and to what our bot saw, answer "which of his trades could we have
taken and why didn't we". It was a set of one-off scripts run by hand; the
server-side youtube_claims pipeline has been dead since ~08-24 (YouTube blocks
the TrueNAS IP) and its captions fallback hid that. This makes the pilot a
nightly, local, cumulative loop without a paid LLM API.
youtube_claims/recap/ (python -m youtube_claims.recap.nightly):
- channel: yt-dlp flat listing of @DaytradeWarrior, m4a audio, faster-whisper
small.en on CPU, pilot-format bundles (meta + our scanner list + transcript).
- extract: Ollama structured output (JSON schema) per EXTRACT_SPEC, chunked
with overlap for long transcripts and merged. The verbatim rule is enforced in
code: every quote must be found (token-level, one-word tolerance) and is
snapped to the transcript's words; unquoted items, trades with no surviving
quote, prices never spoken, and passed-on names never said are dropped.
- llm: endpoint pool over RECAP_OLLAMA_HOSTS (round-robin + failover, per-host
model/ctx/think pins) since the owner runs several Ollama boxes.
- tape/join/ourside: ported join.py/ourside.py -- tickers are price-verified on
the Alpaca SIP tape (plus an order-free check for his own named ticker), then
paper (:5433) and live (:5434) event views are attached. Bars cached on disk.
- store + schema.sql: idempotent recap_* tables in the transcripts schema,
upserted per video_id. report: the pilot's sections + "what changed since last
run" against the previous day's metrics JSON.
- evaluate: scores any model/prompt against the 36 gold extractions.
Model choice (36 gold videos, final code path, P/R):
gpt-oss:20b on the workstation GPU, two focused calls, think=low:
trades P .84 R .70; tickers P .82 R .77; entry +-2% P .54 R .50;
passed-on P .66 R .76; temperature 16/31; kind 30/36; ~49 s/video.
tools:latest (4B, server) one call: trades P .81 R .72, tickers P .83 R .85,
passed-on P .80 R .75, temperature 20/31, but it invents prices
(entry P .42) and occasionally runs away (5-9 min). Prices drive the tape
verification, so the more precise local model is the default.
Iterations: prompt v2 + spoken-price rescale (699 -> 6.99) + split calls took
gpt-oss trades F1 .74 -> .76-.78 and tickers .78 -> .79-.80; think=medium
blew the token budget. qwen3-30b-a3b on mason only finished 5 videos (host
saturated) -- inconclusive.
Also: youtube_claims/transcription.py::_captions_fallback now picks the
youtube-transcript-api shape by feature detection instead of retrying the
removed get_transcript on any error, and transcribe() reports both the whisper
and captions causes -- the IpBlocked error was being masked as AttributeError.
No trading code touched.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
4.2 KiB
Python
95 lines
4.2 KiB
Python
"""Anti-hallucination: every quote the LLM returns must be findable in the transcript.
|
|
|
|
Extends youtube_claims.extraction._quote_supported (whitespace-normalized 40-char prefix match,
|
|
which accepts a quote whose tail is invented) with a full-length, punctuation-insensitive token
|
|
match plus a small fuzzy tolerance for a mis-copied word,
|
|
and SNAPS an accepted quote to the transcript's own words -- so what we store is verbatim
|
|
source text, never the model's paraphrase of it. A quote that can't be located is rejected
|
|
and the caller drops the item it supported."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from difflib import SequenceMatcher
|
|
|
|
from youtube_claims.extraction import _norm
|
|
|
|
_TOKEN = re.compile(r"[a-z0-9]+(?:[.,'][a-z0-9]+)*")
|
|
|
|
|
|
def _tok(s: str) -> list[str]:
|
|
"""Lowercased word tokens; '$5.50' -> '5.50', "don't" -> "don't", 'up-to' -> 'up','to'."""
|
|
return _TOKEN.findall((s or "").lower())
|
|
|
|
|
|
class Transcript:
|
|
"""A transcript indexed for quote lookup. Keeps the original words so a matched quote can
|
|
be returned exactly as transcribed (ASR noise included)."""
|
|
|
|
def __init__(self, text: str):
|
|
self.text = text or ""
|
|
self.ntext = _norm(self.text)
|
|
self.words = self.text.split()
|
|
self.wtoks = [_tok(w) for w in self.words] # tokens per original word
|
|
self.toks: list[str] = []
|
|
self.tok_word: list[int] = [] # token index -> word index
|
|
for wi, ts in enumerate(self.wtoks):
|
|
for t in ts:
|
|
self.toks.append(t)
|
|
self.tok_word.append(wi)
|
|
self.joined = " " + " ".join(self.toks) + " "
|
|
# number tokens also without thousands separators / leading '$' for price lookups
|
|
self.token_set = set(self.toks) | {t.replace(",", "") for t in self.toks}
|
|
self._first: dict[str, list[int]] = {}
|
|
for i, t in enumerate(self.toks):
|
|
self._first.setdefault(t, []).append(i)
|
|
|
|
def _span(self, i: int, n: int) -> str:
|
|
a, b = self.tok_word[i], self.tok_word[min(i + n, len(self.toks)) - 1]
|
|
return " ".join(self.words[a:b + 1])
|
|
|
|
def find(self, quote: str, min_ratio: float = 0.9) -> str | None:
|
|
"""The transcript text matching `quote`, or None if it isn't there.
|
|
Exact (token-level, punctuation-blind) first; then a fuzzy window of the same length
|
|
whose tokens agree >= min_ratio (tolerates one mis-copied word in ten)."""
|
|
q = _tok(quote)
|
|
if len(q) < 3: # too short to be evidence of anything
|
|
return None
|
|
needle = " " + " ".join(q) + " "
|
|
pos = self.joined.find(needle)
|
|
if pos >= 0:
|
|
start = self.joined[:pos + 1].count(" ") - 1
|
|
return self._span(start, len(q))
|
|
if len(q) < 6:
|
|
return None
|
|
n = len(q)
|
|
cands: set[int] = set()
|
|
for k in range(min(4, n)): # windows anchored on any of the
|
|
for i in self._first.get(q[k], ()): # quote's first four tokens
|
|
if i - k >= 0:
|
|
cands.add(i - k)
|
|
best, best_i = 0.0, None
|
|
for i in sorted(cands):
|
|
win = self.toks[i:i + n]
|
|
if len(win) < n * 0.8:
|
|
continue
|
|
r = SequenceMatcher(None, q, win, autojunk=False).ratio()
|
|
if r > best:
|
|
best, best_i = r, i
|
|
if best_i is None or best < min_ratio:
|
|
return None
|
|
# a dropped/added word shifts the true end: pick the window length that fits best
|
|
best_n = max(range(max(1, n - 2), n + 3),
|
|
key=lambda m: (SequenceMatcher(None, q, self.toks[best_i:best_i + m], autojunk=False).ratio(), -abs(m - n)))
|
|
return self._span(best_i, best_n)
|
|
|
|
def supports(self, quote: str) -> bool:
|
|
return self.find(quote) is not None
|
|
|
|
|
|
def transcript_text_from_bundle(bundle: str) -> str:
|
|
"""The transcript body of a bundle, without the [mm:ss] prefixes (segments joined by
|
|
spaces so a quote spanning two segments still matches)."""
|
|
body = bundle.split("TRANSCRIPT (mm:ss text):", 1)[-1]
|
|
return " ".join(re.sub(r"^\[\d+:\d+\]\s?", "", ln) for ln in body.splitlines() if ln.strip())
|