Skip to main content

Universal Lossy Optical Context Compression for Vision-Language Models

Project description

Un-LOCC Wrapper: An OpenAI SDK Wrapper Building Upon the Research of UN-LOCC

Un-LOCC (Universal Lossy Optical Context Compression) is a Python library that wraps the OpenAI SDK to enable optical compression of text inputs. By rendering text into images, it leverages Vision-Language Models (VLMs) for more efficient token usage, especially when dealing with large text contexts.

Features

  • Optical Compression: Converts text into images for VLM-compatible input.
  • Seamless Integration: Drop-in replacement for OpenAI client with compression support.
  • Synchronous and Asynchronous: Supports both sync and async OpenAI operations.
  • Flexible Compression: Customize font, size, dimensions, and more.
  • Efficient Rendering: Uses fast libraries like ReportLab and pypdfium2 when available, falls back to PIL.

Installation

pip install un-locc

Dependencies

  • openai
  • Pillow (PIL)
  • Optional: reportlab, pypdfium2, aggdraw for enhanced performance

Quickstart

Basic Usage

from un_locc import UnLOCC

# Initialize with your OpenAI API key
client = UnLOCC(api_key="your-api-key")

# Compress a message in chat completions
messages = [
    {"role": "user", "content": "Summarize this text.", "compressed": True}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

Asynchronous Usage

import asyncio
from un_locc import AsyncUnLOCC

async def main():
    client = AsyncUnLOCC(api_key="your-api-key")
    messages = [
        {"role": "user", "content": "Analyze this document.", "compressed": True}
    ]
    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=messages
    )
    print(response)

asyncio.run(main())

Responses API with Compression

from un_locc import UnLOCC

client = UnLOCC(api_key="your-api-key")

response = client.responses.create(
    model="gpt-4o",
    input="Large text to compress",
    compression=True
)

Documentation

Classes

  • UnLOCC: Synchronous wrapper for OpenAI client.
  • AsyncUnLOCC: Asynchronous wrapper for OpenAI client.

Both classes initialize like the OpenAI client: UnLOCC(api_key="...").

Compression Parameters

Default compression settings (uses built-in Atkinson Hyperlegible Regular font):

{
    'font_path': 'AtkinsonHyperlegible-Regular.ttf',  # Built-in font
    'font_size': 15,
    'max_width': 864,
    'max_height': 864,
    'padding': 20
}

Customize by passing a dict to compressed:

messages = [
    {
        "role": "user", 
        "content": large_text,
        "compressed": {
            "font_size": 12,
            "max_width": 1024,
            "max_height": 1024
        }
    }
]

For responses.create, pass compression as a dict or True for defaults.

Methods

Chat Completions

  • client.chat.completions.create(messages, **kwargs): Compresses messages with "compressed" key.
  • client.chat.completions.create(**kwargs): Standard usage.

Responses

  • client.responses.create(input, compression=None, **kwargs): Compresses input if compression is provided.

Content Handling

  • String Content: Directly compressed into images.
  • List Content: Processes parts; text parts are compressed, others remain unchanged.

Rendering Methods

The library selects the fastest available rendering method:

  1. ReportLab + pypdfium2 (fastest, recommended).
  2. ReportLab only.
  3. PIL fallback (ultra-fast bitmap).

Ensure fonts are available; defaults to system fonts if not found.

Tips

Through several trials, I've found that it's much better to embed instructions into plain text and then only compress the large context like this:

messages = [
    {
        "role": "user", 
        "content": "Instructions: Summarize the following text."
    },
    {
        "role": "user", 
        "content": long_text,
        "compressed": True
    },
]

This approach keeps instructions clear and readable while compressing only the bulky content. Alternatively, use it to compress prior chat history for efficient context management.

License

[Specify your license here, e.g., MIT]

Contributing

Contributions welcome! Please submit issues and pull requests.

Related Research

For more details on the library and optimal per model configurations, check out github.com/MaxDevv/UN-LOCC.

Based on UN-LOCC research for optical context compression in VLMs.

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

un_locc-0.1.9.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

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

un_locc-0.1.9-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

Details for the file un_locc-0.1.9.tar.gz.

File metadata

  • Download URL: un_locc-0.1.9.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for un_locc-0.1.9.tar.gz
Algorithm Hash digest
SHA256 1a74e6e9e33f440adcca31606ae387632ecc2328448e899a9617457ab76e6219
MD5 884dc50738d9336566ae624c31b4685c
BLAKE2b-256 dde3cc049dd8dcc93b4ed77f5ae7ce20cd9eeec1319d683f25d21ab64d6cd057

See more details on using hashes here.

File details

Details for the file un_locc-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: un_locc-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for un_locc-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0ec44637c355d8cd5d64673fa27bd44647bd40061248f0a20eac117ac7e4aa7f
MD5 ab0d60c8eed811d860b3d9716daa1835
BLAKE2b-256 084e92df80f8095f8b4e42826e773c3b227ad3b3d9d5905077b8744d73bc67e1

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