Files

1 line
2.2 KiB
JSON

{"repo": "ai-researchers/kgcare", "files": {"lightRAG/lightrag/llm/deprecated/siliconcloud.py": {"cd7dd687003ee433c8ee544794062704e1e42b02": {"t": 1785674140.443233, "n": 1894, "h": "64b068dfb2bce3e8", "c": "import sys\n\nif sys.version_info < (3, 9):\n pass\nelse:\n pass\nimport pipmaster as pm # Pipmaster for dynamic library install\n\n# install specific modules\nif not pm.is_installed(\"lmdeploy\"):\n pm.install(\"lmdeploy\")\n\nfrom openai import (\n APIConnectionError,\n RateLimitError,\n APITimeoutError,\n)\nfrom tenacity import (\n retry,\n stop_after_attempt,\n wait_exponential,\n retry_if_exception_type,\n)\n\n\nimport numpy as np\nimport aiohttp\nimport base64\nimport struct\n\n\n@retry(\n stop=stop_after_attempt(3),\n wait=wait_exponential(multiplier=1, min=4, max=60),\n retry=retry_if_exception_type(\n (RateLimitError, APIConnectionError, APITimeoutError)\n ),\n)\nasync def siliconcloud_embedding(\n texts: list[str],\n model: str = \"netease-youdao/bce-embedding-base_v1\",\n base_url: str = \"https://api.siliconflow.cn/v1/embeddings\",\n max_token_size: int = 8192,\n api_key: str = None,\n) -> np.ndarray:\n if api_key and not api_key.startswith(\"Bearer \"):\n api_key = \"Bearer \" + api_key\n\n headers = {\"Authorization\": api_key, \"Content-Type\": \"application/json\"}\n\n truncate_texts = [text[0:max_token_size] for text in texts]\n\n payload = {\"model\": model, \"input\": truncate_texts, \"encoding_format\": \"base64\"}\n\n base64_strings = []\n async with aiohttp.ClientSession() as session:\n async with session.post(base_url, headers=headers, json=payload) as response:\n content = await response.json()\n if \"code\" in content:\n raise ValueError(content)\n base64_strings = [item[\"embedding\"] for item in content[\"data\"]]\n\n embeddings = []\n for string in base64_strings:\n decode_bytes = base64.b64decode(string)\n n = len(decode_bytes) // 4\n float_array = struct.unpack(\"<\" + \"f\" * n, decode_bytes)\n embeddings.append(float_array)\n return np.array(embeddings)\n"}}}}