finettt/context-compressor

context compressor - tool what reduce context length for AI

โ˜… 1Forks 0PythonGitHub โ†—Compare

README

Header SVG

Context compressor - tool for improving vibe-coding experience, preventing LLMs from forgetting useful things from your chat session.

๐Ÿš€ Overview

Context Compressor is a FastAPI-based web application that compresses chat conversations to help maintain context in LLM interactions. It uses OpenAI's API to summarize and condense chat histories by 80% or more, preserving only the most important information about user preferences and assistant decisions.

โœจ Features

  • Conversation Compression: Reduce chat history by 80%+ while preserving key information
  • FastAPI Backend: RESTful API with health check endpoint
  • Jinja2 Templating: Flexible template system for conversation formatting
  • OpenAI Integration: Uses configurable LLM models for compression
  • Easy Setup: Simple configuration with environment variables
  • Docker Support: Containerized deployment with Dockerfile

๐Ÿ› ๏ธ Installation

Prerequisites

  • Python 3.12 or higher
  • OpenAI API key
  • Access to an OpenAI-compatible API endpoint

Setup

  1. Clone the repository:
git clone https://github.com/finettt/context-compressor
cd context-compressor
  1. Install dependencies:
pip install -e .
  1. Configure environment variables:
touch .env

Edit .env file with your configuration:

BASE_URL=your-openai-compatible-api-url
API_KEY=your-api-key
MODEL=your-model-name  # e.g., gpt-3.5-turbo, gpt-4

๐Ÿš€ Usage

Running the Application

Local Development

uv run python main.py

Docker Deployment

docker build -t context-compressor .
docker run -p 8000:8000 --env-file .env context-compressor

The application will start on http://localhost:8000

API Endpoints

Health Check

curl http://localhost:8000/health

Response:

{"status": "healthy"}

Compress Context

curl -X POST "http://localhost:8000/chat/completion" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]}'

Response:

{
  "content": "Compressed conversation summary...",
  "message": "Now, you can use this message instead of previous history"
}

Integration with Chat Applications

You can integrate the context compressor into your chat application by making HTTP requests to the /chat/completion endpoint. Send your conversation messages and receive a compressed summary that can be used to maintain context in LLM interactions.

๐Ÿ“ Project Structure

context-compressor/
โ”œโ”€โ”€ main.py                 # Application entry point
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app.py             # FastAPI application setup
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ””โ”€โ”€ api.py         # API endpoints
โ”‚   โ””โ”€โ”€ client/
โ”‚       โ””โ”€โ”€ main.py        # Core compression logic
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ task_template.jinja2        # Compression task template
โ”‚   โ””โ”€โ”€ chat_template.jinja2    # Conversation formatting template
โ”œโ”€โ”€ pyproject.toml         # Project configuration
โ”œโ”€โ”€ app.dockerfile         # Docker configuration
โ”œโ”€โ”€ .dockerignore          # Docker ignore file
โ””โ”€โ”€ README.md              # This file

โš™๏ธ Configuration

Environment Variables

Variable Description Required
BASE_URL OpenAI-compatible API endpoint Yes
API_KEY API key for authentication Yes
MODEL Model name to use for compression Yes

Customization

You can customize the compression behavior by modifying the templates in the assets/ directory:

  • task_template.jinja2: Controls how the compression task is presented to the LLM
  • chat_template.jinja2: Controls how conversation messages are formatted

๐Ÿ”ง Development

Code Quality

This project uses several tools to maintain code quality:

  • Ruff: Fast Python linter and code formatter
  • Ty: Modern task runner
  • Pytest: Testing framework
  • Bandit: Security linter
  • Safety: Security vulnerability checker

Running Tests

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run linting
ruff check .

# Format code
ruff format .

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“„ License

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

๐Ÿ™ Acknowledgments

Contributors

finettt

Issues