Fix ai-service Dockerfile: bake in drug_entities.json, override its path

This commit is contained in:
2026-08-10 10:35:13 +07:00
parent a4b8e1c4db
commit 60b4397032
51 changed files with 4302 additions and 2087 deletions
+18
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import functools
import re
from dataclasses import dataclass, replace
from difflib import SequenceMatcher
@@ -55,6 +56,20 @@ class CatalogDrugResolver:
self._fuzzy_threshold = fuzzy_threshold
self._ambiguity_margin = ambiguity_margin
# Measured live 2026-08-07: a single `resolve()` call over the real
# ~10,164-alias catalog costs ~0.65-0.7s, `suggest()` ~0.94-0.97s — both
# O(aliases) regex/SequenceMatcher work, pure functions of their
# arguments (only `self._aliases` et al, fixed at construction, feed
# them). `understanding.py`'s `_candidate_ids` calls both PER HISTORY
# LINE on every single turn — so the SAME already-seen history lines
# were being re-resolved from scratch every turn a conversation grew,
# ~1.6-1.7s of pure CPU per repeated line. A real user's ordinary
# multi-turn conversation was enough to exceed the 20s F-08 budget
# before the first Bedrock call ever ran, surfacing as a false
# "Dịch vụ đang gặp sự cố" — not a provider outage at all. Caching by
# exact input turns all but the newest turn's own text into a dict
# lookup on every subsequent call.
@functools.lru_cache(maxsize=4096)
def resolve(self, query: str) -> DrugResolution:
normalized_query = normalize_name(query)
query_tokens = normalized_query.split()
@@ -140,6 +155,9 @@ class CatalogDrugResolver:
break
return ordered
# See the comment on `resolve` above — same cost, same fix, same
# single-caller read-only usage (safe to hand back a cached list).
@functools.lru_cache(maxsize=4096)
def suggest(
self, query: str, k: int = 3, min_score: float = 0.5
) -> list[tuple[str, float]]: