| 211 | |
| 212 | |
| 213 | class DashScopePromptExpander(PromptExpander): |
| 214 | |
| 215 | def __init__(self, |
| 216 | api_key=None, |
| 217 | model_name=None, |
| 218 | max_image_size=512 * 512, |
| 219 | retry_times=4, |
| 220 | is_vl=False, |
| 221 | **kwargs): |
| 222 | ''' |
| 223 | Args: |
| 224 | api_key: The API key for Dash Scope authentication and access to related services. |
| 225 | model_name: Model name, 'qwen-plus' for extending prompts, 'qwen-vl-max' for extending prompt-images. |
| 226 | max_image_size: The maximum size of the image; unit unspecified (e.g., pixels, KB). Please specify the unit based on actual usage. |
| 227 | retry_times: Number of retry attempts in case of request failure. |
| 228 | is_vl: A flag indicating whether the task involves visual-language processing. |
| 229 | **kwargs: Additional keyword arguments that can be passed to the function or method. |
| 230 | ''' |
| 231 | if model_name is None: |
| 232 | model_name = 'qwen-plus' if not is_vl else 'qwen-vl-max' |
| 233 | super().__init__(model_name, is_vl, **kwargs) |
| 234 | if api_key is not None: |
| 235 | dashscope.api_key = api_key |
| 236 | elif 'DASH_API_KEY' in os.environ and os.environ[ |
| 237 | 'DASH_API_KEY'] is not None: |
| 238 | dashscope.api_key = os.environ['DASH_API_KEY'] |
| 239 | else: |
| 240 | raise ValueError("DASH_API_KEY is not set") |
| 241 | if 'DASH_API_URL' in os.environ and os.environ[ |
| 242 | 'DASH_API_URL'] is not None: |
| 243 | dashscope.base_http_api_url = os.environ['DASH_API_URL'] |
| 244 | else: |
| 245 | dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1' |
| 246 | self.api_key = api_key |
| 247 | |
| 248 | self.max_image_size = max_image_size |
| 249 | self.model = model_name |
| 250 | self.retry_times = retry_times |
| 251 | |
| 252 | def extend(self, prompt, system_prompt, seed=-1, *args, **kwargs): |
| 253 | messages = [{ |
| 254 | 'role': 'system', |
| 255 | 'content': system_prompt |
| 256 | }, { |
| 257 | 'role': 'user', |
| 258 | 'content': prompt |
| 259 | }] |
| 260 | |
| 261 | exception = None |
| 262 | for _ in range(self.retry_times): |
| 263 | try: |
| 264 | response = dashscope.Generation.call( |
| 265 | self.model, |
| 266 | messages=messages, |
| 267 | seed=seed, |
| 268 | result_format='message', # set the result to be "message" format. |
| 269 | ) |
| 270 | assert response.status_code == HTTPStatus.OK, response |