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();
相关推荐
星秀日12 小时前
rust学习入门
开发语言·学习·rust
mCell15 小时前
我把默认的 code . 换成了 zed .
rust·visual studio code·trae
Rust研习社15 小时前
MSRV 是什么?一文说清楚
后端·rust·编程语言
特立独行的猫a16 小时前
Rust+ Tauri实现漂亮小巧的Mqtt客户端工具--AtomMQTT Client 实现详解
开发语言·后端·mqtt·rust
咸甜适中16 小时前
rust语言学习笔记Trait(十三)Borrow、BorrowMut(借用)
笔记·学习·rust
朝阳58116 小时前
树莓派跑了个 M3U8 下载服务,内存从 600MB 降到 2MB
性能优化·rust
Tony Bai17 小时前
从 Go 迁移到 Rust
开发语言·后端·golang·rust
红尘散仙1 天前
用 cargo-dist 接管 Rust CLI 的发布:以 TRNovel 为例
rust
Rust语言中文社区1 天前
【Rust日报】2026-05-24 Secluso v1.0.2 版本发布
开发语言·后端·rust