llama_cpp_bindings/model/llama_chat_template.rs
1use std::ffi::{CStr, CString};
2use std::str::Utf8Error;
3
4/// A performance-friendly wrapper around [`super::LlamaModel::chat_template`] which is then
5/// fed into [`super::LlamaModel::apply_chat_template`] to convert a list of messages into an LLM
6/// prompt. Internally the template is stored as a `CString` to avoid round-trip conversions
7/// within the FFI.
8#[derive(Eq, PartialEq, Clone, PartialOrd, Ord, Hash)]
9pub struct LlamaChatTemplate(pub CString);
10
11impl LlamaChatTemplate {
12 /// Create a new template from a string. This can either be the name of a llama.cpp [chat template](https://github.com/ggerganov/llama.cpp/blob/8a8c4ceb6050bd9392609114ca56ae6d26f5b8f5/src/llama-chat.cpp#L27-L61)
13 /// like "chatml" or "llama3" or an actual Jinja template for llama.cpp to interpret.
14 ///
15 /// # Errors
16 /// Returns an error if the template string contains null bytes.
17 pub fn new(template: &str) -> Result<Self, std::ffi::NulError> {
18 Ok(Self(CString::new(template)?))
19 }
20
21 /// Accesses the template as a c string reference.
22 #[must_use]
23 pub fn as_c_str(&self) -> &CStr {
24 &self.0
25 }
26
27 /// Attempts to convert the `CString` into a Rust str reference.
28 ///
29 /// # Errors
30 /// Returns an error if the template is not valid UTF-8.
31 pub fn to_str(&self) -> Result<&str, Utf8Error> {
32 self.0.to_str()
33 }
34
35 /// Convenience method to create an owned String.
36 ///
37 /// # Errors
38 /// Returns an error if the template is not valid UTF-8.
39 pub fn to_string(&self) -> Result<String, Utf8Error> {
40 self.to_str().map(str::to_string)
41 }
42}
43
44impl std::fmt::Debug for LlamaChatTemplate {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 self.0.fmt(f)
47 }
48}