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();
相关推荐
zandy101121 小时前
衡石科技|HENGSHI CLI登场,以Rust架构驱动BI自动驾驶
开发语言·科技·rust
沛沛rh4521 小时前
用 Rust 实现用户态调试器:mini-debugger项目原理剖析与工程复盘
开发语言·c++·后端·架构·rust·系统架构
BugShare1 天前
一个用 Rust 编写的、速度极快的 Python 包和项目管理器
开发语言·python·rust
Binarydog_Lee1 天前
Rust 生命周期机制详解:彻底理解 ‘static
rust
techdashen1 天前
Rust 正式成立 Types Team:类型系统终于有了专属团队
开发语言·后端·rust
bilI LESS1 天前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
Rust研习社1 天前
深入理解 Rust 裸指针:内存操作的双刃剑
开发语言·后端·rust
浪客川2 天前
【百例RUST - 010】字符串
开发语言·后端·rust
好家伙VCC2 天前
# 发散创新:用 Rust实现高性能物理引擎的底层架构设计与实战在游戏开发、虚拟仿真和机器人控
java·开发语言·python·rust·机器人
代码羊羊2 天前
Rust所有权与引用:核心要点速记
rust