Skip to main content

infr_chat/
lib.rs

1//! Shared chat text logic — turning conversations into model prompts and parsing model output back
2//! out. **No GPU, no model, no inference.** Used by `infr-llama` (to feed the forward pass) and by
3//! `infr-engine`/`infr-server` (the OpenAI-shaped API) so prompt rendering lives in ONE place instead
4//! of being re-implemented next to each backend.
5//!
6//! - [`render_chat_jinja`] / [`render_chat_user`] — render a GGUF's embedded `tokenizer.chat_template`
7//!   (jinja, via minijinja) into a prompt string. Return `None` when the GGUF has no template (or it
8//!   fails to render); callers fail loud rather than fabricate a default — infr only supports
9//!   models that ship a chat template.
10//! - [`split_channels`] / [`parse_tool_calls`] — parse model output (reasoning vs answer,
11//!   `<|tool_call>` blocks).
12
13mod stream;
14mod template;
15mod tojson;
16mod tools;
17
18pub use stream::{prompt_prefills_think, ChatStream, Delta};
19pub use template::{
20    render_chat_jinja, render_chat_oai, render_chat_user, render_template, TemplateError,
21};
22pub use tools::{
23    parse_any_tool_calls, parse_hermes_tool_calls, parse_tool_calls, split_channels,
24    split_reasoning, split_think, ToolCall,
25};
26
27/// One chat message (OpenAI-shaped; tool fields preserved for the agentic round-trip).
28#[derive(Clone, Debug, Default)]
29pub struct ChatMessage {
30    pub role: String,
31    pub content: String,
32    /// The assistant's OUTGOING tool calls (OpenAI `message.tool_calls`), replayed into the prompt on
33    /// the next turn so the model sees its own prior calls. Empty/None for non-assistant messages.
34    pub tool_calls: Option<Vec<ToolCall>>,
35    /// For a `tool`-role result message: which call it answers (OpenAI `tool_call_id`).
36    pub tool_call_id: Option<String>,
37    pub name: Option<String>,
38}