|
| 1 | +from typing import Dict, Any |
| 2 | + |
| 3 | +from dashscope.api_entities.api_request_factory import _build_api_request |
| 4 | +from dashscope.api_entities.dashscope_response import DashScopeAPIResponse |
| 5 | +from dashscope.client.base_api import BaseApi |
| 6 | +from dashscope.common.error import ModelRequired |
| 7 | + |
| 8 | + |
| 9 | +class TingWu(BaseApi): |
| 10 | + """API for TingWu APP. |
| 11 | +
|
| 12 | + """ |
| 13 | + |
| 14 | + task = None |
| 15 | + task_group = None |
| 16 | + function = None |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def call( |
| 20 | + cls, |
| 21 | + model: str, |
| 22 | + user_defined_input: Dict[str, Any], |
| 23 | + parameters: Dict[str, Any] = None, |
| 24 | + api_key: str = None, |
| 25 | + **kwargs |
| 26 | + ) -> DashScopeAPIResponse: |
| 27 | + """Call generation model service. |
| 28 | +
|
| 29 | + Args: |
| 30 | + model (str): The requested model, such as qwen-turbo |
| 31 | + api_key (str, optional): The api api_key, can be None, |
| 32 | + if None, will get by default rule(TODO: api key doc). |
| 33 | + user_defined_input: custom input |
| 34 | + parameters: custom parameters |
| 35 | + **kwargs: |
| 36 | + base_address: base address |
| 37 | + additional parameters for request |
| 38 | +
|
| 39 | + Raises: |
| 40 | + InvalidInput: The history and auto_history are mutually exclusive. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + Union[GenerationResponse, |
| 44 | + Generator[GenerationResponse, None, None]]: If |
| 45 | + stream is True, return Generator, otherwise GenerationResponse. |
| 46 | + """ |
| 47 | + if model is None or not model: |
| 48 | + raise ModelRequired('Model is required!') |
| 49 | + input_config, parameters = cls._build_input_parameters(input_config=user_defined_input, |
| 50 | + params=parameters, |
| 51 | + **kwargs) |
| 52 | + |
| 53 | + request = _build_api_request( |
| 54 | + model=model, |
| 55 | + input=input_config, |
| 56 | + api_key=api_key, |
| 57 | + task_group=TingWu.task_group, |
| 58 | + task=TingWu.task, |
| 59 | + function=TingWu.function, |
| 60 | + is_service=False, |
| 61 | + **parameters) |
| 62 | + response = request.call() |
| 63 | + |
| 64 | + return response |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def _build_input_parameters(cls, |
| 68 | + input_config, |
| 69 | + params: Dict[str, Any] = None, |
| 70 | + **kwargs): |
| 71 | + parameters = {} |
| 72 | + if params is not None: |
| 73 | + parameters = params |
| 74 | + |
| 75 | + input_param = input_config |
| 76 | + |
| 77 | + if kwargs.keys() is not None: |
| 78 | + for key in kwargs.keys(): |
| 79 | + parameters[key] = kwargs[key] |
| 80 | + return input_param, {**parameters, **kwargs} |
0 commit comments