Skip to main content

drugdetector

This project provides a simple wrapper for performant zero-shot drug detection using LLMs.

Install

pip install drugdetector

Optionally, but recommended is to install CUDA support for llama-cpp-python (source).

pip install llama-cpp-python \
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/<cuda-version>

Where <cuda-version> is one of the following:

  • cu121: CUDA 12.1
  • cu122: CUDA 12.2
  • cu123: CUDA 12.3
  • cu124: CUDA 12.4
  • cu125: CUDA 12.5

You can find what version of CUDA you support via nvidia-smi and looking for the CUDA Version in the top right corner.

Demo

For this demo, we're using our fine-tuned 8-bit quantized Llama-3 model (fabriceyhc/Llama-3-8B-DrugDetector), but you can use many LLMs available on huggingface. We recommend one of the following fine-tuned models for improved performance:

  • fabriceyhc/Llama-3-8B-DrugDetector
  • fabriceyhc/Llama-3-70B-DrugDetector
from drugdetector import DrugDetector

detector = DrugDetector(model_id="fabriceyhc/Llama-3-8B-DrugDetector")

Detection on default substances

When no specific drugs are requested, default to the ones we've studied.

results = detector.detect("Patient denies using heroin but reports cocaine use.")
print(results)
{'Heroin': False,
 'Cocaine': True,
 'Methamphetamine': False,
 'Benzodiazepine': False,
 'Prescription Opioids': False,
 'Cannabis': False,
 'Injection Drugs': False,
 'General Drugs': True,
 'medical_text': 'Patient denies using heroin but reports cocaine use.',
 'time_taken': 0.5203254222869873}

Detection with custom drug dict

Tests zero-shot accuracy on any substances provided in as a python dictionary in the form of:

new_substances = {
  "substance1":"description1",
  "substance2":"description2",
  ...
  "substanceN":"descriptionN",
}
new_substances = {
    "Alcohol": "Alcohol is a legal substance but can be abused and lead to addiction and various health issues.",
    "Fentanyl": "Fentanyl is a potent synthetic opioid that is highly addictive and can lead to overdose, especially when used illicitly."
}

results = detector.detect("Patient hx of ethl use.", drugs=new_substances)
print(results)
{'Alcohol': True,
 'Fentanyl': False,
 'medical_text': 'Patient hx of ethl use.',
 'time_taken': 0.5203254222869873}

Detection with custom drug dict and explanations provided

Demonstrates the model's ability to explain its decisions.

new_drugs = {
    "Alcohol": "Alcohol is a legal substance but can be abused and lead to addiction and various health issues.",
    "Fentanyl": "Fentanyl is a potent synthetic opioid that is highly addictive and can lead to overdose, especially when used illicitly."
}

results = detector.detect("Patient hx of ethl use.", drugs=new_substances, explain=True)
print(results)
{'Alcohol': True,
 'Fentanyl': False,
 'medical_text': 'Patient hx of ethl use.',
 'time_taken': 1.1692967414855957,
 'Alcohol_explanation': 'The medical note mentions "ethl use", which is likely referring to ethanol, the scientific name for alcohol.',
 'Fentanyl_explanation': 'There is no mention of Fentanyl use in the medical note.'}

Metadata

Release files for drugdetector 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drugdetector 0.3.3
File Size Uploaded Provenance
drugdetector-0.3.3.tar.gz 5.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for drugdetector 0.3.3
File Interpreter ABI Platform
drugdetector-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / drugdetector-0.3.3.tar.gz

Download URL drugdetector-0.3.3.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
08f60c21de64d5a4e5c5df199d40f64eb5e5348e763bc06d131f39262083f09d
BLAKE2b-256 checksum
How to use checksums
8c1641a5a192c9936bfec4918953a2bbfdbbdcc1f205c78aaa31832ebe2e901d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Release files / drugdetector-0.3.3-py3-none-any.whl

Download URL drugdetector-0.3.3-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c2f3ae85999aced18a913a2bf219c556f22991f78609df72f2fa65c86428d03
BLAKE2b-256 checksum
How to use checksums
1ca6c6fd9f90a29c0a1877c8e06d23f7b7570b99637e22ff5acc699d70824ff5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.14

Security for drugdetector 0.3.3

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page