Skip to main content

ChatParams

Struct ChatParams 

Source
pub struct ChatParams {
    pub prompt: String,
    pub grammar: String,
    pub grammar_lazy: bool,
    pub grammar_triggers_json: String,
    pub grammar_triggers: Vec<GrammarTrigger>,
    pub preserved_tokens_json: String,
    pub additional_stops_json: String,
    pub supports_thinking: bool,
    pub thinking_start_tag: String,
    pub thinking_end_tags_json: String,
    pub format: i32,
    /* private fields */
}
Expand description

The rendered prompt and everything needed to constrain and parse generation.

Fields§

§prompt: String

The prompt to feed the model.

§grammar: String

GBNF grammar constraining output. Empty when unconstrained.

§grammar_lazy: bool

When true, grammar must not be applied until a trigger in Self::grammar_triggers fires. Applying it from token zero is what stops thinking models emitting their reasoning prefix.

§grammar_triggers_json: String

Triggers that activate a lazy grammar. Raw JSON, and parsed into Self::grammar_triggers.

§grammar_triggers: Vec<GrammarTrigger>§preserved_tokens_json: String

JSON array of strings to keep verbatim while sampling.

§additional_stops_json: String

JSON array of extra stop strings this format needs.

§supports_thinking: bool

Whether this template supports reasoning.

§thinking_start_tag: String

Opening reasoning tag, e.g. "<think>". Empty when unsupported.

§thinking_end_tags_json: String

JSON array of closing reasoning tags.

§format: i32

common_chat_format discriminant, needed to parse output back.

Implementations§

Source§

impl ChatParams

Source

pub fn format_name(&self) -> Result<String, ChatError>

Human-readable name of this chat format, e.g. "Hermes 2 Pro".

§Errors

Returns ChatError::Failed if llama.cpp cannot name the format.

Source

pub fn sampler_triggers(&self) -> (Vec<String>, Vec<LlamaToken>)

Convert Self::grammar_triggers into the (patterns, tokens) pair LlamaSampler::grammar_lazy_patterns expects.

The four trigger kinds do not map onto the sampler one-for-one, and getting the translation wrong silently produces a grammar that never activates:

  • word is a literal, so it is regex-escaped before becoming a pattern. A raw <tool_call> would otherwise be a character class.
  • pattern passes through unchanged.
  • pattern_full is anchored with ^/$ unless it already is.
  • token becomes a trigger token rather than a pattern.

This mirrors common/sampling.cpp, so callers do not have to.

Source

pub fn generation_prompt(&self) -> &str

The prefix the template already placed at the end of the prompt, e.g. "<|im_start|>assistant\n".

This is not part of the model’s output, but the grammar and the parser are both written as if it were — see Self::grammar_sampler.

Source

pub fn grammar_sampler(&self, model: &LlamaModel) -> Option<LlamaSampler>

Build the grammar sampler this render needs, or None when unconstrained.

Prefer this over constructing the sampler yourself: it handles three things that are each silently wrong if missed.

  • Lazy vs eager. A lazy grammar must be built with its triggers, or it never activates. One built eagerly from a lazy grammar constrains from token zero and blocks a thinking model’s reasoning prefix.
  • Trigger translation. Literal, regex and token triggers map onto the sampler differently — see Self::sampler_triggers.
  • Generation-prompt prefill. llama.cpp writes tool-call grammars to match generation_prompt + output, because that is what the parser later sees. The sampler only sees output, so without advancing the grammar past that prefix it forces the model to re-emit <|im_start|>assistant as generated text. Prefill applies only to non-lazy grammars — a lazy one has not started matching yet.
§Panics

Panics if llama.cpp cannot parse the grammar it just produced, or if that grammar contains an interior NUL.

Source

pub fn parse(&self, text: &str, is_partial: bool) -> Result<String, ChatError>

Parse model output back into an OpenAI-shaped message JSON object with role, content, reasoning_content and tool_calls.

This uses the parser the template produced, so it understands that model family’s tool-call syntax rather than scraping for a fixed marker.

Set is_partial while streaming: the parser then tolerates a truncated tail instead of rejecting the buffer.

§Errors

Returns ChatError::Failed if the output cannot be parsed.

Source

pub fn parse_with( &self, text: &str, is_partial: bool, parse_tool_calls: bool, reasoning_in_content: bool, ) -> Result<String, ChatError>

Self::parse with control over tool-call parsing and whether reasoning is left inline in content.

§Errors

Returns ChatError::Failed if the output cannot be parsed.

Trait Implementations§

Source§

impl Clone for ChatParams

Source§

fn clone(&self) -> ChatParams

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChatParams

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more