# Copyright 1999-2026 Alibaba Group Holding Ltd.
#
# 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.
from typing import Any, Dict, List, Optional, Union
from maxframe import opcodes
from maxframe.learn.contrib.llm.core import (
TASK_SENTENCE_EMBEDDING,
LLMTextEmbeddingOp,
LLMTextGenOperator,
)
from maxframe.learn.contrib.llm.deploy.config import ModelDeploymentConfig
from maxframe.learn.contrib.llm.text import TextEmbeddingModel, TextGenLLM
from maxframe.serialization.serializables import BoolField, ReferenceField, StringField
class ManagedLLMTextGenOp(LLMTextGenOperator):
_op_type_ = opcodes.MANAGED_TEXT_MODAL_GENERATION
_legacy_name = "ManagedLLMTextGenOperator"
inference_framework: str = StringField("inference_framework", default=None)
simple_output: bool = BoolField("simple_output", default=False)
[docs]
class ManagedTextGenLLM(TextGenLLM):
"""
Managed text LLM by MaxFrame.
"""
_legacy_name = "ManagedTextLLM" # since v2.3.0
deploy_config: ModelDeploymentConfig = ReferenceField(
"deploy_config", reference_type=ModelDeploymentConfig, default=None
)
[docs]
def __init__(
self, name: str, deploy_config: Optional[ModelDeploymentConfig] = None
):
"""
Initialize a managed text LLM.
Parameters
----------
name : str
The managed text LLM name to use.
deploy_config : ModelDeploymentConfig
The model deployment config to use.
"""
if deploy_config:
deploy_config.model_name = name
deploy_config.check_validity()
super().__init__(name=name, deploy_config=deploy_config)
def generate(
self,
data,
prompt_template: Union[str, List[Dict[str, Any]]],
simple_output: bool = False,
params: Optional[Dict[str, Any]] = None,
**kw
):
return ManagedLLMTextGenOp(
model=self,
prompt_template=prompt_template,
simple_output=simple_output,
params=params,
**kw,
)(data)
# since v2.3.0, text llm has more types
ManagedTextLLM = ManagedTextGenLLM # for old client compatibility
class ManagedLLMTextEmbeddingOp(LLMTextEmbeddingOp):
_op_type_ = opcodes.LLM_TEXT_EMBEDDING_TASK
inference_framework: str = StringField("inference_framework", default=None)
class ManagedTextEmbeddingModel(TextEmbeddingModel):
"""
Managed text embedder by MaxFrame.
"""
deploy_config: ModelDeploymentConfig = ReferenceField(
"deploy_config", reference_type=ModelDeploymentConfig, default=None
)
def __init__(
self, name: str, deploy_config: Optional[ModelDeploymentConfig] = None
):
"""
Initialize a managed text embedder.
Parameters
----------
name : str
The managed text embedder name to use.
deploy_config : ModelDeploymentConfig, optional
The model deployment config to use.
"""
if deploy_config:
deploy_config.model_name = name
deploy_config.check_validity()
super().__init__(name=name, deploy_config=deploy_config)
def embed(
self,
series,
dimensions: Optional[int] = None,
encoding_format: Optional[str] = None,
simple_output: bool = False,
params: Optional[Dict[str, Any]] = None,
**kw
):
return ManagedLLMTextEmbeddingOp(
model=self,
dimensions=dimensions,
encoding_format=encoding_format,
simple_output=simple_output,
params=params,
task=TASK_SENTENCE_EMBEDDING,
**kw,
)(series)
ManagedLLMTextGenOperator = ManagedLLMTextGenOp