Call the Dashscope API.
(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED)
| 389 | max_time=5, |
| 390 | ) |
| 391 | def call(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED): |
| 392 | """Call the Dashscope API.""" |
| 393 | if model_type == ModelType.LLM: |
| 394 | if not api_kwargs.get("stream", False): |
| 395 | # For non-streaming, enable_thinking must be false. |
| 396 | # Pass it via extra_body to avoid TypeError from openai client validation. |
| 397 | extra_body = api_kwargs.get("extra_body", {}) |
| 398 | extra_body["enable_thinking"] = False |
| 399 | api_kwargs["extra_body"] = extra_body |
| 400 | |
| 401 | completion = self.sync_client.chat.completions.create(**api_kwargs) |
| 402 | |
| 403 | if api_kwargs.get("stream", False): |
| 404 | return handle_streaming_response(completion) |
| 405 | else: |
| 406 | return self.parse_chat_completion(completion) |
| 407 | elif model_type == ModelType.EMBEDDER: |
| 408 | # Extract input texts from api_kwargs |
| 409 | texts = api_kwargs.get("input", []) |
| 410 | |
| 411 | if not texts: |
| 412 | log.warning("😭 No input texts provided") |
| 413 | return EmbedderOutput(data=[], error="No input texts provided", raw_response=None) |
| 414 | |
| 415 | # Ensure texts is a list |
| 416 | if isinstance(texts, str): |
| 417 | texts = [texts] |
| 418 | |
| 419 | # Filter out empty or None texts - following HuggingFace client pattern |
| 420 | valid_texts = [] |
| 421 | valid_indices = [] |
| 422 | for i, text in enumerate(texts): |
| 423 | if text and isinstance(text, str) and text.strip(): |
| 424 | valid_texts.append(text) |
| 425 | valid_indices.append(i) |
| 426 | else: |
| 427 | log.warning(f"🔍 Skipping empty or invalid text at index {i}: type={type(text)}, length={len(text) if hasattr(text, '__len__') else 'N/A'}, repr={repr(text)[:100]}") |
| 428 | |
| 429 | if not valid_texts: |
| 430 | log.error("😭 No valid texts found after filtering") |
| 431 | return EmbedderOutput(data=[], error="No valid texts found after filtering", raw_response=None) |
| 432 | |
| 433 | if len(valid_texts) != len(texts): |
| 434 | filtered_count = len(texts) - len(valid_texts) |
| 435 | log.warning(f"🔍 Filtered out {filtered_count} empty/invalid texts out of {len(texts)} total texts") |
| 436 | |
| 437 | # Create modified api_kwargs with only valid texts |
| 438 | filtered_api_kwargs = api_kwargs.copy() |
| 439 | filtered_api_kwargs["input"] = valid_texts |
| 440 | |
| 441 | log.info(f"🔍 DashScope embedding API call with {len(valid_texts)} valid texts out of {len(texts)} total") |
| 442 | |
| 443 | try: |
| 444 | response = self.sync_client.embeddings.create(**filtered_api_kwargs) |
| 445 | log.info(f"🔍 DashScope API call successful, response type: {type(response)}") |
| 446 | result = self.parse_embedding_response(response) |
| 447 | |
| 448 | # If we filtered texts, we need to create embeddings for the original indices |
no test coverage detected