aixplain.v1.factories.model_factory
Model Factory Class.
__author__
Copyright 2022 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: Duraikrishna Selvaraju, Thiago Castro Ferreira, Shreyas Sharma and Lucas Pavanelli Date: September 1st 2022 Description: Model Factory Class
ModelFactory Objects
class ModelFactory(ModelGetterMixin, ModelListMixin)
Factory class for creating, managing, and exploring models.
This class provides functionality for creating various types of models, managing model repositories, and interacting with the aiXplain platform's model-related features.
Attributes:
backend_urlstr - Base URL for the aiXplain backend API.
PYTHON_SANDBOX_ID
Python sandbox integration ID
create_utility_model
@classmethod
def create_utility_model(cls,
name: Optional[Text] = None,
code: Union[Text, Callable] = None,
inputs: List[UtilityModelInput] = [],
description: Optional[Text] = None,
output_examples: Text = "",
api_key: Optional[Text] = None,
**kwargs) -> UtilityModel
Create a new utility model for custom functionality.
Deprecated: This method is deprecated. Please use create_script_connection_tool instead.
This method creates a utility model that can execute custom code or functions with specified inputs and outputs.
Arguments:
nameOptional[Text] - Name of the utility model.codeUnion[Text, Callable] - Python code as string or callable function implementing the model's functionality.inputsList[UtilityModelInput], optional - List of input specifications. Defaults to empty list.descriptionOptional[Text], optional - Description of what the model does. Defaults to None.output_examplesText, optional - Examples of expected outputs. Defaults to empty string.api_keyOptional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.**kwargs- Additional keyword arguments.
Returns:
UtilityModel- Created and registered utility model instance.
Raises:
Exception- If model creation fails or validation fails.
create_script_connection_tool
@classmethod
def create_script_connection_tool(cls,
name: Optional[Text] = None,
code: Union[Text, Callable] = None,
description: Optional[Text] = None,
api_key: Optional[Text] = None,
**kwargs) -> ConnectionTool
Create a new script connection tool for custom functionality.
This method creates a connection tool that can execute custom code or functions with specified inputs and outputs. It uses the Python sandbox integration via ToolFactory.create as the underlying implementation.
Arguments:
nameOptional[Text] - Name of the connection tool.codeUnion[Text, Callable] - Python code as string or callable function implementing the connection tool's functionality.descriptionOptional[Text], optional - Description of what the connection tool does. Defaults to None.api_keyOptional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.**kwargs- Additional keyword arguments.
Returns:
ConnectionTool- Created and registered connection tool instance.
Raises:
Exception- If model creation fails or validation fails.
list_host_machines
@classmethod
def list_host_machines(cls,
api_key: Optional[Text] = None,
**kwargs) -> List[Dict]
Lists available hosting machines for model.
Arguments:
api_keyText, optional - Team API key. Defaults to None.**kwargs- Additional keyword arguments.
Returns:
List[Dict]- List of dictionaries containing information about each hosting machine.
list_gpus
@classmethod
def list_gpus(cls,
api_key: Optional[Text] = None,
**kwargs) -> List[List[Text]]
List GPU names on which you can host your language model.
Arguments:
api_keyText, optional - Team API key. Defaults to None.**kwargs- Additional keyword arguments.
Returns:
List[List[Text]]- List of all available GPUs and their prices.
list_functions
@classmethod
def list_functions(cls,
verbose: Optional[bool] = False,
api_key: Optional[Text] = None,
**kwargs) -> List[Dict]
Lists supported model functions on platform.
Arguments:
verboseBoolean, optional - Set to True if a detailed response is desired; is otherwise False by default.api_keyText, optional - Team API key. Defaults to None.**kwargs- Additional keyword arguments.
Returns:
List[Dict]- List of dictionaries containing information about each supported function.
create_asset_repo
@classmethod
def create_asset_repo(cls,
name: Text,
description: Text,
function: Text,
source_language: Text,
input_modality: Text,
output_modality: Text,
documentation_url: Optional[Text] = "",
api_key: Optional[Text] = None,
**kwargs) -> Dict
Create a new model repository in the platform.
This method creates and registers a new model repository, setting up the necessary infrastructure for model deployment.
Arguments:
nameText - Name of the model.descriptionText - Description of the model's functionality.functionText - Function name from list_functions() defining model's task.source_languageText - Language code in ISO 639-1 (2-char) or 639-3 (3-char) format.input_modalityText - Type of input the model accepts (e.g., text, audio).output_modalityText - Type of output the model produces (e.g., text, audio).documentation_urlOptional[Text], optional - URL to model documentation. Defaults to empty string.api_keyOptional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.**kwargs- Additional keyword arguments.
Returns:
Dict- Repository creation response containing model ID and other details.
Raises:
Exception- If function name is invalid.AssertionError- If response status code is not 201.
asset_repo_login
@classmethod
def asset_repo_login(cls, api_key: Optional[Text] = None, **kwargs) -> Dict
Return login credentials for the image repository.
Returns credentials for the image repository that corresponds with the given API_KEY.
Arguments:
api_keyText, optional - Team API key. Defaults to None.**kwargs- Additional keyword arguments.
Returns:
Dict- Backend response
onboard_model
@classmethod
def onboard_model(cls,
model_id: Text,
image_tag: Text,
image_hash: Text,
host_machine: Optional[Text] = "",
api_key: Optional[Text] = None,
**kwargs) -> Dict
Onboard a model after its image has been pushed to ECR.
Arguments:
model_idText - Model ID obtained from CREATE_ASSET_REPO.image_tagText - Image tag to be onboarded.image_hashText - Image digest.host_machineText, optional - Machine on which to host model.api_keyText, optional - Team API key. Defaults to None.**kwargs- Additional keyword arguments.
Returns:
Dict- Backend response
create_rlm
@classmethod
def create_rlm(cls,
orchestrator_model_id: Text,
worker_model_id: Text,
name: Text = "RLM",
description:
Text = "Recursive Language Model for long-context analysis.",
max_iterations: int = 10,
api_key: Optional[Text] = None) -> RLM
Create an RLM (Recursive Language Model) instance for long-context analysis.
RLM overcomes LLM context window limits by giving a powerful orchestrator
model a Python REPL environment pre-loaded with the user's context. The
orchestrator writes code to chunk and explore the data, delegating
per-chunk analysis to a lighter worker model via llm_query() calls.
Arguments:
orchestrator_model_idText - aiXplain model ID of the root LLM that plans and writes REPL code. Use a powerful, reasoning-capable model.worker_model_idText - aiXplain model ID of the sub-LLM called inside the REPL for per-chunk analysis. A fast, cost-efficient model is recommended as it may be invoked many times per run.nameText, optional - Display name for the RLM instance. Defaults to"RLM".descriptionText, optional - Description of this RLM instance. Defaults to a generic string.max_iterationsint, optional - Maximum orchestrator loop iterations before a forced final answer is requested. Defaults to 10.api_keyOptional[Text], optional - API key for model lookups. Defaults toconfig.TEAM_API_KEY.
Returns:
RLM- A configured RLM instance ready to callrun().
Raises:
Exception- If either model ID cannot be fetched from the platform.
Example:
from aixplain.factories import ModelFactory
rlm = ModelFactory.create_rlm(
orchestrator_model_id="<powerful-model-id>",
worker_model_id="<fast-cheap-model-id>",
max_iterations=10,
)
response = rlm.run(data={
"context": very_long_document,
"query": "What are the key findings?",
})
print(response.data)
print(f"Done in {response['iterations_used']} iterations.")
deploy_huggingface_model
@classmethod
def deploy_huggingface_model(cls,
name: Text,
hf_repo_id: Text,
revision: Optional[Text] = "",
hf_token: Optional[Text] = "",
api_key: Optional[Text] = None,
**kwargs) -> Dict
Deploy a model from Hugging Face Hub to the aiXplain platform.
This method handles the deployment of a Hugging Face model, including authentication and configuration setup.
Arguments:
nameText - Display name for the deployed model.hf_repo_idText - Hugging Face repository ID in 'author/model-name' format.revisionOptional[Text], optional - Specific model revision/commit hash. Defaults to empty string (latest version).hf_tokenOptional[Text], optional - Hugging Face access token for private models. Defaults to empty string.api_keyOptional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.**kwargs- Additional keyword arguments.
Returns:
Dict- Deployment response containing model ID and status information.
get_huggingface_model_status
@classmethod
def get_huggingface_model_status(cls,
model_id: Text,
api_key: Optional[Text] = None,
**kwargs)
Check the deployment status of a Hugging Face model.
This method retrieves the current status and details of a deployed Hugging Face model.
Arguments:
model_idText - Model ID returned by deploy_huggingface_model.api_keyOptional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.**kwargs- Additional keyword arguments.
Returns:
Dict- Status response containing:- status: Current deployment status
- name: Model name
- id: Model ID
- pricing: Pricing information