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();
相关推荐
维维酱4 小时前
Rust - 互斥锁
rust
维维酱4 小时前
Rust - 共享状态的并发
rust
ArcX6 小时前
从 JS 到 Rust 的旅程
前端·javascript·rust
Humbunklung6 小时前
Rust Floem UI 框架使用简介
开发语言·ui·rust
寻月隐君11 小时前
深入解析 Rust 的面向对象编程:特性、实现与设计模式
后端·rust·github
KENYCHEN奉孝1 天前
基于 actix-web 框架的简单 demo
前端·rust
love530love1 天前
【笔记】旧版MSYS2 环境中 Rust 升级问题及解决过程
开发语言·人工智能·windows·笔记·python·rust·virtualenv
Humbunklung1 天前
Rust 函数
开发语言·后端·rust
荣江1 天前
【实战】基于 Tauri 和 Rust 实现基于无头浏览器的高可用网页抓取
后端·rust
susnm1 天前
创建你的第一个 Dioxus app
rust·全栈