https://github.com/christian-byrne/optimize-input-tokens-pipeline

https://github.com/christian-byrne/optimize-input-tokens-pipeline

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.0%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: christian-byrne
  • Language: Python
  • Default Branch: main
  • Size: 333 KB
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created 11 months ago · Last pushed 11 months ago
Metadata Files
Readme

README.md

LLM Cost Optimization Toolkit

A comprehensive collection of tools and strategies for reducing LLM API costs through intelligent token optimization.

🎯 Project Goal

Reduce LLM API costs by 50-80% through intelligent preprocessing, compression, and optimization techniques while maintaining output quality.

📁 Repository Structure

llm-cost-optimization/ ├── token-optimizer/ # Main pipeline implementation │ ├── pipeline.py # Core orchestrator with verbose mode │ ├── scripts/ # Individual optimization stages │ │ ├── 01_spell_check.py │ │ ├── 02_abbreviations.py │ │ ├── 03_token_aware.py │ │ └── 04_ml_paraphrase.py │ ├── config/ # Configuration files │ ├── utils/ # Utilities (diff viewer, etc.) │ └── examples/ # Usage examples ├── docs/ # Research and documentation │ ├── ideas-brainstorm.md │ ├── text-normalization-approaches.md │ └── implementation-plan.md ├── requirements.txt # Python dependencies ├── setup_project.sh # One-click setup script └── todo.md # Project task tracking

🚀 Quick Start

1. Initial Setup

```bash

Clone and setup

cd llm-cost-optimization ./setup_project.sh

Activate virtual environment

source venv/bin/activate ```

2. Basic Usage

```bash

Navigate to token optimizer

cd token-optimizer

Process text

echo "Please could you help me understand this?" | python pipeline.py

Output: "help understand this?"

Process file with verbose mode

python pipeline.py input.txt --verbose

Analyze optimization potential

python pipeline.py document.txt --analyze ```

3. Advanced Features

```bash

Use specific stages only

python pipeline.py input.txt --stages abbreviations token_aware

Different tokenizer models

sed -i 's/model: "gpt2"/model: "gpt-4"/' config/pipeline_config.yaml

Run the demo

./demo.sh ```

🛠️ Features

Token Optimizer Pipeline

  • Modular Architecture: 4 independent stages that can be mixed and matched
  • Smart Token Counting: Actual token measurement for your target LLM
  • ML Compression: Uses small models (T5-small) for intelligent paraphrasing
  • Beautiful Verbose Mode: Visual diffs, statistics, and progress tracking
  • Configurable: YAML configuration for easy customization

Optimization Techniques

  1. Spell Correction: Fix typos that waste tokens
  2. Smart Abbreviations: Context-aware replacements
  3. Token-Aware Optimization: Only changes that actually save tokens
  4. ML Paraphrasing: Intelligent compression using small language models

📊 Performance

Typical results on real-world prompts: - Token Reduction: 30-60% - Processing Time: <500ms (without ML), <2s (with ML) - Quality Preservation: 95%+ semantic similarity

🔧 Configuration

Edit token-optimizer/config/pipeline_config.yaml:

```yaml tokenizer: model: "gpt2" # or "gpt-4", "claude", etc.

pipeline: spellcheck: enabled: true abbreviations: enabled: true tokenaware: enabled: true mlparaphrase: enabled: true model: "t5-small" maxlength_ratio: 0.8 ```

📚 Documentation

🤝 Contributing

This project is designed for parallel development. Check todo.md for available tasks.

Development Workflow

  1. Pick a task from todo.md
  2. Create a new branch
  3. Implement and test
  4. Update documentation
  5. Submit PR

🧪 Testing

Quick Test

```bash

Run basic tests

./run_tests.sh ```

Full Test Suite

```bash

Unit tests with coverage

make ci-test

All CI checks (format, lint, type, test)

make ci

Individual checks

make ci-format # Format code make ci-lint # Run linters make ci-typecheck # Type checking ```

Test Structure

tests/ ├── conftest.py # Shared fixtures and helpers ├── unit/ # Fast, isolated unit tests │ ├── test_spell_check.py │ ├── test_abbreviations.py │ ├── test_token_aware.py │ └── test_pipeline.py └── integration/ # End-to-end integration tests └── test_full_pipeline.py

Writing Tests

```python

Use provided fixtures

def testexample(sampletext, abbreviationsdict, testhelpers): result = process(sampletext) assert testhelpers.calculatereduction(sampletext, result) > 30 ```

📈 Roadmap

  • [x] Core pipeline implementation
  • [x] Verbose mode with visual diffs
  • [ ] Web interface
  • [ ] Real-time optimization API
  • [ ] Custom model training
  • [ ] Integration plugins (VSCode, CLI tools)

📄 License

MIT License - See LICENSE file for details

🙏 Acknowledgments

  • Microsoft Research for LLMLingua
  • Hugging Face for transformers library
  • Rich library for beautiful terminal UI

Owner

  • Name: Christian Byrne
  • Login: christian-byrne
  • Kind: user
  • Location: San Francisco
  • Company: Comfy-Org

GitHub Events

Total
  • Push event: 2
Last Year
  • Push event: 2

Dependencies

.github/workflows/ci.yml actions
  • actions/cache v3 composite
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • codecov/codecov-action v3 composite
pyproject.toml pypi
  • black ^23.3.0 develop
  • flake8 ^6.0.0 develop
  • ipykernel ^6.0.0 develop
  • jupyter ^1.0.0 develop
  • mypy ^1.3.0 develop
  • pytest ^7.3.0 develop
  • pytest-cov ^4.0.0 develop
  • click ^8.1.3
  • deepdiff ^6.7.0
  • llmlingua ^0.1.0
  • nltk ^3.8
  • pandas ^2.0.0
  • pyspellchecker ^0.7.0
  • python ^3.8
  • python-dotenv ^1.0.0
  • pyyaml ^6.0
  • rich ^13.7.0
  • sentence-transformers ^2.2.2
  • sentencepiece ^0.1.99
  • spacy ^3.5.0
  • symspellpy ^6.7.7
  • termcolor ^2.4.0
  • tokenizers ^0.13.3
  • torch ^2.0.0
  • tqdm ^4.65.0
  • transformers ^4.30.0
  • txtai ^6.0.0
requirements.txt pypi
  • black >=23.3.0
  • click >=8.1.3
  • deepdiff >=6.7.0
  • flake8 >=6.0.0
  • ipykernel >=6.0.0
  • jupyter >=1.0.0
  • llmlingua >=0.1.0
  • mypy >=1.3.0
  • nltk >=3.8
  • pandas >=2.0.0
  • pyspellchecker >=0.7.0
  • pytest >=7.3.0
  • pytest-cov >=4.0.0
  • python-dotenv >=1.0.0
  • pyyaml >=6.0
  • rich >=13.7.0
  • ruff >=0.1.0
  • sentence-transformers >=2.2.2
  • sentencepiece >=0.1.99
  • spacy >=3.5.0
  • symspellpy >=6.7.7
  • termcolor >=2.4.0
  • tokenizers >=0.13.3
  • torch >=2.0.0
  • tqdm >=4.65.0
  • transformers >=4.30.0
  • txtai >=6.0.0
token-optimizer/requirements.txt pypi
  • black >=23.3.0
  • click >=8.1.3
  • deepdiff >=6.7.0
  • flake8 >=6.0.0
  • llmlingua >=0.1.0
  • mypy >=1.3.0
  • nltk >=3.8
  • pandas >=2.0.0
  • pyspellchecker >=0.7.0
  • pytest >=7.3.0
  • pytest-cov >=4.0.0
  • python-dotenv >=1.0.0
  • pyyaml >=6.0
  • rich >=13.7.0
  • sentence-transformers >=2.2.2
  • sentencepiece >=0.1.99
  • spacy >=3.5.0
  • symspellpy >=6.7.7
  • termcolor >=2.4.0
  • tokenizers >=0.13.3
  • torch >=2.0.0
  • tqdm >=4.65.0
  • transformers >=4.30.0
  • txtai >=6.0.0