Class HuggingFaceModel


public class HuggingFaceModel extends OpenAiCompatibleModel
A specialized model instance that encapsulates its own metadata discovery from the Hugging Face Hub.

Fetches and parses architectural specifications (config.json), chat templates and tokenizer settings (tokenizer_config.json), and sampling parameters (generation_config.json) in parallel, enriching generation requests with explicit max_tokens limits.

Author:
anahata
  • Field Details

    • hubConfig

      private com.fasterxml.jackson.databind.JsonNode hubConfig
      The raw JSON from the model's 'config.json', containing architectural details and token limits.
    • tokenizerConfig

      private com.fasterxml.jackson.databind.JsonNode tokenizerConfig
      The raw JSON from the model's 'tokenizer_config.json', used to discover chat templates and tool support markers.
    • generationConfig

      private com.fasterxml.jackson.databind.JsonNode generationConfig
      The raw JSON from the model's 'generation_config.json', used to extract default sampling parameters like temperature and top-p.
  • Constructor Details

    • HuggingFaceModel

      public HuggingFaceModel(HuggingFaceProvider provider, String modelId, String displayName)
      Constructs a new Hugging Face model instance.
      Parameters:
      provider - The owning HF provider.
      modelId - The full repo ID.
      displayName - The display name.
  • Method Details

    • getProvider

      public HuggingFaceProvider getProvider()
      Returns the parent OpenAiChatCompletionsProvider instance owning this model.

      Returns the typed parent HuggingFaceProvider owning this model.

      Overrides:
      getProvider in class OpenAiCompatibleModel
      Returns:
      The OpenAI-compatible provider instance.
    • inspect

      public void inspect(String apiKey)
      Performs deep inspection of this model by fetching its configuration files from the Hugging Face Hub sequentially in a single thread, using a pre-resolved API key.
      Parameters:
      apiKey - The pre-resolved API key to avoid lock contention with the provider.
    • fetchHubJson

      private com.fasterxml.jackson.databind.JsonNode fetchHubJson(String filename, String apiKey)
      Helper to fetch a JSON file from the Hugging Face Hub's main branch.
      Parameters:
      filename - The config filename (e.g. 'config.json').
      apiKey - The pre-resolved Bearer token.
      Returns:
      The parsed JSON or null if not found/error.
    • enrichPayload

      protected void enrichPayload(com.fasterxml.jackson.databind.node.ObjectNode payload, GenerationRequest request)
      Hook for subclasses to add or modify payload parameters before sending the request.

      Implementation details: Ensures that max_tokens is populated in the request payload so Hugging Face router does not truncate generations at its low server default.

      Overrides:
      enrichPayload in class OpenAiCompatibleModel
      Parameters:
      payload - The JSON payload being constructed.
      request - The generation request.
    • buildHubHtmlDescription

      public String buildHubHtmlDescription()
      Builds a rich HTML view combining architecture, tokenizer class, and deep-inspected sampling parameters.
      Returns:
      The formatted HTML summary string.