-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support chat template from GGUF (#416)
* Support loading chat template from gguf * Implement sourcing chat template from gguf * Add some logging * Handle case where using gguf chat template * Handle case where using gguf chat template * Update docs
- Loading branch information
1 parent
46b0364
commit ac1537d
Showing
13 changed files
with
187 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use anyhow::Result; | ||
use candle_core::quantized::gguf_file::Content; | ||
use tracing::info; | ||
|
||
use crate::utils::gguf_metadata::ContentMetadata; | ||
|
||
struct PropsGGUFTemplate { | ||
chat_template: Option<String>, | ||
} | ||
|
||
impl TryFrom<ContentMetadata<'_>> for PropsGGUFTemplate { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(c: ContentMetadata) -> Result<Self, Self::Error> { | ||
// No required keys | ||
|
||
let props = Self { | ||
chat_template: c.get_option_value("chat_template")?, | ||
}; | ||
|
||
Ok(props) | ||
} | ||
} | ||
|
||
// Get chat template from GGUF metadata if it exists | ||
pub fn get_gguf_chat_template(content: &Content) -> Result<Option<String>> { | ||
let metadata = ContentMetadata { | ||
path_prefix: "tokenizer", | ||
metadata: &content.metadata, | ||
}; | ||
let props = PropsGGUFTemplate::try_from(metadata)?; | ||
if let Some(ref chat_template) = props.chat_template { | ||
info!( | ||
"Discovered and using GGUF chat template: `{}`", | ||
chat_template.replace('\n', "\\n") | ||
); | ||
} | ||
Ok(props.chat_template) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod chat_template; | ||
mod gguf_tokenizer; | ||
|
||
pub use chat_template::get_gguf_chat_template; | ||
pub(crate) use gguf_tokenizer::{convert_gguf_to_hf_tokenizer, GgufTokenizerConversion}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.