Model Config

The ModelConfig class is used to define the configuration for various types of models you want to manage or use with BerryDB. It utilizes a builder pattern to allow for a clear and step-by-step definition of model attributes.

You start by choosing a provider-specific builder, set the necessary attributes, and then call build() to get a ModelConfig instance. This instance can then be used, for example, with ModelRepo.save() to register the model in BerryDB.

ModelConfig Static Methods

Fetching Available Model Categories

static ModelConfig.get_available_model_categories(api_key)

Fetches the available model categories and their subcategories from BerryDB.

This can be used to understand the valid options for the category and subcategory parameters when configuring Hugging Face or Custom models. The category and subcategory help in organizing and discovering models within BerryDB. The SDK must be initialized using BerryDB.init(host) before calling this method.

Returns:

  • Dict[str, List[str]]: A dictionary where keys are category names (str) and values are lists of subcategory names (List[str]) for that category.

Parameters:

  • api_key (str): The API key for authenticating with BerryDB.

Example:

 from berrydb import ModelConfig, BerryDB

# Ensure BerryDB SDK is initialized
# BerryDB.init("YOUR_BERRYDB_HOST")
berrydb_api_key = "BERRYDB_API_KEY" # Replace with your actual API key

try:
    categories_data = ModelConfig.get_available_model_categories(api_key=berrydb_api_key)
    # categories_data will look like:
    # {
    #     "Generative AI": [
    #         "Supervised Language Model Fine-tuning",
    #         "Human Preference collection for RLHF",
    #         # ... other subcategories ...
    #     ],
    #     "Others": ["Others"]
    # }
    for category_name, subcategories_list in categories_data.items():
        print(f"Category: {category_name}")
        for subcategory_name in subcategories_list:
            print(f"  Subcategory: {subcategory_name}")
except Exception as e:
    print(f"Error fetching categories: {e}")

ModelConfig (Builder Entry Points)

These class methods are the starting points for creating a model configuration for a specific provider.

classmethod ModelConfig.huggingface_builder()

Returns a builder for creating Hugging Face model configurations.

This class method provides a convenient way to construct a ModelConfig specifically tailored for Hugging Face models.

Returns:

  • ModelConfig.HuggingFaceModelConfigBuilder: An instance of the builder that can be used to set Hugging Face specific model attributes.

Example:

from berrydb import ModelConfig

# Configure the Hugging Face model
hf_config = (
    ModelConfig.huggingface_builder()
    .name("my-sentiment-analyzer")
    .description("A sentiment analysis model from Hugging Face.")
    .hf_model_name("distilbert-base-uncased-finetuned-sst-2-english")
    .build(api_key="BERRYDB_API_KEY")
)

# The hf_config object is now ready to be used, for example, with repo.save()
classmethod ModelConfig.vertexai_builder()

Returns a builder for creating Vertex AI model configurations.

This class method provides a convenient way to construct a ModelConfig specifically tailored for requesting models from Vertex AI.

Returns:

  • ModelConfig.VertexAIModelConfigBuilder: An instance of the builder that can be used to set Vertex AI specific model attributes for a request.

Example:

from berrydb import ModelConfig

# Configure the Vertex AI model request
vertex_config = (
    ModelConfig.vertexai_builder()
    .name("gemini-pro-request")  # Name for your reference in BerryDB
    .request_model("gemini-pro")  # The actual Vertex AI model name to request
    .notes("Requesting a Gemini Pro model for text generation.")
    .build(api_key="BERRYDB_API_KEY")
)

# The vertex_config object is now ready to be used, for example, with repo.request()
classmethod ModelConfig.berrydb_builder()

Returns a builder for creating BerryDB model configurations.

This class method provides a convenient way to construct a ModelConfig specifically tailored for models managed by BerryDB.

Returns:

  • ModelConfig.BerryDBModelConfigBuilder: An instance of the builder that can be used to set BerryDB specific model attributes.

Example:

from berrydb import ModelConfig

# Configure the BerryDB model
berrydb_config = (
    ModelConfig.berrydb_builder()
    .name("my-internal-model")
    .description("An internal model managed by BerryDB.")
    .build(api_key="BERRYDB_API_KEY")
)

# The berrydb_config object is now ready to be used.

ModelConfig.Builder Methods

Once you have a builder instance (e.g., HuggingFaceModelConfigBuilder, VertexAIModelConfigBuilder, etc.), you can use the following methods to set the configuration attributes. Some methods are common across all builders, while others are specific to a particular model provider.

Hugging Face Specific Builder Methods

HuggingFaceModelConfigBuilder.name(name)

Sets the name for the Hugging Face model configuration.

This name is used to identify the model configuration within BerryDB.

Parameters:

  • name (str): The desired name for the model configuration.

Returns:

  • ModelConfig.HuggingFaceModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

hf_builder = ModelConfig.huggingface_builder()
hf_config = (
    hf_builder
    .name("my-awesome-hf-model")
    .hf_model_name("bert-base-uncased")
    # ... other configurations ...
    .build(api_key="BERRYDB_API_KEY")
)
# hf_config.name will be "my-awesome-hf-model"
HuggingFaceModelConfigBuilder.description(desc)

Sets the description for the Hugging Face model configuration.

This provides additional context or details about the model.

Parameters:

  • desc (str): The description string for the model configuration.

Returns:

  • ModelConfig.HuggingFaceModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

hf_builder = ModelConfig.huggingface_builder()
hf_config = (
    hf_builder
    .description("A T5 model fine-tuned for English to French translation.")
    .name("my-hf-translator")
    .hf_model_name("t5-small")
    .build(api_key="BERRYDB_API_KEY")
)
HuggingFaceModelConfigBuilder.hf_model_name(hf_model_name)

Sets the Hugging Face model ID for the configuration.

This is the identifier of the model on the Hugging Face Hub (e.g., “bert-base-uncased”, “t5-small”).

Parameters:

  • hf_model_name (str): The Hugging Face model ID.

Returns:

  • ModelConfig.HuggingFaceModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

hf_builder = ModelConfig.huggingface_builder()
hf_config = (
    hf_builder
    .hf_model_name("distilbert-base-uncased-finetuned-sst-2-english")
    .name("my-hf-sentiment-model")
    .build(api_key="BERRYDB_API_KEY")
)
HuggingFaceModelConfigBuilder.build(api_key)

Constructs a ModelConfig instance for a Hugging Face model.

This method uses all the settings previously configured on the builder to create a new ModelConfig object.

Returns:

  • ModelConfig: A new ModelConfig instance configured for a Hugging Face model.

Example:

from berrydb import ModelConfig

hf_builder = ModelConfig.huggingface_builder()
hf_config = (
    hf_builder
    .name("my-hf-model")
    .hf_model_name("bert-base-uncased")
    .description("A standard BERT model.")
    .build(api_key="BERRYDB_API_KEY") # This call creates the ModelConfig instance
)
# hf_config is now an instance of ModelConfig

Vertex AI Specific Builder Methods

VertexAIModelConfigBuilder.name(name)

Sets the reference name for the Vertex AI model configuration.

This name is used to identify this specific model request/configuration within BerryDB.

Parameters:

  • name (str): The desired name for the Vertex AI model configuration.

Returns:

  • ModelConfig.VertexAIModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

vertex_builder = ModelConfig.vertexai_builder()
vertex_config = (
    vertex_builder
    .name("my-gemini-pro-request") # Name for BerryDB reference
    .request_model("gemini-pro")    # Actual Vertex AI model
    .build(api_key="BERRYDB_API_KEY")
)
VertexAIModelConfigBuilder.request_model(request_model)

Sets the Vertex AI model to be requested.

This specifies the identifier of the model from Google Vertex AI that BerryDB should attempt to make available (e.g., “gemini-pro”, “text-bison@001”).

Parameters:

  • request_model (str): The Vertex AI model identifier.

Returns:

  • ModelConfig.VertexAIModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

vertex_builder = ModelConfig.vertexai_builder()
vertex_config = (
    vertex_builder
    .name("my-vertex-text-model") # Name for BerryDB reference
    .request_model("text-bison@001") # The Vertex AI model to request
    .build(api_key="BERRYDB_API_KEY")
)
VertexAIModelConfigBuilder.notes(notes)

Sets optional notes for the Vertex AI model request.

These notes can be used to add any relevant comments or metadata associated with the model request.

Parameters:

  • notes (str): A string containing notes for the model request.

Returns:

  • ModelConfig.VertexAIModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

vertex_builder = ModelConfig.vertexai_builder()
vertex_config = (
    vertex_builder
    .name("my-vertex-image-model")
    .request_model("imagegeneration@005")
    .notes("Requesting for internal testing of image generation capabilities.")
    .build(api_key="BERRYDB_API_KEY")
)
VertexAIModelConfigBuilder.build(api_key)

Constructs a ModelConfig instance for a Vertex AI model request.

This method uses all the settings previously configured on the builder to create a new ModelConfig object. The API key is required for potential validation steps during model configuration creation.

Parameters:

  • api_key (str): The API key for BerryDB.

Returns:

  • ModelConfig: A new ModelConfig instance configured for a Vertex AI model request.

Example:

from berrydb import ModelConfig

vertex_builder = ModelConfig.vertexai_builder()
vertex_config = (
    vertex_builder
    .name("my-vertex-model-request")
    .request_model("gemini-pro")
    .notes("Requesting for text generation.")
    .build(api_key="BERRYDB_API_KEY") # Pass API key here
)
# vertex_config is now an instance of ModelConfig

Custom Model Specific Builder Methods

CustomModelConfigBuilder.name(name)

Sets the name for the Custom model configuration.

This name is used to identify the model configuration within BerryDB.

Parameters:

  • name (str): The desired name for the model configuration.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-custom-image-classifier")
    # ... other configurations for the custom model ...
    .build(api_key="BERRYDB_API_KEY")
)
# custom_config.name will be "my-custom-image-classifier"
CustomModelConfigBuilder.description(desc)

Sets the description for the Custom model configuration.

This provides additional context or details about the model.

Parameters:

  • desc (str): The description string for the model configuration.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-object-detector")
    .description("A custom model trained to detect various objects in images.")
    # ... other configurations for the custom model ...
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.self_hosted(hosted=True)

Specifies if the Custom model is self-hosted.

If True, BerryDB expects the model to be accessible via a URL provided by hosted_url(). If False (or not set, as False is the default if this method isn’t called after upload_file_path or upload_file_url is used), BerryDB will host the model, and you must provide model artifacts via upload_file_path() or upload_file_url().

Parameters:

  • hosted (bool, optional): True if the model is self-hosted, False otherwise. Defaults to True if called without arguments.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

# Configure a self-hosted model
self_hosted_builder = ModelConfig.custom_builder()
self_hosted_config = (
    self_hosted_builder
    .name("my-external-api-model")
    .self_hosted(True) # or simply .self_hosted()
    .hosted_url("http://my.service.com/predict")
    .build(api_key="BERRYDB_API_KEY")
)

Note

self_hosted is implicitly False

CustomModelConfigBuilder.upload_file_path(path)

Sets the local file path(s) for the Custom model artifacts.

Use this method when BerryDB is hosting the model (self_hosted is False) and you are providing the model files from your local filesystem. The provided path(s) will be uploaded to BerryDB.

Parameters:

  • path (List[str]): A list of strings, where each string is a path to a model file to be uploaded. If the model requires a directory structure, it should be zipped before upload.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-local-pytorch-model")
    .description("A PyTorch model uploaded from local files.")
    .upload_file_path(["/path/to/my_model.pt", "/path/to/config.json"]) # or ["/path/to/model_directory.zip"]
    .framework("pytorch")
    .framework_version("1.9")
    # .self_hosted(False) # Implicitly False when upload_file_path is used
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.upload_file_url(url)

Sets the URL from which Custom model artifacts can be downloaded.

Use this method when BerryDB is hosting the model (self_hosted is False) and your model files are accessible via a public URL. BerryDB will download the artifacts from this URL.

Parameters:

  • url (str): The direct downloadable URL to the model artifact(s) (e.g., a URL to a .zip file).

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-remote-sklearn-model")
    .description("An sklearn model downloaded from a URL.")
    .upload_file_url("http://example.com/models/my_sklearn_model.zip")
    .framework("scikit-learn")
    .framework_version("1.0")
    # .self_hosted(False) # Implicitly False when upload_file_url is used
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.hosted_url(url)

Sets the prediction endpoint URL for a self-hosted Custom model.

Use this method when self_hosted is True. BerryDB will use this URL to send prediction requests to your model.

Parameters:

  • url (str): The complete URL of your model’s prediction endpoint.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-external-sentiment-analyzer")
    .self_hosted(True) # Indicate it's self-hosted
    .hosted_url("https://my.custom.model.api/predict") # Provide the endpoint
    .description("A sentiment analysis model hosted on my own server.")
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.framework(framework)

Sets the framework for the Custom model.

This specifies the machine learning framework used by your model (e.g., “keras”, “pytorch”, “scikit-learn”). This information is needed by BerryDB when hosting the model.

Parameters:

  • framework (str): The name of the machine learning framework.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-tf-model")
    .framework("keras")
    # ... other configurations for a tensorflow model ...
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.framework_version(version)

Sets the framework version for the Custom model.

This specifies the version of the machine learning framework (e.g., “2.14” for TensorFlow/Keras, “1.9” for PyTorch). This information is needed by BerryDB when hosting the model.

Parameters:

  • version (str): The version string of the machine learning framework.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-tf-model-specific-version")
    .framework("keras")
    .framework_version("2.14")
    # ... other configurations ...
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.hardware_accelerator(accel=True)

Specifies if a hardware accelerator (GPU) should be used for the Custom model.

This is applicable when BerryDB is hosting the model (self_hosted is False). Setting this to True requests a GPU for the model deployment.

Parameters:

  • accel (bool, optional): True to request a hardware accelerator (GPU), False otherwise. Defaults to True.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config_gpu = (
    custom_builder
    .name("my-gpu-intensive-model")
    .upload_file_path(["/path/to/model_files.zip"])
    .framework("pytorch")
    .framework_version("1.10")
    .hardware_accelerator(True) # Request GPU
    .build(api_key="BERRYDB_API_KEY")
)

custom_builder_cpu = ModelConfig.custom_builder()
custom_config_cpu = (
    custom_builder_cpu
    .name("my-cpu-model")
    .upload_file_path(["/path/to/other_model.zip"])
    .framework("sklearn")
    .framework_version("1.0")
    .hardware_accelerator(False) # Explicitly request CPU
    .build(api_key="BERRYDB_API_KEY")
)
CustomModelConfigBuilder.build(api_key)

Constructs a ModelConfig instance for a Custom model.

This method uses all the settings previously configured on the builder to create a new ModelConfig object.

Returns:

  • ModelConfig: A new ModelConfig instance configured for a Custom model.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-custom-model")
    .description("A custom model for a specific task.")
    .self_hosted(False)
    .upload_file_path(["/path/to/model.zip"])
    .framework("tensorflow")
    .framework_version("2.7")
    .build(api_key="BERRYDB_API_KEY") # This call creates the ModelConfig instance
)
# custom_config is now an instance of ModelConfig

BerryDB Model Specific Builder Methods

Note

BerryDB specific models are typically internal.

CustomModelConfigBuilder.name(name)

Sets the name for the Custom model configuration.

This name is used to identify the model configuration within BerryDB.

Parameters:

  • name (str): The desired name for the model configuration.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-custom-image-classifier")
    # ... other configurations for the custom model ...
    .build(api_key="BERRYDB_API_KEY")
)
# custom_config.name will be "my-custom-image-classifier"
CustomModelConfigBuilder.description(desc)

Sets the description for the Custom model configuration.

This provides additional context or details about the model.

Parameters:

  • desc (str): The description string for the model configuration.

Returns:

  • ModelConfig.CustomModelConfigBuilder: The builder instance, allowing for method chaining.

Example:

from berrydb import ModelConfig

custom_builder = ModelConfig.custom_builder()
custom_config = (
    custom_builder
    .name("my-object-detector")
    .description("A custom model trained to detect various objects in images.")
    # ... other configurations for the custom model ...
    .build(api_key="BERRYDB_API_KEY")
)