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();
相关推荐
Patrick_Wilson11 小时前
sccache 用在 Rust 上为什么常「不省编译」:原理、限制与 Windows 接入
ci/cd·rust·编译器
k4m7v2pz17 小时前
rvs 26.8.43 → 26.8.53 更新概览:MCP 落地、审计增强与谬误澄清
rust·repl·mcp server·rust-verb-shell·rvs·shell 工具
程序员爱钓鱼18 小时前
Rust impl详解:为Struct定义方法与关联函数
前端·后端·rust
k4m7v2pz19 小时前
Rust 程序在 ReactOS 上“加载即崩“:从崩溃地址 0x6107104d 到 CRT 依赖的深度排查
rust·交叉编译·逆向工程·reactos·pe格式·windows兼容
Thneonl1 天前
一个人从 Rust 内核做到 React 前端:我做了一个云原生 SRE 巡检图谱桌面工具
rust
程序员爱钓鱼1 天前
Rust Struct结构体详解:定义自己的复杂数据类型
后端·面试·rust
苏灿烤鱼1 天前
公司**不可计算,就自己做操作系统
rust·typescript·agent
nnerddboy2 天前
Rust教程04:引用、借用与切片
开发语言·后端·rust
咸甜适中2 天前
rust语言AI编程学习笔记(五)clap命令行参数解析
学习·rust·ai编程
梦醒沉醉2 天前
19、Rust程序设计语言——高级特性
rust