Skip to main content

A lightweight GPT-based language model framework for training custom question-answering models on any domain

Project description

GptMed ๐Ÿค–

A lightweight GPT-based language model framework for training custom question-answering models on any domain. This package provides a transformer-based GPT architecture that you can train on your own Q&A datasets - whether it's casual conversations, technical support, education, or any other domain.

PyPI version Python 3.8+ License: MIT

๐Ÿ“– Complete User Manual | Quick Start

New to GptMed? Check out the step-by-step User Manual for a complete guide on training your own model!

Features

  • ๐Ÿง  Custom GPT Architecture: Lightweight transformer model for any Q&A domain
  • ๐ŸŽฏ Domain-Agnostic: Train on any question-answering dataset (casual chat, tech support, education, etc.)
  • โšก Fast Inference: Optimized for quick question answering
  • ๐Ÿ”ง Flexible Training: Easy to train on your own custom datasets
  • ๐Ÿ“ฆ Lightweight: Small model size suitable for edge deployment
  • ๐Ÿ› ๏ธ Complete Toolkit: Includes tokenizer training, model training, and inference utilities

Table of Contents

Installation

From PyPI (Recommended)

pip install gptmed

From Source

git clone https://github.com/sigdelsanjog/gptmed.git
cd gptmed
pip install -e .

With Optional Dependencies

# For development
pip install gptmed[dev]

# For training
pip install gptmed[training]

# All dependencies
pip install gptmed[dev,training]

Quick Start

Inference (Generate Answers)

from gptmed.inference.generator import TextGenerator
from gptmed.model.architecture import GPTTransformer
from gptmed.model.configs.model_config import get_small_config

# Load model
config = get_small_config()
model = GPTTransformer(config)

# Load your trained checkpoint
# model.load_state_dict(torch.load('path/to/checkpoint.pt'))

# Create generator
generator = TextGenerator(
    model=model,
    tokenizer_path='path/to/tokenizer.model'
)

# Generate answer
question = "What's your favorite programming language?"
answer = generator.generate(
    prompt=question,
    max_length=100,
    temperature=0.7
)

print(f"Q: {question}")
print(f"A: {answer}")

Using Command Line

# Generate answers
gptmed-generate --prompt "How do I train a custom model?" --max-length 100

# Train model
gptmed-train --model-size small --num-epochs 10 --batch-size 16

Training Your Own Model

from gptmed.training.train import main
from gptmed.configs.train_config import get_default_config
from gptmed.model.configs.model_config import get_small_config

# Configure training
train_config = get_default_config()
train_config.batch_size = 16
train_config.num_epochs = 10
train_config.learning_rate = 3e-4

# Start training
main()

Model Architecture

The model uses a custom GPT-based transformer architecture:

  • Embedding: Token + positional embeddings
  • Transformer Blocks: Multi-head self-attention + feed-forward networks
  • Parameters: ~10M (small), ~50M (medium)
  • Context Length: 512 tokens
  • Vocabulary: Custom SentencePiece tokenizer trained on your data

Configuration

Model Sizes

from gptmed.model.configs.model_config import (
    get_tiny_config,   # ~2M parameters - for testing
    get_small_config,  # ~10M parameters - recommended
    get_medium_config  # ~50M parameters - higher quality
)

Training Configuration

from gptmed.configs.train_config import TrainingConfig

config = TrainingConfig(
    batch_size=16,
    learning_rate=3e-4,
    num_epochs=10,
    warmup_steps=100,
    grad_clip=1.0
)

Package Structure

Core Modules

The gptmed package contains the following main modules:

gptmed/
โ”œโ”€โ”€ model/                  # Model architecture and configurations
โ”œโ”€โ”€ inference/              # Text generation and sampling
โ”œโ”€โ”€ training/               # Training loops and datasets
โ”œโ”€โ”€ tokenizer/              # Tokenizer training and data processing
โ”œโ”€โ”€ data/                   # Data parsers and formatters
โ”œโ”€โ”€ configs/                # Training configurations
โ””โ”€โ”€ utils/                  # Utilities (checkpoints, logging)

Model Components

gptmed.model.architecture - GPT Transformer Implementation

  • GPTTransformer - Main model class
  • TransformerBlock - Individual transformer layers
  • MultiHeadAttention - Attention mechanism
  • FeedForward - Feed-forward networks
  • RoPEPositionalEncoding - Rotary position embeddings

gptmed.model.configs - Model Configurations

  • get_tiny_config() - ~2M parameters (testing)
  • get_small_config() - ~10M parameters (recommended)
  • get_medium_config() - ~50M parameters (high quality)
  • ModelConfig - Custom configuration class

Training Components

gptmed.training - Training Pipeline

  • train.py - Main training script (CLI: gptmed-train)
  • Trainer - Training loop with checkpointing
  • TokenizedDataset - PyTorch dataset for tokenized data
  • create_dataloaders() - DataLoader creation utilities

gptmed.configs - Training Configurations

  • TrainingConfig - Training hyperparameters
  • get_default_config() - Default training settings
  • get_quick_test_config() - Fast testing configuration

Inference Components

gptmed.inference - Text Generation

  • TextGenerator - Main generation class
  • generator.py - CLI command (CLI: gptmed-generate)
  • sampling.py - Sampling strategies (top-k, top-p, temperature)
  • decoding_utils.py - Decoding utilities
  • GenerationConfig - Generation parameters

Data Processing

gptmed.tokenizer - Tokenizer Training & Data Processing

  • train_tokenizer.py - Train SentencePiece tokenizer
  • tokenize_data.py - Convert text to token sequences
  • SentencePiece BPE tokenizer support

gptmed.data.parsers - Data Parsing & Formatting

  • MedQuADParser - XML Q&A parser (example)
  • CausalTextFormatter - Format Q&A pairs for training
  • FormatConfig - Formatting configuration

Utilities

gptmed.utils - Helper Functions

  • checkpoints.py - Model checkpoint management
  • logging.py - Training metrics logging

Detailed Project Structure

gptmed/
โ”œโ”€โ”€ model/
โ”‚   โ”œโ”€โ”€ architecture/
โ”‚   โ”‚   โ”œโ”€โ”€ gpt.py              # GPT transformer model
โ”‚   โ”‚   โ”œโ”€โ”€ attention.py        # Multi-head attention
โ”‚   โ”‚   โ”œโ”€โ”€ feedforward.py      # Feed-forward networks
โ”‚   โ”‚   โ””โ”€โ”€ embeddings.py       # Token + positional embeddings
โ”‚   โ””โ”€โ”€ configs/
โ”‚       โ””โ”€โ”€ model_config.py     # Model size configurations
โ”œโ”€โ”€ inference/
โ”‚   โ”œโ”€โ”€ generator.py            # Text generation (CLI command)
โ”‚   โ”œโ”€โ”€ sampling.py             # Sampling strategies
โ”‚   โ”œโ”€โ”€ decoding_utils.py       # Decoding utilities
โ”‚   โ””โ”€โ”€ generation_config.py    # Generation parameters
โ”œโ”€โ”€ training/
โ”‚   โ”œโ”€โ”€ train.py                # Main training script (CLI command)
โ”‚   โ”œโ”€โ”€ trainer.py              # Training loop
โ”‚   โ”œโ”€โ”€ dataset.py              # PyTorch dataset
โ”‚   โ””โ”€โ”€ utils.py                # Training utilities
โ”œโ”€โ”€ tokenizer/
โ”‚   โ”œโ”€โ”€ train_tokenizer.py      # Train SentencePiece tokenizer
โ”‚   โ””โ”€โ”€ tokenize_data.py        # Tokenize text data
โ”œโ”€โ”€ data/
โ”‚   โ””โ”€โ”€ parsers/
โ”‚       โ”œโ”€โ”€ medquad_parser.py   # Example XML parser
โ”‚       โ””โ”€โ”€ text_formatter.py   # Q&A text formatter
โ”œโ”€โ”€ configs/
โ”‚   โ””โ”€โ”€ train_config.py         # Training configurations
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ checkpoints.py          # Model checkpointing
    โ””โ”€โ”€ logging.py              # Training logging

Command-Line Interface

The package provides two main CLI commands:

# Train a model
gptmed-train --model-size small --num-epochs 10 --batch-size 16

# Generate text
gptmed-generate --prompt "Your question?" --max-length 100

Requirements

  • Python >= 3.8
  • PyTorch >= 2.0.0
  • sentencepiece >= 0.1.99
  • numpy >= 1.24.0
  • tqdm >= 4.65.0

Documentation

๐Ÿ“š Complete User Manual - Step-by-step guide for training your own model

Quick Links

Performance

Model Size Parameters Training Time Inference Speed
Tiny ~2M 2 hours ~100 tokens/sec
Small ~10M 8 hours ~80 tokens/sec
Medium ~50M 24 hours ~50 tokens/sec

Tested on GTX 1080 8GB

Examples

Medical Question Answering

# Example 1: Symptoms inquiry
question = "What are the early signs of Alzheimer's disease?"
answer = generator.generate(question, temperature=0.7)

# Example 2: Treatment information
question = "How is Type 2 diabetes treated?"
answer = generator.generate(question, temperature=0.6)

# Example 3: Medical definitions
question = "What is hypertension?"
answer = generator.generate(question, temperature=0.5)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Citation

If you use this model in your research, please cite:

@software{llm_med_2026,
  author = {Sanjog Sigdel},
  title = {GptMed: A custom causal question answering general purpose GPT Transformer Architecture Model},
  year = {2026},
  url = {https://github.com/sigdelsanjog/gptmed}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • MedQuAD dataset creators
  • PyTorch team

Disclaimer

โš ๏ธ Medical Disclaimer: This model is for research and educational purposes only. It should NOT be used for actual medical diagnosis or treatment decisions. Always consult qualified healthcare professionals for medical advice.

Support

Changelog

See CHANGELOG.md for version history.


Made with โค๏ธ for learning purpose

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gptmed-0.3.2.tar.gz (53.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gptmed-0.3.2-py3-none-any.whl (62.7 kB view details)

Uploaded Python 3

File details

Details for the file gptmed-0.3.2.tar.gz.

File metadata

  • Download URL: gptmed-0.3.2.tar.gz
  • Upload date:
  • Size: 53.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for gptmed-0.3.2.tar.gz
Algorithm Hash digest
SHA256 ce0e9f9ec9246a8e8f8e6add8ece96a292e218aab20a110baac32aee72d9ae33
MD5 3d5040ebd8a01b440c04d78f6677a80e
BLAKE2b-256 ee92b160f8ede71e3fc03ac0a22ce4223429a5304319203d6f59d5a1b8e453d9

See more details on using hashes here.

File details

Details for the file gptmed-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: gptmed-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 62.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for gptmed-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 39d3a0cc6404367006f9a511837998e030453160a55933cf10ff162baa7043f6
MD5 12c738faf4ca4a67b1daee26174c5f2f
BLAKE2b-256 d7af9a9ff7363a492d1c4d63d6cf91ce7ea0d0a4486d3844f039e9a4ea4f32cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page