Source code for maxframe.learn.contrib.llm.models.dashscope

# 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

from maxframe import opcodes
from maxframe.learn.contrib.llm.core import TASK_IMAGE_TEXT_TO_TEXT, LLMTextGenOperator
from maxframe.learn.contrib.llm.multi_modal import (
    LLMMultiModalGenerationOp,
    MultiModalGenLLM,
)
from maxframe.learn.contrib.llm.text import TextGenLLM
from maxframe.serialization.serializables.core import Serializable
from maxframe.serialization.serializables.field import BoolField, StringField


class DashScopeLLMMixin(Serializable):
    __slots__ = ()

    _not_supported_params = {"stream", "incremental_output"}

    def validate_params(self, params: Dict[str, Any]):
        for k in params.keys():
            if k in self._not_supported_params:
                raise ValueError(f"{k} is not supported")


[docs] class DashScopeTextLLM(TextGenLLM, DashScopeLLMMixin): """ DashScope text LLM. """ api_key_resource = StringField("api_key_resource", default=None)
[docs] def __init__(self, name: str, api_key_resource: str): """ Initialize a DashScope text LLM. Parameters ---------- name : str The LLM name to use, check DashScope for `available models <https://help.aliyun.com/zh/model-studio/getting-started/models>`_. api_key_resource : str The MaxCompute resource file name containing the DashScope API key. """ super().__init__(name=name, api_key_resource=api_key_resource)
def generate( self, data, prompt_template: Dict[str, Any], params: Dict[str, Any] = None, ): return DashScopeTextGenerationOp( model=self, prompt_template=prompt_template, params=params, )(data)
[docs] class DashScopeMultiModalLLM(MultiModalGenLLM, DashScopeLLMMixin): """ DashScope multi-modal LLM. """ api_key_resource = StringField("api_key_resource", default=None)
[docs] def __init__(self, name: str, api_key_resource: str): """ Initialize a DashScope multi-modal LLM. Parameters ---------- name : str The LLM name to use, check DashScope for `available models <https://help.aliyun.com/zh/model-studio/getting-started/models>`_. api_key_resource : str The MaxCompute resource file name containing the DashScope API key. """ super().__init__(name=name, api_key_resource=api_key_resource)
def generate( self, data, messages=None, prompt_template: Dict[str, Any] = None, simple_output: bool = False, params: Dict[str, Any] = None, **kw, ): prompt_template = messages if messages is not None else prompt_template if prompt_template is None: raise ValueError("messages or prompt_template is required") return DashScopeMultiModalGenerationOp( model=self, prompt_template=prompt_template, simple_output=simple_output, params=params, task=TASK_IMAGE_TEXT_TO_TEXT, **kw, )(data)
class DashScopeTextGenerationOp(LLMTextGenOperator): _op_type_ = opcodes.DASHSCOPE_TEXT_GENERATION _legacy_name = "DashScopeTextGenerationOperator" simple_output = BoolField("simple_output", default=False) class DashScopeMultiModalGenerationOp(LLMMultiModalGenerationOp): _op_type_ = opcodes.DASHSCOPE_MULTI_MODAL_GENERATION _legacy_name = "DashScopeMultiModalGenerationOperator" simple_output = BoolField("simple_output", default=False) DashScopeTextGenerationOperator = DashScopeTextGenerationOp DashScopeMultiModalGenerationOperator = DashScopeMultiModalGenerationOp