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();
相关推荐
受之以蒙6 小时前
Rust FFI实战指南:跨越语言边界的优雅之道
笔记·rust
柑木7 小时前
Rust-开发必备-性能调优-Benchmark基准测试
rust
勇敢牛牛_10 小时前
【Rust基础】使用Rocket从Token中提取用户信息
开发语言·rust·bootstrap·rocket
muyouking1118 小时前
4.Rust+Axum Tower 中间件实战:从集成到自定义
开发语言·中间件·rust
Source.Liu19 小时前
【TeamFlow】4.2 Yew库详细介绍
rust·yew
勇敢牛牛_19 小时前
【MRAG】使用RAG技术增强AI回复的实时性和准确性
rust·知识库·rag
techdashen21 小时前
性能比拼: Rust vs Zig vs Go
开发语言·golang·rust
muyouking111 天前
9.Rust+Axum 测试驱动开发与性能优化全攻略
驱动开发·性能优化·rust
阿阳热爱前端1 天前
BongoCat 桌宠全新升级!开源 × 跨平台,快来撸猫!
前端·rust·app