aixplain.modules.model.utility_model
Copyright 2024 The aiXplain SDK authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Author: Thiago Castro Ferreira, Shreyas Sharma and Lucas Pavanelli Date: November 25th 2024 Description: Utility Model Class
BaseUtilityModelParams Objects
class BaseUtilityModelParams(BaseModel)
Base model for utility model parameters.
This class defines the basic parameters required to create or update a utility model.
Attributes:
nameText - The name of the utility model.codeUnion[Text, Callable] - The implementation code, either as a string or a callable function.descriptionOptional[Text] - A description of what the utility model does. Defaults to None.
UtilityModelInput Objects
@dataclass
class UtilityModelInput()
A class representing an input parameter for a utility model.
This class defines the structure and validation rules for input parameters that can be used with utility models.
Attributes:
nameText - The name of the input parameter.descriptionText - A description of what this input parameter represents.typeDataType - The data type of the input parameter. Must be one of: TEXT, BOOLEAN, or NUMBER. Defaults to DataType.TEXT.
validate
def validate()
Validate that the input parameter has a supported data type.
Raises:
ValueError- If the type is not one of: TEXT, BOOLEAN, or NUMBER.
to_dict
def to_dict()
Convert the input parameter to a dictionary representation.
Returns:
dict- A dictionary containing the input parameter's name, description, and type (as a string value).
utility_tool
def utility_tool(name: Text,
description: Text,
inputs: List[UtilityModelInput] = None,
output_examples: Text = "",
status=AssetStatus.DRAFT)
Decorator for utility tool functions
Arguments:
name- Name of the utility tooldescription- Description of what the utility tool doesinputs- List of input parameters, must be UtilityModelInput objectsoutput_examples- Examples of expected outputsstatus- Asset status
Raises:
ValueError- If name or description is emptyTypeError- If inputs contains non-UtilityModelInput objects
UtilityModel Objects
class UtilityModel(Model, DeployableMixin)
Ready-to-use Utility Model.
Note: Non-deployed utility models (status=DRAFT) will expire after 24 hours after creation. Use the .deploy() method to make the model permanent.
Attributes:
idText - ID of the ModelnameText - Name of the ModelcodeUnion[Text, Callable] - code of the model.descriptionText - description of the model. Defaults to "".inputsList[UtilityModelInput] - inputs of the model. Defaults to [].output_examplesText - output examples. Defaults to "".api_keyText, optional - API key of the Model. Defaults to None.supplierUnion[Dict, Text, Supplier, int], optional - supplier of the asset. Defaults to "aiXplain".versionText, optional - version of the model. Defaults to "1.0".functionFunction, optional - model AI function. Defaults to None.name0 bool, optional - Is the user subscribed. Defaults to False.name1 Dict, optional - model price. Defaults to None.name2 AssetStatus, optional - status of the model. Defaults to AssetStatus.DRAFT.name3 - Any additional Model info to be saved
__init__
def __init__(id: Text,
name: Optional[Text] = None,
code: Union[Text, Callable] = None,
description: Optional[Text] = None,
inputs: List[UtilityModelInput] = [],
output_examples: Text = "",
api_key: Optional[Text] = None,
supplier: Union[Dict, Text, Supplier, int] = "aiXplain",
version: Optional[Text] = None,
function: Optional[Function] = None,
is_subscribed: bool = False,
cost: Optional[Dict] = None,
status: AssetStatus = AssetStatus.DRAFT,
function_type: Optional[FunctionType] = FunctionType.UTILITY,
**additional_info) -> None
Initialize a new UtilityModel instance.
Arguments:
idText - ID of the utility model.nameOptional[Text], optional - Name of the utility model. If not provided, will be extracted from the code if decorated. Defaults to None.codeUnion[Text, Callable], optional - Implementation code, either as a string or a callable function. Defaults to None.descriptionOptional[Text], optional - Description of what the model does. If not provided, will be extracted from the code if decorated. Defaults to None.inputsList[UtilityModelInput], optional - List of input parameters the model accepts. If not provided, will be extracted from the code if decorated. Defaults to [].output_examplesText, optional - Examples of the model's expected outputs. Defaults to "".api_keyOptional[Text], optional - API key for accessing the model. Defaults to None.supplierUnion[Dict, Text, Supplier, int], optional - Supplier of the model. Defaults to "aiXplain".versionOptional[Text], optional - Version of the model. Defaults to None.functionOptional[Function], optional - Function type. Must be Function.UTILITIES. Defaults to None.name0 bool, optional - Whether the user is subscribed. Defaults to False.name1 Optional[Dict], optional - Cost information for the model. Defaults to None.name2 AssetStatus, optional - Current status of the model. Defaults to AssetStatus.DRAFT.name3 Optional[FunctionType], optional - Type of the function. Defaults to FunctionType.UTILITY.name4 - Any additional model info to be saved.
Raises:
name5 - If function is not Function.UTILITIES.
Notes:
Non-deployed utility models (status=DRAFT) will expire after 24 hours. Use the .deploy() method to make the model permanent.
validate
def validate()
Validate the Utility Model.
This method checks if the utility model exists in the backend and if the code is a string with s3://. If not, it parses the code and updates the description and inputs and does the validation. If yes, it just does the validation on the description and inputs.
to_dict
def to_dict()
Convert the utility model to a dictionary representation.
This method creates a dictionary containing all the essential information about the utility model, suitable for API requests or serialization.
Returns:
dict- A dictionary containing:- name (str): The model's name
- description (str): The model's description
- inputs (List[dict]): List of input parameters as dictionaries
- code (Union[str, Callable]): The model's implementation code
- function (str): The function type as a string value
- outputDescription (str): Examples of expected outputs
- status (str): Current status as a string value
update
def update()
Update the Utility Model.
This method validates the utility model and updates it in the backend.
Raises:
Exception- If the update fails.
save
def save()
Save the Utility Model.
This method updates the utility model in the backend.
delete
def delete()
Delete the Utility Model.
This method deletes the utility model from the backend.
__repr__
def __repr__()
Return a string representation of the UtilityModel instance.
Returns:
str- A string in the format "UtilityModel: <name> by <supplier> (id=<id>)". If supplier is a dictionary, uses supplier['name'], otherwise uses supplier directly.