| 591 | retry=retry_if_exception_type((RateLimitError, APIConnectionError, Timeout)), |
| 592 | ) |
| 593 | async def siliconcloud_embedding( |
| 594 | texts: list[str], |
| 595 | model: str = "netease-youdao/bce-embedding-base_v1", |
| 596 | base_url: str = "https://api.siliconflow.cn/v1/embeddings", |
| 597 | max_token_size: int = 512, |
| 598 | api_key: str = None, |
| 599 | ) -> np.ndarray: |
| 600 | if api_key and not api_key.startswith("Bearer "): |
| 601 | api_key = "Bearer " + api_key |
| 602 | |
| 603 | headers = {"Authorization": api_key, "Content-Type": "application/json"} |
| 604 | |
| 605 | truncate_texts = [text[0:max_token_size] for text in texts] |
| 606 | |
| 607 | payload = {"model": model, "input": truncate_texts, "encoding_format": "base64"} |
| 608 | |
| 609 | base64_strings = [] |
| 610 | async with aiohttp.ClientSession() as session: |
| 611 | async with session.post(base_url, headers=headers, json=payload) as response: |
| 612 | content = await response.json() |
| 613 | if "code" in content: |
| 614 | raise ValueError(content) |
| 615 | base64_strings = [item["embedding"] for item in content["data"]] |
| 616 | |
| 617 | embeddings = [] |
| 618 | for string in base64_strings: |
| 619 | decode_bytes = base64.b64decode(string) |
| 620 | n = len(decode_bytes) // 4 |
| 621 | float_array = struct.unpack("<" + "f" * n, decode_bytes) |
| 622 | embeddings.append(float_array) |
| 623 | return np.array(embeddings) |
| 624 | |
| 625 | |
| 626 | # @wrap_embedding_func_with_attrs(embedding_dim=1024, max_token_size=8192) |