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();
相关推荐
T1an-16 小时前
Axum web框架【实习】
学习·rust
中国胖子风清扬6 小时前
Rust 日志库完全指南:从入门到精通
spring boot·后端·rust·学习方法·logback
小喷友9 小时前
阶段四:实战(项目开发能力)
前端·rust
小喷友1 天前
阶段三:进阶(Rust 高级特性)
前端·rust
Python私教1 天前
源滚滚Rust全栈班v1.02 无符号整数详解
开发语言·后端·rust
专注VB编程开发20年2 天前
CSS 的命名方式像是 PowerShell 的动词-名词结构,缺乏面向对象的层级关系
开发语言·后端·rust
伍哥的传说2 天前
Tailwind CSS v4 终极指南:体验 Rust 驱动的闪电般性能与现代化 CSS 工作流
前端·css·rust·tailwindcss·tailwind css v4·lightning css·utility-first
专注VB编程开发20年2 天前
rust语言-对象多级访问
服务器·前端·rust
编码浪子2 天前
趣味学RUST基础篇(构建一个命令行程序2重构)
开发语言·重构·rust
susnm2 天前
组件生命周期
rust·全栈