|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2025 Oracle and/or its affiliates. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 4 | + |
| 5 | +from typing import Dict, List, Optional |
| 6 | + |
| 7 | +from pydantic import Field |
| 8 | + |
| 9 | +from ads.aqua.common.entities import ContainerSpec |
| 10 | +from ads.aqua.config.utils.serializer import Serializable |
| 11 | + |
| 12 | + |
| 13 | +class AquaContainerConfigSpec(Serializable): |
| 14 | + """ |
| 15 | + Represents container specification details. |
| 16 | +
|
| 17 | + Attributes |
| 18 | + ---------- |
| 19 | + cli_param (Optional[str]): CLI parameter for container configuration. |
| 20 | + server_port (Optional[str]): The server port for the container. |
| 21 | + health_check_port (Optional[str]): The health check port for the container. |
| 22 | + env_vars (Optional[List[Dict]]): Environment variables for the container. |
| 23 | + restricted_params (Optional[List[str]]): Restricted parameters for container configuration. |
| 24 | + """ |
| 25 | + |
| 26 | + cli_param: Optional[str] = Field( |
| 27 | + default=None, description="CLI parameter for container configuration." |
| 28 | + ) |
| 29 | + server_port: Optional[str] = Field( |
| 30 | + default=None, description="Server port for the container." |
| 31 | + ) |
| 32 | + health_check_port: Optional[str] = Field( |
| 33 | + default=None, description="Health check port for the container." |
| 34 | + ) |
| 35 | + env_vars: Optional[List[Dict]] = Field( |
| 36 | + default_factory=list, description="List of environment variables." |
| 37 | + ) |
| 38 | + restricted_params: Optional[List[str]] = Field( |
| 39 | + default_factory=list, description="List of restricted parameters." |
| 40 | + ) |
| 41 | + |
| 42 | + class Config: |
| 43 | + extra = "allow" |
| 44 | + |
| 45 | + |
| 46 | +class AquaContainerConfigItem(Serializable): |
| 47 | + """ |
| 48 | + Represents an item of the AQUA container configuration. |
| 49 | +
|
| 50 | + Attributes |
| 51 | + ---------- |
| 52 | + name (Optional[str]): Name of the container configuration item. |
| 53 | + version (Optional[str]): Version of the container. |
| 54 | + display_name (Optional[str]): Display name for UI. |
| 55 | + family (Optional[str]): Container family or category. |
| 56 | + platforms (Optional[List[str]]): Supported platforms. |
| 57 | + model_formats (Optional[List[str]]): Supported model formats. |
| 58 | + spec (Optional[AquaContainerConfigSpec]): Container specification details. |
| 59 | + """ |
| 60 | + |
| 61 | + name: Optional[str] = Field( |
| 62 | + default=None, description="Name of the container configuration item." |
| 63 | + ) |
| 64 | + version: Optional[str] = Field( |
| 65 | + default=None, description="Version of the container." |
| 66 | + ) |
| 67 | + display_name: Optional[str] = Field( |
| 68 | + default=None, description="Display name of the container." |
| 69 | + ) |
| 70 | + family: Optional[str] = Field( |
| 71 | + default=None, description="Container family or category." |
| 72 | + ) |
| 73 | + platforms: Optional[List[str]] = Field( |
| 74 | + default_factory=list, description="Supported platforms." |
| 75 | + ) |
| 76 | + model_formats: Optional[List[str]] = Field( |
| 77 | + default_factory=list, description="Supported model formats." |
| 78 | + ) |
| 79 | + spec: Optional[AquaContainerConfigSpec] = Field( |
| 80 | + default_factory=AquaContainerConfigSpec, |
| 81 | + description="Detailed container specification.", |
| 82 | + ) |
| 83 | + |
| 84 | + class Config: |
| 85 | + extra = "allow" |
| 86 | + |
| 87 | + |
| 88 | +class AquaContainerConfig(Serializable): |
| 89 | + """ |
| 90 | + Represents a configuration of AQUA containers to be returned to the client. |
| 91 | +
|
| 92 | + Attributes |
| 93 | + ---------- |
| 94 | + inference (Dict[str, AquaContainerConfigItem]): Inference container configuration items. |
| 95 | + finetune (Dict[str, AquaContainerConfigItem]): Fine-tuning container configuration items. |
| 96 | + evaluate (Dict[str, AquaContainerConfigItem]): Evaluation container configuration items. |
| 97 | + """ |
| 98 | + |
| 99 | + inference: Dict[str, AquaContainerConfigItem] = Field( |
| 100 | + default_factory=dict, description="Inference container configuration items." |
| 101 | + ) |
| 102 | + finetune: Dict[str, AquaContainerConfigItem] = Field( |
| 103 | + default_factory=dict, description="Fine-tuning container configuration items." |
| 104 | + ) |
| 105 | + evaluate: Dict[str, AquaContainerConfigItem] = Field( |
| 106 | + default_factory=dict, description="Evaluation container configuration items." |
| 107 | + ) |
| 108 | + |
| 109 | + def to_dict(self): |
| 110 | + return { |
| 111 | + "inference": list(self.inference.values()), |
| 112 | + "finetune": list(self.finetune.values()), |
| 113 | + "evaluate": list(self.evaluate.values()), |
| 114 | + } |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def from_container_index_json( |
| 118 | + cls, |
| 119 | + config: Dict, |
| 120 | + enable_spec: Optional[bool] = False, |
| 121 | + ) -> "AquaContainerConfig": |
| 122 | + """ |
| 123 | + Creates an AquaContainerConfig instance from a container index JSON. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + config (Optional[Dict]): The container index JSON. |
| 128 | + enable_spec (Optional[bool]): If True, fetch container specification details. |
| 129 | +
|
| 130 | + Returns |
| 131 | + ------- |
| 132 | + AquaContainerConfig: The constructed container configuration. |
| 133 | + """ |
| 134 | + #TODO: Return this logic back if necessary in the next iteraion. |
| 135 | + # if not config: |
| 136 | + # config = get_container_config() |
| 137 | + |
| 138 | + inference_items: Dict[str, AquaContainerConfigItem] = {} |
| 139 | + finetune_items: Dict[str, AquaContainerConfigItem] = {} |
| 140 | + evaluate_items: Dict[str, AquaContainerConfigItem] = {} |
| 141 | + |
| 142 | + for container_type, containers in config.items(): |
| 143 | + if isinstance(containers, list): |
| 144 | + for container in containers: |
| 145 | + platforms = container.get("platforms", []) |
| 146 | + model_formats = container.get("modelFormats", []) |
| 147 | + container_spec = ( |
| 148 | + config.get(ContainerSpec.CONTAINER_SPEC, {}).get( |
| 149 | + container_type, {} |
| 150 | + ) |
| 151 | + if enable_spec |
| 152 | + else None |
| 153 | + ) |
| 154 | + container_item = AquaContainerConfigItem( |
| 155 | + name=container.get("name", ""), |
| 156 | + version=container.get("version", ""), |
| 157 | + display_name=container.get( |
| 158 | + "displayName", container.get("version", "") |
| 159 | + ), |
| 160 | + family=container_type, |
| 161 | + platforms=platforms, |
| 162 | + model_formats=model_formats, |
| 163 | + spec=( |
| 164 | + AquaContainerConfigSpec( |
| 165 | + cli_param=container_spec.get( |
| 166 | + ContainerSpec.CLI_PARM, "" |
| 167 | + ), |
| 168 | + server_port=container_spec.get( |
| 169 | + ContainerSpec.SERVER_PORT, "" |
| 170 | + ), |
| 171 | + health_check_port=container_spec.get( |
| 172 | + ContainerSpec.HEALTH_CHECK_PORT, "" |
| 173 | + ), |
| 174 | + env_vars=container_spec.get(ContainerSpec.ENV_VARS, []), |
| 175 | + restricted_params=container_spec.get( |
| 176 | + ContainerSpec.RESTRICTED_PARAMS, [] |
| 177 | + ), |
| 178 | + ) |
| 179 | + if container_spec |
| 180 | + else None |
| 181 | + ), |
| 182 | + ) |
| 183 | + if container.get("type") == "inference": |
| 184 | + inference_items[container_type] = container_item |
| 185 | + elif ( |
| 186 | + container.get("type") == "fine-tune" |
| 187 | + or container_type == "odsc-llm-fine-tuning" |
| 188 | + ): |
| 189 | + finetune_items[container_type] = container_item |
| 190 | + elif ( |
| 191 | + container.get("type") == "evaluate" |
| 192 | + or container_type == "odsc-llm-evaluate" |
| 193 | + ): |
| 194 | + evaluate_items[container_type] = container_item |
| 195 | + |
| 196 | + return cls( |
| 197 | + inference=inference_items, finetune=finetune_items, evaluate=evaluate_items |
| 198 | + ) |
0 commit comments