Chat API Reference¶
Chat is the high-level interface that wraps messages and Template to render prompts and build tokenized inputs. This is the interface that we recommend users to use.
Minimal Example¶
from chat_bricks import Chat, get_template
from transformers import AutoTokenizer
tools = [{
"type": "function",
"name": "get_weather",
"description": "Retrieves current weather for the given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Units the temperature will be returned in."
}
},
"required": ["location", "units"]
}
}]
chat = Chat(
template="qwen3",
messages=[{"role": "user", "content": "Hello!"}],
)
# Render prompt text
prompt = chat.prompt(tools=tools)
print(prompt)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
# Tokenize for training or inference
inputs = chat.tokenize(
tokenizer,
add_generation_prompt=True, # keep generation token for inference
tools=tools # optional tool definitions
)
chat_bricks.chat.Chat
¶
__init__(template, messages=None, tools=None, skills=None, tokenizer=None, ignore_tool_calls=False)
¶
Parameters:
-
template(str | Template | HFTemplate) –The name of the template to use.
-
messages(List[Dict], default:None) –The messages to use for the chat.
-
tools–The tools to use for the chat.
-
skills–Optional list of skill objects/dicts to advertise via the template's
{skills}placeholder (requiresskills_templateon the template). -
tokenizer(PreTrainedTokenizer, default:None) –The tokenizer to use for the chat.
preprocess_messages(messages)
¶
Preprocess the messages for the chat. Since we don't change message content, we don't copy it here.
prompt(add_generation_prompt=False, tools=None, skills=None, **kwargs)
¶
Get the prompt for the chat.
Parameters:
-
add_generation_prompt–Whether to add the generation prompt.
-
tools–The tools to use for the chat.
-
skills–Optional list of skill objects/dicts (see
Chat.__init__). -
**kwargs–Additional keyword arguments to pass to the template render method.
Returns:
-
str–The string formatted prompt for the messages after applying the chat template.
prompt_with_mask(add_generation_prompt=False, tools=None, skills=None, **kwargs)
¶
Get the prompt for the chat with highlight on the masked parts.
Parameters:
-
add_generation_prompt–Whether to add the generation prompt.
-
tools–The tools to use for the chat.
-
skills–Optional list of skill objects/dicts (see
Chat.__init__). -
**kwargs–Additional keyword arguments to pass to the template render method.
Returns:
-
str–The string formatted prompt for the messages after applying the chat template with highlight on the masked parts.
set_messages(messages)
¶
Set the messages for the chat.
tokenize(tokenizer=None, add_generation_prompt=False, tools=None, skills=None, processor=None, train_on_last_turn_only=False, **kwargs)
¶
Tokenize the messages.
Parameters:
-
tokenizer(PreTrainedTokenizer, default:None) –The tokenizer to use for the chat.
-
add_generation_prompt–Whether to add the generation prompt.
-
tools–The tools to use for the chat.
-
skills–Optional list of skill objects/dicts (see
Chat.__init__). -
processor–The processor to use for the chat.
Returns:
-
inputs(dict) –Inputs for helping training. - input_ids - attention_mask - labels - action_mask - multi_modal_inputs