Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
805f41a
Separate out one inference call for abstract LLMComponent
mart-r Nov 5, 2026
4ea3f53
Move one chat to its separate method in preparation for multiprocessing
mart-r Nov 5, 2026
91e8843
Create greater separation for LLM-interactions for future multiproces…
mart-r Nov 5, 2026
cbc318a
Minot linting fix
mart-r Nov 5, 2026
e534834
Move stuff around in prep for multiprocessing
mart-r Nov 5, 2026
23442de
Do linker context preparation once
mart-r Nov 5, 2026
e982ae9
Allow multiprocessing for linker
mart-r Nov 5, 2026
52f764d
Fix minor issue with linking
mart-r Nov 9, 2026
d963226
Include status in candidates
mart-r Nov 9, 2026
77268a9
Allow skipping clear-match concept queries
mart-r Nov 9, 2026
0ddd934
Update prompt somewhat
mart-r Nov 9, 2026
5a17e07
Remove debug output
mart-r Nov 9, 2026
cdffcb1
Add pure LLM component
mart-r Nov 16, 2026
aea0706
Registr pure LLM component
mart-r Nov 16, 2026
7b1851e
Update base in line with changes for pure-llm component
mart-r Nov 16, 2026
41dcff7
Add tests for new component
mart-r Nov 16, 2026
b99610f
Add new component to registration tests
mart-r Nov 16, 2026
9b30926
Linting fixes
mart-r Nov 16, 2026
77f8053
Use filters instead of entire CUI list
mart-r Nov 17, 2026
bb1daaa
Add in-flight recalculation based on filters
mart-r Nov 17, 2026
cfba599
Fix no-filter case
mart-r Nov 17, 2026
abc34d8
Update model creation to allow creating PureLLM component
mart-r Nov 17, 2026
a474c64
Add a few more pure LLM tests (e.g for filters)
mart-r Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 111 additions & 85 deletions medcat-plugins/llm-comps/src/medcat_llm_components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,78 @@ class LLMConnectionConfig(ComponentConfig):
def get_init_attrs(cls) -> list[str]:
return ['base_url', 'model']

def one_chat(
cnf: LLMConnectionConfig,
client: OpenAI,
prompt: str,
schema: dict[str, Any] | None = None,
request_kwargs: dict[str, Any] | None = None,
) -> str:
"""Send `prompt` as a single user message, return the raw text
response. `schema`, if given, requests structured output for
this call specifically (falls back to freeform if the backend
rejects it). Retries transient connection/timeout failures.

`request_kwargs` is merged into the chat-completions call for
backend-specific options (for example disabling reasoning).
Model, messages, temperature, and timeout always come from `cnf`.
"""
structured_output_supported = cnf.use_structured_output
use_schema = schema if structured_output_supported else None

last_exc: Exception | None = None
for attempt in range(cnf.retries + 1):
try:
return _one_call(
cnf, client, prompt, use_schema, request_kwargs)
except _StructuredOutputUnsupported:
logger.warning(
"%s: backend rejected structured output, falling "
"back to freeform for the rest of this session",
cnf.comp_name)
structured_output_supported = False
use_schema = None
continue # retry immediately, don't burn a retry slot on this
except (APIConnectionError, APITimeoutError, APIError) as exc:
last_exc = exc
logger.warning(
"%s: LLM call failed (attempt %d/%d): %s",
cnf.comp_name, attempt + 1, cnf.retries + 1, exc)
if attempt < cnf.retries:
time.sleep(cnf.retry_backoff_seconds)
assert last_exc is not None
raise last_exc


def _one_call(
cnf: LLMConnectionConfig,
client: OpenAI,
prompt: str,
schema: dict[str, Any] | None,
request_kwargs: dict[str, Any] | None = None,
) -> str:
kwargs: dict[str, Any] = {}
if request_kwargs:
kwargs.update(request_kwargs)
kwargs.update({
"model": cnf.model,
"messages": [{"role": "user", "content": prompt}],
"temperature": cnf.temperature,
"timeout": cnf.timeout,
})
if schema is not None:
kwargs["response_format"] = {
"type": "json_schema",
"json_schema": {"name": "response", "schema": schema, "strict": True},
}
try:
resp = client.chat.completions.create(**kwargs)
except APIError as exc:
if schema is not None and _looks_like_unsupported_response_format(exc):
raise _StructuredOutputUnsupported from exc
raise
return resp.choices[0].message.content or ""


class AbstractLLMEntityComponent(AbstractEntityProvidingComponent, ABC):
"""Shared connection / chat / cleanup plumbing for LLM-based components.
Expand All @@ -94,91 +166,45 @@ def __init__(self, cnf: LLMConnectionConfig) -> None:
# don't keep paying a failed round-trip to rediscover that
self._structured_output_supported = cnf.use_structured_output

def _chat(self, prompt: str, schema: dict[str, Any] | None = None) -> str:
"""Send `prompt` as a single user message, return the raw text
response. `schema`, if given, requests structured output for
this call specifically (falls back to freeform if the backend
rejects it). Retries transient connection/timeout failures."""
use_schema = schema if self._structured_output_supported else None

last_exc: Exception | None = None
for attempt in range(self.cnf.retries + 1):
try:
return self._one_call(prompt, use_schema)
except _StructuredOutputUnsupported:
logger.warning(
"%s: backend rejected structured output, falling "
"back to freeform for the rest of this session",
self.cnf.comp_name)
self._structured_output_supported = False
use_schema = None
continue # retry immediately, don't burn a retry slot on this
except (APIConnectionError, APITimeoutError, APIError) as exc:
last_exc = exc
logger.warning(
"%s: LLM call failed (attempt %d/%d): %s",
self.cnf.comp_name, attempt + 1, self.cnf.retries + 1, exc)
if attempt < self.cnf.retries:
time.sleep(self.cnf.retry_backoff_seconds)
assert last_exc is not None
raise last_exc

def _one_call(self, prompt: str, schema: dict[str, Any] | None) -> str:
kwargs: dict[str, Any] = {
"model": self.cnf.model,
"messages": [{"role": "user", "content": prompt}],
"temperature": self.cnf.temperature,
"timeout": self.cnf.timeout,
}
if schema is not None:
kwargs["response_format"] = {
"type": "json_schema",
"json_schema": {"name": "response", "schema": schema, "strict": True},
}
try:
resp = self._client.chat.completions.create(**kwargs)
except APIError as exc:
if schema is not None and _looks_like_unsupported_response_format(exc):
raise _StructuredOutputUnsupported from exc
raise
return resp.choices[0].message.content or ""

_FENCE_RE = re.compile(r"^```[a-zA-Z]*\n|\n```$")
_TABLE_ROW_RE = re.compile(r"^\|.*\|$")
_TABLE_SEP_RE = re.compile(r"^[\s|:-]+$")

def _clean_response(self, raw: str) -> str:
text = raw.strip()
# models wrap output in ```csv/```json fences despite instructions not to
text = self._FENCE_RE.sub("", text).strip()
return self._strip_markdown_table(text)

def _strip_markdown_table(self, text: str) -> str:
r"""Small/instruction-weak models sometimes wrap the requested
CSV/plain output in a markdown table instead - either one field
per cell (`| entity | start | end |`) or, just as often, the
whole line squished into a single cell (`| entity,start,end |`).
Detected structurally (every non-empty line is pipe-wrapped) so
it doesn't depend on the exact header text; both variants above
collapse to the same comma-joined output.

Only triggers when the *entire* response looks like a table.
Prose mixed with a table (e.g. "Here are the entities:\n| ... |")
isn't handled - that needs pulling the table out of surrounding
text, a step up in complexity from a plain cleanup pass.
"""
non_empty = [ln.strip() for ln in text.splitlines() if ln.strip()]
if not non_empty or not all(
self._TABLE_ROW_RE.match(ln) for ln in non_empty):
return text

out_lines = []
for ln in non_empty:
if self._TABLE_SEP_RE.match(ln):
continue # header-separator row
cells = [c.strip() for c in ln.strip("|").split("|")]
out_lines.append(",".join(cells) if len(cells) > 1 else cells[0])
return "\n".join(out_lines)

_FENCE_RE = re.compile(r"^```[a-zA-Z]*\n|\n```$")
_TABLE_ROW_RE = re.compile(r"^\|.*\|$")
_TABLE_SEP_RE = re.compile(r"^[\s|:-]+$")


def clean_response(raw: str) -> str:
text = raw.strip()
# models wrap output in ```csv/```json fences despite instructions not to
text = _FENCE_RE.sub("", text).strip()
return _strip_markdown_table(text)


def _strip_markdown_table(text: str) -> str:
r"""Small/instruction-weak models sometimes wrap the requested
CSV/plain output in a markdown table instead - either one field
per cell (`| entity | start | end |`) or, just as often, the
whole line squished into a single cell (`| entity,start,end |`).
Detected structurally (every non-empty line is pipe-wrapped) so
it doesn't depend on the exact header text; both variants above
collapse to the same comma-joined output.

Only triggers when the *entire* response looks like a table.
Prose mixed with a table (e.g. "Here are the entities:\n| ... |")
isn't handled - that needs pulling the table out of surrounding
text, a step up in complexity from a plain cleanup pass.
"""
non_empty = [ln.strip() for ln in text.splitlines() if ln.strip()]
if not non_empty or not all(
_TABLE_ROW_RE.match(ln) for ln in non_empty):
return text

out_lines = []
for ln in non_empty:
if _TABLE_SEP_RE.match(ln):
continue # header-separator row
cells = [c.strip() for c in ln.strip("|").split("|")]
out_lines.append(",".join(cells) if len(cells) > 1 else cells[0])
return "\n".join(out_lines)


class MisconfiguredComponentException(ValueError):
Expand Down
Loading
Loading