Skip to main content

A Python template library for Multimodal LLMs that extends string.Template to support image substitution in prompts

Project description

MLLM Prompt Template

A Python template library for Multimodal LLMs(MLLMs) that extends Python's string.Template to support image substitution in prompts.

# ❌
from string import Template
template = Template("Extract all text from the image. \n image: $img")
template.safe_substitute(
    img=Image.open("photo.jpg")
)
# ✅
from mllm_prompt_template import Template
template = Template("Extract all text from the image. \n image: $img")
template.safe_substitute(
    img=Image.open("photo.jpg")
)

Installation

pip install mllm-prompt-template

Usage

from mllm_prompt_template import Template
from PIL import Image

# Create template with image placeholders
prompt_template = Template("""
这个人是$name, 这是他的自拍照:
$img
$name很可爱对不对
""")

# Substitute text and image variables
prompt_template.safe_substitute(
    name="co-gy",
    img=Image.open("photo.jpg")
)

# Convert to OpenAI-style message format
messages = prompt_template.to_messages()

# Call MLLM (example with OpenAI-compatible API)
import openai

client = openai.OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")

response = client.chat.completions.create(
    model="Qwen3-VL-8B-Instruct",
    messages=messages,
    max_tokens=500
)

print(response.choices[0].message.content)

API

  • Template(prompt_template: str) - Create template
  • safe_substitute(**kwargs) - Substitute variables (supports strings and PIL Images)
  • to_messages() - Convert to OpenAI-style message format

Requirements

  • Python >= 3.8
  • Pillow >= 9.0.0

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

mllm_prompt_template-1.0.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file mllm_prompt_template-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mllm_prompt_template-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 713915ab2fdcef1d8165b8667be45f6c8f7307f6f50361c07e3d829aefcc6f18
MD5 8c7836f384a0b7667edda2b7a9056094
BLAKE2b-256 046ef9743e031782e569eba812be47f5f98a6fdf16e402c1b8b4477d12b7a347

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