MedCAT LLM component examples
This project aims to provide a few simple examples for LLM based components (for NER and/or linking).
These are not designed to be the best implementation. They are designed as an example.
How to install
pip install medcat-llm-components
How to use
Add to existing model
This way you can use the CDB already tied to the model.
from medcat.cat import CAT
from medcat_llm_components.ner import LLMNERConfig
from medcat_llm_components.linker import LLMLinkConfig
### INPUT ###
# existing model
model_path = ""
# the URL to the (e.g) ollama instance
base_url = "my_ollama_ip:port/whatever"
# the model to use
llm_model = "gemma:2b"
### AUTOMATION ###
# NOTE: don't need to use both NER and linker
# we just have one example for both
cat = CAT.load_model_pack(model_path)
# create configs
# ner
ner_cnf = LLMNERConfig(
base_url=base_url,
model=llm_model,
# for other optional arguments such as prompt
# refer to code or IDE inspection
)
# linker
linking_cnf = LLMLinkConfig(
base_url=base_url,
model=llm_model,
# for other optional arguments such as prompt
# refer to code or IDE inspection
)
# update model pack
# ner
cat.config.components.ner.comp_name = "llm_ner"
# NOTE: different path for NER and linker
cat.config.components.ner.custom_cnf = ner_cnf
# linking
cat.config.components.linking.comp_name = "llm_linker"
# NOTE: different path for NER and linker
cat.config.components.linking.additional = linking_cnf
# recreate pipe
cat._recreate_pipe()
print(cat.describe_pipeline())
# ready to use!
print(cat.get_entities("Anhedonia"))
Create as part of a new model pack
You can create a new model pack yourself. This will almost certainly be more involved.
from medcat.cat import CAT
from medcat.cdb import CDB
from medcat.vocab import Vocab
from medcat.config import Config
from medcat_llm_components.ner import LLMNERConfig
from medcat_llm_components.linker import LLMLinkConfig
# the URL to the (e.g) ollama instance
base_url = "my_ollama_ip:port/whatever"
# the model to use
llm_model = "gemma:2b"
# create configs
# ner
ner_cnf = LLMNERConfig(
base_url=base_url,
model=llm_model,
# for other optional arguments such as prompt
# refer to code or IDE inspection
)
# linker
linking_cnf = LLMLinkConfig(
base_url=base_url,
model=llm_model,
# for other optional arguments such as prompt
# refer to code or IDE inspection
)
config = Config()
config.components.ner.comp_name = "llm_ner"
config.components.ner.custom_cnf = ner_cnf
config.components.linking.comp_name = "llm_linker"
config.components.linking.additional = linking_cnf
vocab = Vocab()
# or load a CDB on its own with CDB.load("my_cdb.zip")
cdb = CDB(config)
cat = CAT(cdb, vocab)
print(cat.describe_pipeline())
# ready to use!
Instruct an existing LLM-component-saved model:
If you've already got a model that's saved with the config specific to your use case (e.g URL and model and the like) then you can just load it up and use it like any other model.
However, if you've got a model saved with the right components, but the wrong URL or model (or you just wish to change it) you can load and change these in one go as follows:
from medcat.cat import CAT
MODEL_PATH = "my_model_path"
LLM_URL = "api_endpoint_url:80/v1"
LLM_MODEL = "gemma:2b"
# NOTE: this is only necessary if the URL or other
# info has changed between save and load
config_dict = {
"components": {
# for NER
"ner": {
"custom_cnf": {
"base_url": LLM_URL,
"model": LLM_MODEL,
}
},
# for linking
"linking": {
"additional": {
"base_url": LLM_URL,
"model": LLM_MODEL,
}
}
}
}
cat = CAT.load_model_pack(
MODEL_PATH, config_dict=config_dict,
)
print(
"CAT.get_entities",
cat.get_entities("Patient had kidney disease")['entities']
)
Metadata
Release files for medcat-llm-components 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| medcat_llm_components-0.1.0.tar.gz | 15.2 kB | With provenance | Details for medcat_llm_components-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| medcat_llm_components-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for medcat_llm_components-0.1.0-py3-none-any.whl |
Total release size: 26.4 kB
Release files / medcat_llm_components-0.1.0.tar.gz
| Download URL | medcat_llm_components-0.1.0.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91f80c0f592acae5d6fe110ad3eb32b6b65aefa8df3a715b620d7ee8985270f1
|
|
BLAKE2b-256 checksum How to use checksums |
553cfeb4a503d433058031fb7267cfd4ee9f5be2a3ce35a0f5c25b0f2a3ea3f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logRelease files / medcat_llm_components-0.1.0-py3-none-any.whl
| Download URL | medcat_llm_components-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb644a5cdef0b80791b5ff67e14dbe2e9c3c36d11b32a158a2943d611c2ec735
|
|
BLAKE2b-256 checksum How to use checksums |
4f8eddf270782e15c479f9d3ae47d17bef191ed3228b84343ff3e65118112720
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logSecurity for medcat-llm-components 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for medcat_llm_components-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.