pictoken


Namepictoken JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryCalculate image tokens for (Azure) OpenAI models. Offers multiple utilities to resize images to reduce token usage.
upload_time2024-10-07 21:19:08
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT License Copyright (c) 2024 Mick Vleeshouwer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords azure gpt4o image llm openai vllm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pictoken
Calculate image tokens for Azure OpenAI models. This utility helps resize images to minimize token usage. Unlike [openai/tiktoken](https://github.com/openai/tiktoken), it isn't a tokenizer but calculates image tokens for specific requests. It supports OpenAI models such as gpt-4o-mini and gpt-4o.

Based on [calculation costs for vision capabilities](https://platform.openai.com/docs/guides/vision/calculating-costs) for OpenAI models. In the future models from other providers might be supported as well.

## Installation

Requires Python 3.9 or later.

```
pip install pictoken
```

## Usage

### Calculate image tokens

You can provide images in two main ways: by passing a link to the image or by passing the base64 encoded image directly in the request.

Please note that passing the model is required, as the image token calculation is model-specific. [The dollar price per image is the same for GPT-4o and GPT-4o mini. To maintain this, GPT-4o mini uses more tokens per image.](https://x.com/romainhuet/status/1814054938986885550?t=AMFK4svMvCluYqAXUqRDMQ&s=19). These additional tokens are only used for billing purposes and do not affect the context limit of the model.

#### By image
```python
import pictoken

image_tokens_from_url = pictoken.calculate_image_tokens_by_image(image="http://", model="gpt-4o-mini")
image_tokens_from_base64 = pictoken.calculate_image_tokens_by_image(image="data:image/png;base64,...", model="gpt-4o-mini")
```

#### By image dimensions

```python
import pictoken

image_tokens = pictoken.calculate_image_tokens_by_image_dimensions(
    width=2048, height=2048, model=pictoken.Model.GPT_4O, detail=pictoken.ImageDetail.HIGH
)

print(image_tokens)
# Response(total_tokens=765, base_tokens=85, tile_tokens=680, total_tiles=4, width_tiles=2, height_tiles=2, resized=ResizedImage(width=768, height=768))
```

### Resize images

For low res mode, OpenAI expects a 512px x 512px image. For high res mode, the short side of the image should be less than 768px and the long side should be less than 2,000px. The latency of the model can also be improved by downsizing your images ahead of time to be less than the maximum size they are expected them to be.

```python
#TODO
```

### Add visual metadata to image

The model doesn't process original file names or metadata, and images are resized before analysis, affecting their original dimensions. This utility adds a small text overlay to the image. This can be useful for models to reference to the specific image in a request with multiple images.

```python
#TODO
```

## Development

Contributions are welcome. This packages uses [uv](https://docs.astral.sh/uv/getting-started/installation/) for dependency management and packaging. If you use Visual Studio Code with Docker or GitHub CodeSpaces, you can leverage the included devcontainer. This will install all required dependencies and tools and has the right Python version available. Easy!

To manually set up the development environment, make sure you have uv installed and run `uv sync` to install all dependencies.

## License

MIT

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pictoken",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "Azure, GPT4o, Image, LLM, OpenAI, VLLM",
    "author": null,
    "author_email": "Mick Vleeshouwer <mick@imick.nl>",
    "download_url": "https://files.pythonhosted.org/packages/44/b3/315bf8145c4504e7fc343aaa8dab15c077f4f8b40864c445654d0bf954e4/pictoken-0.1.0.tar.gz",
    "platform": null,
    "description": "# pictoken\nCalculate image tokens for Azure OpenAI models. This utility helps resize images to minimize token usage. Unlike [openai/tiktoken](https://github.com/openai/tiktoken), it isn't a tokenizer but calculates image tokens for specific requests. It supports OpenAI models such as gpt-4o-mini and gpt-4o.\n\nBased on [calculation costs for vision capabilities](https://platform.openai.com/docs/guides/vision/calculating-costs) for OpenAI models. In the future models from other providers might be supported as well.\n\n## Installation\n\nRequires Python 3.9 or later.\n\n```\npip install pictoken\n```\n\n## Usage\n\n### Calculate image tokens\n\nYou can provide images in two main ways: by passing a link to the image or by passing the base64 encoded image directly in the request.\n\nPlease note that passing the model is required, as the image token calculation is model-specific. [The dollar price per image is the same for GPT-4o and GPT-4o mini. To maintain this, GPT-4o mini uses more tokens per image.](https://x.com/romainhuet/status/1814054938986885550?t=AMFK4svMvCluYqAXUqRDMQ&s=19). These additional tokens are only used for billing purposes and do not affect the context limit of the model.\n\n#### By image\n```python\nimport pictoken\n\nimage_tokens_from_url = pictoken.calculate_image_tokens_by_image(image=\"http://\", model=\"gpt-4o-mini\")\nimage_tokens_from_base64 = pictoken.calculate_image_tokens_by_image(image=\"data:image/png;base64,...\", model=\"gpt-4o-mini\")\n```\n\n#### By image dimensions\n\n```python\nimport pictoken\n\nimage_tokens = pictoken.calculate_image_tokens_by_image_dimensions(\n    width=2048, height=2048, model=pictoken.Model.GPT_4O, detail=pictoken.ImageDetail.HIGH\n)\n\nprint(image_tokens)\n# Response(total_tokens=765, base_tokens=85, tile_tokens=680, total_tiles=4, width_tiles=2, height_tiles=2, resized=ResizedImage(width=768, height=768))\n```\n\n### Resize images\n\nFor low res mode, OpenAI expects a 512px x 512px image. For high res mode, the short side of the image should be less than 768px and the long side should be less than 2,000px. The latency of the model can also be improved by downsizing your images ahead of time to be less than the maximum size they are expected them to be.\n\n```python\n#TODO\n```\n\n### Add visual metadata to image\n\nThe model doesn't process original file names or metadata, and images are resized before analysis, affecting their original dimensions. This utility adds a small text overlay to the image. This can be useful for models to reference to the specific image in a request with multiple images.\n\n```python\n#TODO\n```\n\n## Development\n\nContributions are welcome. This packages uses [uv](https://docs.astral.sh/uv/getting-started/installation/) for dependency management and packaging. If you use Visual Studio Code with Docker or GitHub CodeSpaces, you can leverage the included devcontainer. This will install all required dependencies and tools and has the right Python version available. Easy!\n\nTo manually set up the development environment, make sure you have uv installed and run `uv sync` to install all dependencies.\n\n## License\n\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Mick Vleeshouwer  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Calculate image tokens for (Azure) OpenAI models. Offers multiple utilities to resize images to reduce token usage.",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/iMicknl/pictoken",
        "Repository": "https://github.com/iMicknl/pictoken"
    },
    "split_keywords": [
        "azure",
        " gpt4o",
        " image",
        " llm",
        " openai",
        " vllm"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2f7ca052d7db35f8d128bd43b662321276fddd72332d578b7efd389e2e3e891f",
                "md5": "675dd1663e272ab72fdca2d47e4ca524",
                "sha256": "91c32a5630f13694383e45d3b83324bc0f726cd07c4de665b0389c56be2bb663"
            },
            "downloads": -1,
            "filename": "pictoken-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "675dd1663e272ab72fdca2d47e4ca524",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7329,
            "upload_time": "2024-10-07T21:19:07",
            "upload_time_iso_8601": "2024-10-07T21:19:07.455224Z",
            "url": "https://files.pythonhosted.org/packages/2f/7c/a052d7db35f8d128bd43b662321276fddd72332d578b7efd389e2e3e891f/pictoken-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "44b3315bf8145c4504e7fc343aaa8dab15c077f4f8b40864c445654d0bf954e4",
                "md5": "df40afb56e4202123ff03dcc00627207",
                "sha256": "4f38a2eb236aeebb731a7304a6d0b05c2058b2bd84595a9a2accb2c12f923c4d"
            },
            "downloads": -1,
            "filename": "pictoken-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "df40afb56e4202123ff03dcc00627207",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 6672,
            "upload_time": "2024-10-07T21:19:08",
            "upload_time_iso_8601": "2024-10-07T21:19:08.478945Z",
            "url": "https://files.pythonhosted.org/packages/44/b3/315bf8145c4504e7fc343aaa8dab15c077f4f8b40864c445654d0bf954e4/pictoken-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-07 21:19:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "iMicknl",
    "github_project": "pictoken",
    "github_not_found": true,
    "lcname": "pictoken"
}
        
Elapsed time: 0.39444s