Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /HKUDS_LightRAG /lightrag /llm /nvidia_openai.py
| import sys | |
| if sys.version_info < (3, 9): | |
| pass | |
| else: | |
| pass | |
| import pipmaster as pm # Pipmaster for dynamic library install | |
| # install specific modules | |
| if not pm.is_installed("openai"): | |
| pm.install("openai") | |
| from openai import ( | |
| AsyncOpenAI, | |
| APIConnectionError, | |
| RateLimitError, | |
| APITimeoutError, | |
| ) | |
| from tenacity import ( | |
| retry, | |
| stop_after_attempt, | |
| wait_exponential, | |
| retry_if_exception_type, | |
| ) | |
| from lightrag.utils import ( | |
| wrap_embedding_func_with_attrs, | |
| ) | |
| import numpy as np | |
| async def nvidia_openai_embed( | |
| texts: list[str], | |
| model: str = "nvidia/llama-3.2-nv-embedqa-1b-v1", | |
| # refer to https://build.nvidia.com/nim?filters=usecase%3Ausecase_text_to_embedding | |
| base_url: str = "https://integrate.api.nvidia.com/v1", | |
| api_key: str = None, | |
| input_type: str = "passage", # query for retrieval, passage for embedding | |
| trunc: str = "NONE", # NONE or START or END | |
| encode: str = "float", # float or base64 | |
| ) -> np.ndarray: | |
| client_kwargs = {} | |
| if base_url is not None: | |
| client_kwargs["base_url"] = base_url | |
| if api_key: | |
| client_kwargs["api_key"] = api_key | |
| openai_async_client = AsyncOpenAI(**client_kwargs) | |
| # Hold the client in an async-with so its httpx connection pool is | |
| # released on every exit path (success, error, and each @retry attempt), | |
| # instead of leaking one pool per call until GC. Mirrors ``openai_embed``. | |
| async with openai_async_client: | |
| response = await openai_async_client.embeddings.create( | |
| model=model, | |
| input=texts, | |
| encoding_format=encode, | |
| extra_body={"input_type": input_type, "truncate": trunc}, | |
| ) | |
| return np.array([dp.embedding for dp in response.data]) | |
Xet Storage Details
- Size:
- 2.08 kB
- Xet hash:
- 7b216bb653b48fe6b9a6acba64909984d33bc518604a28cad0284197ed633ba9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.