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.1.tar.gz (38.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.1-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.1.tar.gz.

File metadata

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

File hashes

Hashes for dim_for_python-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0b7cbc11c149535811e67e04b2a2d089249db8cffd34c594a6cdfedd9828887e
MD5 014a6a11d432e795f2286e9e2592a3d0
BLAKE2b-256 da367abf5385565dbed8179a855fec84ef5710eb24d13f1783dd39c7dbf76ff1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dim_for_python-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 18dd23cf8c9c5ac4e0e3d8f539cbe883d59db1fb4e48b701b6ea029e741aa8f1
MD5 53f6f4d16b169f749b6c357c4d8d221c
BLAKE2b-256 de112ef3e71560edb51174c9290ff53b0e46511ec6b6b19d22b8d94861fc1801

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