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']
)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file medcat_llm_components-0.1.0.tar.gz.
File metadata
- Download URL: medcat_llm_components-0.1.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91f80c0f592acae5d6fe110ad3eb32b6b65aefa8df3a715b620d7ee8985270f1
|
|
| MD5 |
949494ae5d3bb94e1e7521465383894f
|
|
| BLAKE2b-256 |
553cfeb4a503d433058031fb7267cfd4ee9f5be2a3ce35a0f5c25b0f2a3ea3f2
|
Provenance
The following attestation bundles were made for medcat_llm_components-0.1.0.tar.gz:
Publisher:
medcat-llm-components_ci.yml on CogStack/cogstack-nlp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
medcat_llm_components-0.1.0.tar.gz -
Subject digest:
91f80c0f592acae5d6fe110ad3eb32b6b65aefa8df3a715b620d7ee8985270f1 - Sigstore transparency entry: 2794303002
- Sigstore integration time:
-
Permalink:
CogStack/cogstack-nlp@163e9aeb178739f0137a08dd1c067b11aea1d045 -
Branch / Tag:
refs/tags/medcat-llm-components/v0.1.0 - Owner: https://github.com/CogStack
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
medcat-llm-components_ci.yml@163e9aeb178739f0137a08dd1c067b11aea1d045 -
Trigger Event:
push
-
Statement type:
File details
Details for the file medcat_llm_components-0.1.0-py3-none-any.whl.
File metadata
- Download URL: medcat_llm_components-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb644a5cdef0b80791b5ff67e14dbe2e9c3c36d11b32a158a2943d611c2ec735
|
|
| MD5 |
6bf42f414d8c1f57cc35e82a8a2394be
|
|
| BLAKE2b-256 |
4f8eddf270782e15c479f9d3ae47d17bef191ed3228b84343ff3e65118112720
|
Provenance
The following attestation bundles were made for medcat_llm_components-0.1.0-py3-none-any.whl:
Publisher:
medcat-llm-components_ci.yml on CogStack/cogstack-nlp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
medcat_llm_components-0.1.0-py3-none-any.whl -
Subject digest:
bb644a5cdef0b80791b5ff67e14dbe2e9c3c36d11b32a158a2943d611c2ec735 - Sigstore transparency entry: 2794303022
- Sigstore integration time:
-
Permalink:
CogStack/cogstack-nlp@163e9aeb178739f0137a08dd1c067b11aea1d045 -
Branch / Tag:
refs/tags/medcat-llm-components/v0.1.0 - Owner: https://github.com/CogStack
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
medcat-llm-components_ci.yml@163e9aeb178739f0137a08dd1c067b11aea1d045 -
Trigger Event:
push
-
Statement type: