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();
相关推荐
FAFU_kyp7 小时前
Rust 泛型(Generics)学习教程
开发语言·学习·rust
木木木一1 天前
Rust学习记录--C12 实例:写一个命令行程序
学习·算法·rust
柠檬丶抒情1 天前
Rust深度学习框架Burn 0.20是否能超过python?
python·深度学习·rust·vllm
Vallelonga1 天前
浅谈 Rust bindgen 工具
开发语言·rust
木木木一1 天前
Rust学习记录--C13 Part1 闭包和迭代器
开发语言·学习·rust
木木木一1 天前
Rust学习记录--C13 Part2 闭包和迭代器
开发语言·学习·rust
Vallelonga1 天前
Rust 中 extern “C“ 关键字
c语言·开发语言·rust
栈与堆3 天前
LeetCode 21 - 合并两个有序链表
java·数据结构·python·算法·leetcode·链表·rust
盛者无名3 天前
Rust的所有权(Owenership)
开发语言·rust
BlockChain8883 天前
MPC 钱包实战(三):Rust MPC Node + Java 调度层 + ETH 实际转账(可运行)
java·开发语言·rust