pub struct ChatTemplate(/* private fields */);Expand description
A compiled chat template. Cheap to clone (Arc inside) because every
*Loaded struct in ferrox-server carries one and hands it to each
request.
Implementations§
Source§impl ChatTemplate
impl ChatTemplate
Sourcepub fn from_jinja(source: &str) -> Result<Self, TemplateError>
pub fn from_jinja(source: &str) -> Result<Self, TemplateError>
Compiles source as Jinja2. Errors are returned, never swallowed.
pub fn builtin(b: BuiltinTemplate) -> Self
Sourcepub fn from_gguf_metadata(
chat_template: Option<&str>,
arch: Option<&str>,
byte_tokenizer: bool,
) -> Self
pub fn from_gguf_metadata( chat_template: Option<&str>, arch: Option<&str>, byte_tokenizer: bool, ) -> Self
The load-time entry point: what a GGUF’s metadata says.
- a non-empty
tokenizer.chat_templateis compiled, and a compile failure is recorded (so the load still succeeds and/v1/completionsstill works) but makes every chat render fail with the compiler’s message; - no template + a real tokenizer ⇒ ChatML, matching llama.cpp
--jinja’sCHATML_TEMPLATE_SRCdefault; - no template + a byte/synthetic tokenizer ⇒
Plain, since there is no real vocabulary for markers to live in.
Sourcepub fn source(&self) -> Option<&str>
pub fn source(&self) -> Option<&str>
The compiled Jinja source, for callers that need to inspect it.
Sourcepub fn handles_tools(&self) -> bool
pub fn handles_tools(&self) -> bool
Whether the template itself renders tools.
Templates that never mention tools cannot express a tool call,
so a caller offering tools to such a checkpoint has to fall back
to describing them in a system message (ferrox-server’s
tool_preamble). This is a textual check on the template source,
which is what llama.cpp’s common/chat.cpp does too
(caps.supports_tools is probed by rendering, but the cheap
source check is what gates it here).
Sourcepub fn render(
&self,
messages: &[Value],
opts: &RenderOptions,
) -> Result<String, TemplateError>
pub fn render( &self, messages: &[Value], opts: &RenderOptions, ) -> Result<String, TemplateError>
Renders messages (OpenAI-shaped JSON objects) into a prompt.
Trait Implementations§
Source§impl Clone for ChatTemplate
impl Clone for ChatTemplate
Source§fn clone(&self) -> ChatTemplate
fn clone(&self) -> ChatTemplate
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for ChatTemplate
impl !UnwindSafe for ChatTemplate
impl Freeze for ChatTemplate
impl Send for ChatTemplate
impl Sync for ChatTemplate
impl Unpin for ChatTemplate
impl UnsafeUnpin for ChatTemplate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more