Skip to main content

A helper library for LLM/VLM chat templates.

Project description

🧩 Chat Bricks

Static Badge

Jinja Template is Not You Need!

Chat Bricks is a powerful and flexible template system inspired by building block toys, designed to support various LLM and VLM chat templates for training and inference.

Key Features

  • Training and Inference: Chat template formatted prompts, with tokenized inputs and masks.
  • Modular design: Templates are built from configurable components.
  • Multi-modal support: Built-in vision-language templates.
  • Jinja template generation: Automatic HuggingFace-compatible template generation.
  • HuggingFace Integration: Directly supports using an HF repo id as template.
  • Advanced configuration: Fine-grained control over template behavior.

Installation

pip install chat-bricks

Quick Start

Basic Usage

Create a chat object with a built-in template and render the prompt:

from chat_bricks import Chat

# Create a chat object with template and messages
chat = Chat(
    template="qwen3",
    messages=[
        {"role": "user", "content": "Hello, how are you?"},
        {"role": "assistant", "content": "I am fine, thank you."}
    ],
)

# Render the final prompt
prompt = chat.prompt()
print(prompt)

Tokenization for Training/Inference

You can easily tokenize messages for model input:

from transformers import AutoTokenizer
from chat_bricks import Chat

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct")
chat = Chat(template="qwen2.5", messages=[{"role": "user", "content": "Hello!"}])

inputs = chat.tokenize(
    tokenizer,
    add_generation_prompt=True,  # keep generation token for inference
)

print(inputs["input_ids"])

Custom Templates

Define your own template format using the Template class:

from chat_bricks import Chat, Template

custom = Template(
    name="my-template",
    system_template="<|im_start|>system\n{system_message}<|im_end|>\n",
    system_message="You are a concise assistant.",
    user_template="<|im_start|>user\n{content}<|im_end|>\n",
    assistant_template="<|im_start|>assistant\n{content}<|im_end|>\n",
    stop_words=["<|im_end|>"],
)

chat = Chat(template=custom, messages=[{"role": "user", "content": "Hi!"}])
print(chat.prompt())

Using HuggingFace Repo ID as Template

You can directly use any HuggingFace model repository ID as a template. Chat Bricks will automatically load the tokenizer's chat template:

from chat_bricks import Chat

# Use a HuggingFace repo id directly
chat = Chat(
    template="Qwen/Qwen2.5-3B-Instruct",
    messages=[
        {"role": "user", "content": "Hello, how are you?"},
        {"role": "assistant", "content": "I am fine, thank you."}
    ],
)

# Render the prompt using the model's native chat template
prompt = chat.prompt()
print(prompt)
prompt_with_mask = chat.prompt_with_mask()
print(prompt_with_mask)

# Tokenize with proper masking for training
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct")
inputs = chat.tokenize(tokenizer, add_generation_prompt=True)

This feature automatically detects if the repo ID is not a built-in template and creates an HFTemplate that uses the tokenizer's chat template. It supports tools, generation prompts, and proper masking for training. See the HuggingFace Templates Guide for more details.

Documentation

For full documentation, please visit our docs (or run mkdocs serve locally).

Community

WeChat Discord

Scan to join wechat group

Join our discord channel

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

chat_bricks-0.1.2.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

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

chat_bricks-0.1.2-py3-none-any.whl (49.4 kB view details)

Uploaded Python 3

File details

Details for the file chat_bricks-0.1.2.tar.gz.

File metadata

  • Download URL: chat_bricks-0.1.2.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for chat_bricks-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f14e07febe9d96b7b3be2ea6169e3b75e2e66cb414a7033a1cfa21ca6424e9d5
MD5 1fe0f68fc56bfb72594a50b549188086
BLAKE2b-256 16e962f6d64bccca9e7f3e0daba4260a1a671f46687b6143132861abfe948b9a

See more details on using hashes here.

File details

Details for the file chat_bricks-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: chat_bricks-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 49.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for chat_bricks-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a66667fff57325e0ded59a0db2b7ff81a3c84620e62c03cf78fc5b320f7e24d1
MD5 bfb76226b027def2a3df2aec63659098
BLAKE2b-256 5f4985536bc6b9bb27f01ea1b69769b0d7682f1c740cb14fb8880c2275706c72

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