An LLM-powered Python package for dynamic concept mining and mixed-membership clustering over text.
Project description
deduct + induct via LLMs
duktr is an LLM-powered Python package for dynamic concept mining and mixed-membership (multi-label) assignment/clustering over text. It maintains an evolving catalog of concepts and, for each input text, returns the set of concepts that describe it, reusing existing concepts where possible and introducing new concepts when needed.
This is useful when the concept set cannot be pre-defined and will evolve over time (e.g., news topics, issues in tickets, patients’ symptoms, canonical product identities). You define what a “concept” means for your use case, and duktr provides the flexibility to extract the concepts you have in mind to cluster textual information based on the specified concept.
Features
- Mixed-membership labeling: each text can map to zero, one, or many concepts.
- Dynamic concept discovery: concepts are discovered from data and the catalog grows over time.
- Catalog-aware prompting: prompts reuse of existing concepts to avoid drift/duplication and to support clustering.
- Scales to large catalogs: progressive partitioning keeps LLM inputs bounded as the catalog grows.
- Pluggable LLM backends: OpenAI, Gemini, or a custom Python function (including your LLM of choice, e.g., Hugging Face).
- Advantage over traditional clustering: Leverages LLM reasoning to capture semantic similarity beyond surface-level text features and density-based methods (e.g., DBSCAN), enabling finer-grained and more subtle concepts and better performance in low-density settings.
Installation
pip install duktr
Quick Start
from duktr import ConceptMiner, GeminiProvider
# Initialize a miner that extracts "symptom" as the target concept from text records.
miner = ConceptMiner(
llm=GeminiProvider(api_key="YOUR_API_KEY"),
task="Extract the symptom(s) of the patient from their record.",
concept="symptom",
rules="""
- Use short noun phrases (2–6 words)
- Symptom(s) must be independent; no duplicates
- Output in English
""",
)
# Example inputs (note: the third record paraphrases the first).
texts = [
"Patient reports frequent headaches and occasional dizziness.",
"The individual is experiencing shortness of breath during exertion.",
"The patient describes recurrent head pain along with vertigo." # paraphrased version of first text
]
# Mine concepts per text.
per_text_concepts = miner.mine(texts)
print(per_text_concepts) # concepts found in each record
print(miner.catalog) # global catalog across all records
Expected output:
[{"Headache", "Dizziness"}, {"Shortness of breath"}, {"Headache", "Dizziness"}]
{"Headache", "Dizziness", "Shortness of breath"}
Notes:
- There are multiple ways to configure the prompt used by the
ConceptMiner. See the documentation for more details. - Runtime is mainly determined by the computational speed of the underlying LLM.
- Test different prompt templates and rules with a small sample of your data to see what works best for your use case before running on the full dataset.
Documentation
For more detailed information, see the full documentation.
License and contributing
- License: MIT License. See the
LICENSEfile for full terms. - Contributing: Issues and pull requests are welcome.
Project details
Release history Release notifications | RSS feed
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 duktr-0.1.0.tar.gz.
File metadata
- Download URL: duktr-0.1.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab6fc4b3be0cc18d666413c6c41cb7d2477bb2ece9355ef0051fa905924fc662
|
|
| MD5 |
0c75810769b5913da0b3653aa8a115cb
|
|
| BLAKE2b-256 |
34d8cea506e6cd3b941150c66359604dc4f1ddb876a12d2f0afd658981977716
|
File details
Details for the file duktr-0.1.0-py3-none-any.whl.
File metadata
- Download URL: duktr-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15c236534a56e7d16656dbb16d2c10fccbc9813a35031db1055599295c68899d
|
|
| MD5 |
e838a84cccddffd1db18fea650b3ff44
|
|
| BLAKE2b-256 |
295529a2f4bcf899a319559bf6232a751d8f0a4559e58b7b79621474c49b4301
|