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();
相关推荐
明理的信封13 小时前
AI 基础设施的“去 Python 化“:Rust 与 C# 的两条替代路径
人工智能·python·rust
doiito1 天前
【Agent Harness】Gliding Horse v0.1.4.preview 发布:时间感知、闭环审计与智能增强
ai·rust·架构设计·系统设计·ai agent
独孤留白1 天前
从C到Rust:堆内存,数据如何逃逸栈的约束?
rust
doiito(Do It Together)1 天前
【安全,架构】RustyVault 项目深度分析报告以及和HashiCorp Vault的差异
微服务·架构·rust
问窗1 天前
Rust实现Windows本地搜索工具
windows·搜索引擎·rust
程序员爱钓鱼1 天前
Rust 变量与不可变性:为什么默认不能修改变量?
前端·后端·rust
第一程序员1 天前
Rust Agent 子进程执行:Command 之前,先定义输入和超时
python·rust·github
IT笔记2 天前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
取地址符2 天前
Rust学习笔记(基于Rustlings)(2):数据结构与集合
笔记·学习·rust
取地址符2 天前
Rust语法学习 (基于Rustlings)(1):基础语法
经验分享·笔记·学习·rust