Documentation
¶
Overview ¶
Package huggingface is a small, dependency-free client for the Hugging Face Hub API (https://huggingface.co/api). It exposes model search and per-model lookup and returns Hub-native types — it knows nothing about any particular application's domain (no model catalogs, VRAM math, or instance types here).
It is intentionally self-contained (standard library only) so it can be lifted into a shared toolkit or its own repository unchanged. Construct a Client with New and functional options; the base URL, HTTP client, and User-Agent are all injectable so callers can point it at a test server and identify themselves as good API citizens.
Index ¶
Constants ¶
const DefaultBaseURL = "https://huggingface.co"
DefaultBaseURL is the public Hugging Face Hub.
Variables ¶
var ( // ErrNotFound is returned when a model id does not exist (HTTP 404). ErrNotFound = errors.New("huggingface: not found") // ErrRateLimited is returned when the Hub asks us to slow down (HTTP 429). ErrRateLimited = errors.New("huggingface: rate limited") )
Sentinel errors callers can match with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to the Hugging Face Hub API. It is safe for concurrent use.
func New ¶
New constructs a Client. With no options it targets the public Hub with a 15s timeout and the library's default User-Agent.
func (*Client) Model ¶
Model returns a single model by id (GET /api/models/{id}), including its safetensors parameter counts when available. Returns ErrNotFound if the id does not exist.
func (*Client) Search ¶
Search returns models matching opts (GET /api/models). It is the cursor-less convenience wrapper over SearchPage; use SearchPage to paginate.
func (*Client) SearchPage ¶
SearchPage returns one page of models matching opts along with the cursor for the next page ("" when the listing is exhausted). Pass the returned cursor back in SearchOptions.Cursor to continue. The Hub paginates via an RFC 5988 Link header.
type Gated ¶
type Gated string
Gated captures the Hub's polymorphic "gated" field, which is either the JSON boolean false (open) or a string mode ("auto"/"manual") for gated repos. It unmarshals both into a string: "" means open, otherwise the mode.
func (*Gated) UnmarshalJSON ¶
UnmarshalJSON accepts either a bool or a string.
type ModelInfo ¶
type ModelInfo struct {
ID string `json:"id"`
SHA string `json:"sha"` // the repo's current commit hash — pin this for reproducible pulls
Author string `json:"author"`
PipelineTag string `json:"pipeline_tag"`
LibraryName string `json:"library_name"`
Gated Gated `json:"gated"`
Downloads int `json:"downloads"`
Likes int `json:"likes"`
CreatedAt time.Time `json:"createdAt"`
LastModified time.Time `json:"lastModified"`
Tags []string `json:"tags"`
Safetensors *Safetensors `json:"safetensors"`
}
ModelInfo is a model record as returned by the Hub. Fields not requested or not applicable to a given endpoint are zero (notably Safetensors is nil on the search list endpoint; fetch a single model to populate it).
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL overrides the API base URL (default DefaultBaseURL). Trailing slashes are trimmed.
func WithHTTPClient ¶
WithHTTPClient sets the underlying HTTP client (e.g. to inject a timeout, transport, or a test server's client).
func WithUserAgent ¶
WithUserAgent sets the User-Agent header. Identifying your application is good Hub etiquette and helps the maintainers reach you if your traffic misbehaves.
type Safetensors ¶
type Safetensors struct {
Total int64 `json:"total"`
Parameters map[string]int64 `json:"parameters"`
}
Safetensors describes a model's parameter counts as reported by the Hub's safetensors metadata. Total is the overall parameter count; Parameters breaks it down by tensor dtype (e.g. {"BF16": 8190735360}).
func (*Safetensors) DominantDtype ¶
func (s *Safetensors) DominantDtype() string
DominantDtype returns the dtype holding the most parameters (e.g. "BF16"), or "" if unknown. It is the dtype a caller would assume for a size estimate.
type SearchOptions ¶
type SearchOptions struct {
Search string // free-text query
Author string // restrict to an author/org
PipelineTag string // e.g. "text-generation"
Filter string // tag filter, e.g. "gguf"
Sort string // e.g. "downloads", "likes", "trendingScore", "createdAt"
Direction int // -1 descending, 1 ascending; 0 omits
Limit int // max results; 0 omits
Full bool // request full metadata
Expand []string // request specific fields via expand[]=; e.g. "safetensors", "tags".
// Note: the Hub treats Expand as mutually exclusive with Full — when expand[] is
// present, full is ignored and only the listed fields (plus id) are returned.
Cursor string // pagination token from a prior page's Link header; "" for the first page
}
SearchOptions parameterizes a model search. Zero-value fields are omitted from the request, yielding the Hub's defaults.