name: hugging-face-tool-builder description: 当用户想要构建工具/脚本或完成任务时使用此技能,其中使用来自Hugging Face API的数据会有所帮助。这在链式或组合API调用或任务将重复/自动化时特别有用。此技能创建可重复使用的脚本来获取、丰富或处理数据。
Hugging Face API 工具构建器
您的目的现在是创建可重复使用的命令行脚本和实用程序,用于使用Hugging Face API,允许在有帮助的地方进行链式调用、管道处理和中间处理。您可以直接访问API,以及使用hf命令行工具。模型和数据集卡片可以直接从仓库访问。
脚本规则
确保遵循这些规则:
- 脚本必须接受
--help命令行参数来描述其输入和输出 - 非破坏性脚本应在交给用户之前进行测试
- 首选Shell脚本,但如果复杂性或用户需求需要,使用Python或TSX
- 重要:使用
HF_TOKEN环境变量作为授权头。例如:curl -H "Authorization: Bearer ${HF_TOKEN}" https://huggingface.co/api/。这提供更高的速率限制和适当的数据访问授权。 - 在确定最终设计之前调查API结果的形状;在可组合性有优势的地方使用管道和链式调用 - 尽可能首选简单解决方案。
- 完成后分享使用示例。
确保在有问题或需要澄清时确认用户偏好。
示例脚本
下面的路径是相对于此技能目录的。
参考示例:
references/hf_model_papers_auth.sh— 自动使用HF_TOKEN并链式处理趋势 → 模型元数据 → 模型卡片解析与回退;它演示了多步API使用以及用于门控/私有内容的授权卫生。references/find_models_by_paper.sh— 通过--token可选使用HF_TOKEN,一致的身份验证搜索,以及当arXiv前缀搜索太窄时的重试路径;它展示了弹性查询策略和清晰的用户面向帮助。references/hf_model_card_frontmatter.sh— 使用hfCLI下载模型卡片,提取YAML前置元数据,并发射NDJSON摘要(许可证、管道标签、标签、门控提示标志)以便于过滤。
基线示例(超简单,最小逻辑,原始JSON输出带HF_TOKEN头):
references/baseline_hf_api.sh— bashreferences/baseline_hf_api.py— pythonreferences/baseline_hf_api.tsx— typescript可执行文件
可组合实用程序(stdin → NDJSON):
references/hf_enrich_models.sh— 从stdin读取模型ID,为每个ID获取元数据,为流式管道发射每行一个JSON对象。
通过管道实现可组合性(shell友好的JSON输出):
references/baseline_hf_api.sh 25 | jq -r '.[].id' | references/hf_enrich_models.sh | jq -s 'sort_by(.downloads) | reverse | .[:10]'references/baseline_hf_api.sh 50 | jq '[.[] | {id, downloads}] | sort_by(.downloads) | reverse | .[:10]'printf '%s ' openai/gpt-oss-120b meta-llama/Meta-Llama-3.1-8B | references/hf_model_card_frontmatter.sh | jq -s 'map({id, license, has_extra_gated_prompt})'
高级端点
以下是在https://huggingface.co可用的主要API端点
/api/datasets
/api/models
/api/spaces
/api/collections
/api/daily_papers
/api/notifications
/api/settings
/api/whoami-v2
/api/trending
/oauth/userinfo
访问API
API使用OpenAPI标准在https://huggingface.co/.well-known/openapi.json文档化。
重要: 不要尝试直接读取https://huggingface.co/.well-known/openapi.json,因为它太大而无法处理。
重要 使用jq查询和提取相关部分。例如,
获取所有160个端点的命令
curl -s "https://huggingface.co/.well-known/openapi.json" | jq '.paths | keys | sort'
模型搜索端点详情
curl -s "https://huggingface.co/.well-known/openapi.json" | jq '.paths["/api/models"]'
您也可以查询端点以查看数据的形状。当这样做时,将结果限制在低数字以便易于处理,但仍然具有代表性。
使用HF命令行工具
hf命令行工具为您提供对Hugging Face仓库内容和基础设施的进一步访问。
❯ hf --help
Usage: hf [OPTIONS] COMMAND [ARGS]...
Hugging Face Hub CLI
Options:
--help Show this message and exit.
Commands:
auth Manage authentication (login, logout, etc.).
cache Manage local cache directory.
download Download files from the Hub.
endpoints Manage Hugging Face Inference Endpoints.
env Print information about the environment.
jobs Run and manage Jobs on the Hub.
repo Manage repos on the Hub.
repo-files Manage files in a repo on the Hub.
upload Upload a file or a folder to the Hub.
upload-large-folder Upload a large folder to the Hub.
version Print information about the hf version.
hf CLI命令已取代现在已弃用的huggingface_hub CLI命令。