siliconcloud.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import sys
  2. if sys.version_info < (3, 9):
  3. pass
  4. else:
  5. pass
  6. import pipmaster as pm # Pipmaster for dynamic library install
  7. # install specific modules
  8. if not pm.is_installed("lmdeploy"):
  9. pm.install("lmdeploy")
  10. from openai import (
  11. APIConnectionError,
  12. RateLimitError,
  13. APITimeoutError,
  14. )
  15. from tenacity import (
  16. retry,
  17. stop_after_attempt,
  18. wait_exponential,
  19. retry_if_exception_type,
  20. )
  21. import numpy as np
  22. import aiohttp
  23. import base64
  24. import struct
  25. @retry(
  26. stop=stop_after_attempt(3),
  27. wait=wait_exponential(multiplier=1, min=4, max=60),
  28. retry=retry_if_exception_type(
  29. (RateLimitError, APIConnectionError, APITimeoutError)
  30. ),
  31. )
  32. async def siliconcloud_embedding(
  33. texts: list[str],
  34. model: str = "netease-youdao/bce-embedding-base_v1",
  35. base_url: str = "https://api.siliconflow.cn/v1/embeddings",
  36. max_token_size: int = 8192,
  37. api_key: str = None,
  38. ) -> np.ndarray:
  39. if api_key and not api_key.startswith("Bearer "):
  40. api_key = "Bearer " + api_key
  41. headers = {"Authorization": api_key, "Content-Type": "application/json"}
  42. truncate_texts = [text[0:max_token_size] for text in texts]
  43. payload = {"model": model, "input": truncate_texts, "encoding_format": "base64"}
  44. base64_strings = []
  45. async with aiohttp.ClientSession() as session:
  46. async with session.post(base_url, headers=headers, json=payload) as response:
  47. content = await response.json()
  48. if "code" in content:
  49. raise ValueError(content)
  50. base64_strings = [item["embedding"] for item in content["data"]]
  51. embeddings = []
  52. for string in base64_strings:
  53. decode_bytes = base64.b64decode(string)
  54. n = len(decode_bytes) // 4
  55. float_array = struct.unpack("<" + "f" * n, decode_bytes)
  56. embeddings.append(float_array)
  57. return np.array(embeddings)