rust 获取 hugging face 模型 chat template

前言

rust 版 tokenizer 没有直接获取 chat template的方法,这里记录一下获取的方法

方案

手动复制保存

直接在模型库右边点Chat template -> Copy,保存下来

爬虫

reqwestscraper 解析 html 取 div.SVELTE_HYDRATER.contentsdata-props 属性转成 json 再过滤取到 chat template

rust 复制代码
let repo = "Qwen/Qwen2.5-7B-Instruct";

let resp = Client::new()
    .get(format!("https://huggingface.co/{repo}"))
    .send()
    .await?;

let document = Html::parse_document(&resp.text().await?);
let selector = Selector::parse("div.SVELTE_HYDRATER.contents").unwrap();

for elem in document.select(&selector) {
    if let Some(attr) = elem.attr("data-props") {
        let attr: Value = serde_json::from_str(attr)?;
        println!("{:#?}", attr);
    }
}

api

hugging face api没有提供chat template,但是可以通过api获取模型的配置信息再得到template

rust 复制代码
let repo = "Qwen/Qwen2.5-7B-Instruct";
let repo = Api::new()?.model(repo.into());

let json: Value = repo.info_request().send().await?.json().await?;

let chat_template = json["config"]["tokenizer_config"]["chat_template"].as_str().unwrap();
相关推荐
l1t20 小时前
测试DuckDB电子表格读取插件rusty_sheet 0.2版
数据库·rust·插件·xlsx·duckdb
嚴寒1 天前
被10个终端窗口逼疯后,我用Rust写了个零依赖跨平台终端Agent启动神器
rust·agent
alwaysrun2 天前
Rust中模式匹配
rust·match·模式匹配·if let·while let·值绑定
编码浪子3 天前
Dioxus hot-dog 总结
rust
光影少年3 天前
rust生态及学习路线,应用领域
开发语言·学习·rust
Kiri霧4 天前
Linux下的Rust 与 C 的互操作性解析
c语言·开发语言·rust
大鱼七成饱4 天前
Rust 多线程编程入门:从 thread::spawn 步入 Rust 并发世界
后端·rust
ServBay5 天前
Rust 1.89更新,有哪些值得关注的新功能
后端·rust
MOON404☾5 天前
Rust程序语言设计(5-8)
开发语言·后端·rust
Vallelonga6 天前
Rust 中的数组和数组切片引用
开发语言·rust