Skip to main content

Vectorization with LLMs for Python

Project description

dim-python

A Python binding for dim-rs, providing efficient text and image vectorization using OpenAI models.

Installation

pip install dim-for-python

Features

  • Async text vectorization using OpenAI's language models
  • Async image vectorization using OpenAI's vision models
  • Concurrent processing for improved performance
  • Custom API endpoint support
  • Type hints included

Requirements

  • Python 3.7+
  • OpenAI API key
  • Rust toolkit (for building from source)

Usage

Text Vectorization

import asyncio
from dim_for_python import vectorize_string

async def main():
    # Initialize text and prompts
    text = "The quick brown fox jumps over the lazy dog"
    prompts = [
        "What is the mood of this text?",
        "What actions are described in this text?"
    ]

    # Get vector representation
    vector = await vectorize_string(
        string=text,
        prompts=prompts,
        model="gpt-4",
        api_key="your-api-key"
    )

    print(f"Vector dimension: {len(vector)}")
    print(f"Vector: {vector}")

# Run the async function
asyncio.run(main())

Image Vectorization

import asyncio
from dim_for_python import vectorize_image
from PIL import Image
import io

async def main():
    # Load and prepare image
    image = Image.open("path/to/image.jpg")
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='PNG')
    img_bytes = img_byte_arr.getvalue()

    # Define prompts for image analysis
    prompts = [
        "What is the main subject of this image?",
        "Describe the colors in this image"
    ]

    # Get vector representation
    vector = await vectorize_image(
        image_bytes=img_bytes,
        prompts=prompts,
        model="gpt-4-vision-preview",
        api_key="your-api-key"
    )

    print(f"Vector dimension: {len(vector)}")
    print(f"Vector: {vector}")

# Run the async function
asyncio.run(main())

Using Custom API Endpoint

vector = await vectorize_string(
    string="Your text here",
    prompts=["Your prompt"],
    model="gpt-4",
    api_key="your-api-key",
    base_url="https://your-custom-endpoint.com"
)

API Reference

vectorize_string

async def vectorize_string(
    string: str,
    prompts: List[str],
    model: str,
    api_key: str,
    base_url: Optional[str] = None
) -> List[float]

Parameters:

  • string: The input text to vectorize
  • prompts: List of prompts to guide the vectorization
  • model: OpenAI model identifier
  • api_key: Your OpenAI API key
  • base_url: Optional custom API endpoint

Returns:

  • List of floating-point numbers representing the text vector

vectorize_image

async def vectorize_image(
    image_bytes: bytes,
    prompts: List[str],
    model: str,
    api_key: str,
    base_url: Optional[str] = None
) -> List[float]

Parameters:

  • image_bytes: Raw bytes of the image
  • prompts: List of prompts to guide the vectorization
  • model: OpenAI model identifier
  • api_key: Your OpenAI API key
  • base_url: Optional custom API endpoint

Returns:

  • List of floating-point numbers representing the image vector

Development

Building from source

# Clone the repository
git clone https://github.com/yourusername/dim-python
cd dim-python

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install development dependencies
pip install maturin pytest pytest-asyncio Pillow

# Build and install in development mode
maturin develop

Running tests

pytest tests/ -v

License

MIT

Contributing

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

Acknowledgments

This project is a Python binding for dim-rs, which provides the core functionality for text and image vectorization.

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

dim_for_python-0.2.4.tar.gz (314.5 kB view details)

Uploaded Source

Built Distribution

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

dim_for_python-0.2.4-cp311-cp311-macosx_11_0_arm64.whl (3.9 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file dim_for_python-0.2.4.tar.gz.

File metadata

  • Download URL: dim_for_python-0.2.4.tar.gz
  • Upload date:
  • Size: 314.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.8

File hashes

Hashes for dim_for_python-0.2.4.tar.gz
Algorithm Hash digest
SHA256 a0e35a04f010f94b17dd2a126b80736eb17a32b1f2e22c642db63d2a3f8b1d75
MD5 b24166c1e59ec119bc5193596f435e56
BLAKE2b-256 53cd8728c7026ca25b68a667a2adc48708475b3705d64c427c6ef3959b1bcd95

See more details on using hashes here.

File details

Details for the file dim_for_python-0.2.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dim_for_python-0.2.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 79c65fc1a4ce8b676e3a94280f56fa0f31de0c3bc6f8852c7d8246f65908b84a
MD5 8cc290a33658f2e68b14bdd32fdd785e
BLAKE2b-256 75dc8cf1c449e3011b5f49b7802e6e0757c697b7567a1d062a9ea3ecd99338bf

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