Builds PaperTrail from the base skeleton into a full evidence-verification platform for the "Built with Claude: Life Sciences" hackathon (Builder Track). New named-user features (Build Beyond the Bench): - Lab Notebook Companion — rough bench notes -> structured, searchable, grounded experiment records (lib/labNotebook, /console/lab-notebook, migration 0059) - Clinical Trial Matcher — de-identified patient notes -> ranked eligible ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning (lib/trialMatcher, /console/trial-matcher, migrations 0060) Platform: - Deterministic verification moat (biostats recompute, exact-span grounding) - 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar, PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier) - Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise governance (RBAC, /api/v1 gateway, metering, audit chain, retention) - 17 OSS projects assimilated into native TypeScript on our stack (backend/engines retained + credited in NOTICE) - Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site - Next.js 16 upgrade, benchmark harness (honest smoke-test caveat) Licensing: Apache-2.0 (LICENSE + NOTICE).
83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
"""Token estimation utilities with optional tokenizer-backed support."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
class TokenEstimator:
|
|
"""Estimate token counts for chat messages and plain text."""
|
|
|
|
def __init__(self, heuristic_chars_per_token: float = 4.0):
|
|
self.heuristic_chars_per_token = heuristic_chars_per_token
|
|
self._tiktoken = None
|
|
self._encodings = {}
|
|
try:
|
|
import tiktoken # type: ignore
|
|
|
|
self._tiktoken = tiktoken
|
|
except Exception:
|
|
self._tiktoken = None
|
|
|
|
def estimate_text_tokens(self, text: str, model_hint: Optional[str] = None) -> int:
|
|
if not text:
|
|
return 0
|
|
encoding = self._get_encoding(model_hint)
|
|
if encoding is not None:
|
|
try:
|
|
return len(encoding.encode(text))
|
|
except Exception:
|
|
pass
|
|
return max(1, math.ceil(len(text) / self.heuristic_chars_per_token))
|
|
|
|
def estimate_chat_tokens(self, messages: List[Dict[str, Any]], model_hint: Optional[str] = None) -> int:
|
|
if not messages:
|
|
return 0
|
|
# OpenAI chat messages have per-message overhead; keep conservative defaults.
|
|
message_overhead = 4
|
|
reply_overhead = 2
|
|
total = reply_overhead
|
|
for message in messages:
|
|
total += message_overhead
|
|
total += self.estimate_text_tokens(str(message.get("role", "")), model_hint)
|
|
content = message.get("content", "")
|
|
total += self.estimate_text_tokens(self._content_to_text(content), model_hint)
|
|
if "name" in message:
|
|
total += self.estimate_text_tokens(str(message["name"]), model_hint)
|
|
return max(total, 1)
|
|
|
|
def _get_encoding(self, model_hint: Optional[str]):
|
|
if self._tiktoken is None:
|
|
return None
|
|
key = model_hint or "default"
|
|
if key in self._encodings:
|
|
return self._encodings[key]
|
|
try:
|
|
if model_hint:
|
|
encoding = self._tiktoken.encoding_for_model(model_hint)
|
|
else:
|
|
encoding = self._tiktoken.get_encoding("cl100k_base")
|
|
except Exception:
|
|
try:
|
|
encoding = self._tiktoken.get_encoding("cl100k_base")
|
|
except Exception:
|
|
encoding = None
|
|
self._encodings[key] = encoding
|
|
return encoding
|
|
|
|
@staticmethod
|
|
def _content_to_text(content: Any) -> str:
|
|
if isinstance(content, str):
|
|
return content
|
|
if isinstance(content, list):
|
|
parts = []
|
|
for item in content:
|
|
if isinstance(item, dict):
|
|
parts.append(str(item.get("text", "")))
|
|
else:
|
|
parts.append(str(item))
|
|
return "\n".join(parts)
|
|
return str(content)
|
|
|