A component wrapper for the Dashscope (Alibaba Cloud) API client. Dashscope provides access to Alibaba Cloud's Qwen and other models through an OpenAI-compatible API. Args: api_key (Optional[str], optional): Dashscope API key. Defaults to None. workspace_id (Optional[st
| 102 | |
| 103 | |
| 104 | class DashscopeClient(ModelClient): |
| 105 | """A component wrapper for the Dashscope (Alibaba Cloud) API client. |
| 106 | |
| 107 | Dashscope provides access to Alibaba Cloud's Qwen and other models through an OpenAI-compatible API. |
| 108 | |
| 109 | Args: |
| 110 | api_key (Optional[str], optional): Dashscope API key. Defaults to None. |
| 111 | workspace_id (Optional[str], optional): Dashscope workspace ID. Defaults to None. |
| 112 | base_url (str): The API base URL. Defaults to "https://dashscope.aliyuncs.com/compatible-mode/v1". |
| 113 | env_api_key_name (str): Environment variable name for the API key. Defaults to "DASHSCOPE_API_KEY". |
| 114 | env_workspace_id_name (str): Environment variable name for the workspace ID. Defaults to "DASHSCOPE_WORKSPACE_ID". |
| 115 | |
| 116 | References: |
| 117 | - Dashscope API Documentation: https://help.aliyun.com/zh/dashscope/ |
| 118 | """ |
| 119 | |
| 120 | def __init__( |
| 121 | self, |
| 122 | api_key: Optional[str] = None, |
| 123 | workspace_id: Optional[str] = None, |
| 124 | chat_completion_parser: Callable[[Completion], Any] = None, |
| 125 | input_type: Literal["text", "messages"] = "text", |
| 126 | base_url: Optional[str] = None, |
| 127 | env_base_url_name: str = "DASHSCOPE_BASE_URL", |
| 128 | env_api_key_name: str = "DASHSCOPE_API_KEY", |
| 129 | env_workspace_id_name: str = "DASHSCOPE_WORKSPACE_ID", |
| 130 | ): |
| 131 | super().__init__() |
| 132 | self._api_key = api_key |
| 133 | self._workspace_id = workspace_id |
| 134 | self._env_api_key_name = env_api_key_name |
| 135 | self._env_workspace_id_name = env_workspace_id_name |
| 136 | self._env_base_url_name = env_base_url_name |
| 137 | self.base_url = base_url or os.getenv(self._env_base_url_name, "https://dashscope.aliyuncs.com/compatible-mode/v1") |
| 138 | self.sync_client = self.init_sync_client() |
| 139 | self.async_client = None |
| 140 | |
| 141 | # Force use of get_first_message_content to ensure string output |
| 142 | self.chat_completion_parser = get_first_message_content |
| 143 | self._input_type = input_type |
| 144 | self._api_kwargs = {} |
| 145 | |
| 146 | def _prepare_client_config(self): |
| 147 | """ |
| 148 | Private helper method to prepare client configuration. |
| 149 | |
| 150 | Returns: |
| 151 | tuple: (api_key, workspace_id, base_url) for client initialization |
| 152 | |
| 153 | Raises: |
| 154 | ValueError: If API key is not provided |
| 155 | """ |
| 156 | api_key = self._api_key or os.getenv(self._env_api_key_name) |
| 157 | workspace_id = self._workspace_id or os.getenv(self._env_workspace_id_name) |
| 158 | |
| 159 | if not api_key: |
| 160 | raise ValueError( |
| 161 | f"Environment variable {self._env_api_key_name} must be set" |
no outgoing calls
no test coverage detected