Metadata-Version: 2.4
Name: chatunify
Version: 0.1.0
Summary: Convert any sentence or chat messages array to the official chat template for OpenAI, Mistral, and Qwen models.
Project-URL: Homepage, https://github.com/you/chatunify
License: MIT License
        
        Copyright (c) 2026
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# chatunify

Convert any sentence or chat messages array into the official chat template for OpenAI, Mistral, and Qwen models.

## Installation

```bash
pip install chatunify
```

## Quick Start

```python
from chatunify import apply_template

# Plain string — auto-wrapped as a user message
result = apply_template("Hello!", model="gpt-4o")
result = apply_template("Hello!", model="mistral-large-latest")
result = apply_template("Hello!", model="qwen2.5-72b-instruct")

# Full messages array
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the capital of France?"},
]
result = apply_template(messages, model="mistral-large-latest")
```

## Supported Models

| Provider | Model name patterns | Template |
|---|---|---|
| **OpenAI** | `gpt-*`, `o1`, `o3`, `o4`, `chatgpt-*` | JSON messages array (API format) |
| **Mistral V1** | `mistral-7b-v0.1` | `<s> [INST] user [/INST] assistant</s>` |
| **Mistral V2** | `mistral-7b-v0.2`, `mixtral-*`, `mistral-small/medium` | `<s>[INST] user[/INST] assistant</s>` |
| **Mistral V3** | `mistral-large`, `mistral-nemo`, `codestral`, `*-2407+` | `<s>[INST]user[/INST]assistant</s>` |
| **Qwen** | `qwen*`, `qwq*` | ChatML `<\|im_start\|>role\ncontent<\|im_end\|>` |

## Template Outputs

### OpenAI
Returns a pretty-printed JSON string of the messages array — the canonical OpenAI API input format.

```json
[
  {"role": "system", "content": "You are helpful."},
  {"role": "user", "content": "Hello!"}
]
```

### Mistral (V3 / Tekken)
```
<s>[INST]You are helpful.

Hello![/INST]
```

### Qwen (ChatML)
```
<|im_start|>system
You are helpful.<|im_end|>
<|im_start|>user
Hello!<|im_end|>
<|im_start|>assistant
```

## API

### `apply_template(input, model)`

| Parameter | Type | Description |
|---|---|---|
| `input` | `str` or `list[dict]` | Plain string or list of `{"role": ..., "content": ...}` dicts |
| `model` | `str` | Model name (e.g. `"gpt-4o"`, `"mistral-large-latest"`, `"qwen2.5-72b-instruct"`) |

**Returns:** `str` — the formatted prompt string.

### `detect_provider(model)`

Returns `(provider, variant)` tuple for a given model name. Useful for debugging routing.

```python
from chatunify import detect_provider

detect_provider("gpt-4o")               # ("openai", None)
detect_provider("mistral-large-latest") # ("mistral", "v3")
detect_provider("qwen2.5-72b-instruct") # ("qwen", None)
```

## Running Tests

```bash
python tests/test_templates.py
```
