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.2.tar.gz (312.9 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.2-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.2.tar.gz.

File metadata

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

File hashes

Hashes for dim_for_python-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a238cc6fa24141d6596052ec6d5e25728a14e373f666d461e8a3d76053c85258
MD5 713004b7d8ec7dfdb3f3a66ac103ad0d
BLAKE2b-256 e32581db9c10546c4fe71d97e3cc1c3aa504f8840b8e7d63da7e521f723da34f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dim_for_python-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 30c1e8a2bc48943cd760ccec29e901d11830718cbe2141935d51a396cf5e9ed6
MD5 a66be6f4fcc36795d50ac6d9fc401496
BLAKE2b-256 c52efe2170a75ca8b9d6bf5d95f48bf9885eacbe96f5393aabda36df43cdd3ab

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