Add extensive test suites for API routes and utilities: - Implement test_search_routes.py (406 lines) for search endpoint validation - Implement test_upload_routes.py (724 lines) for document upload workflows - Implement test_s3_client.py (618 lines) for S3 storage operations - Implement test_citation_utils.py (352 lines) for citation extraction - Implement test_chunking.py (216 lines) for text chunking validation Add S3 storage client implementation: - Create lightrag/storage/s3_client.py with S3 operations - Add storage module initialization with exports - Integrate S3 client with document upload handling Enhance API routes and core functionality: - Add search_routes.py with full-text and graph search endpoints - Add upload_routes.py with multipart document upload support - Update operate.py with bulk operations and health checks - Enhance postgres_impl.py with bulk upsert and parameterized queries - Update lightrag_server.py to register new API routes - Improve utils.py with citation and formatting utilities Update dependencies and configuration: - Add S3 and test dependencies to pyproject.toml - Update docker-compose.test.yml for testing environment - Sync uv.lock with new dependencies Apply code quality improvements across all modified files: - Add type hints to function signatures - Update imports and router initialization - Fix logging and error handling
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
import os
|
|
|
|
import pipmaster as pm # Pipmaster for dynamic library install
|
|
|
|
# install specific modules
|
|
if not pm.is_installed('openai'):
|
|
pm.install('openai')
|
|
|
|
from typing import Literal
|
|
|
|
import numpy as np
|
|
from openai import (
|
|
APIConnectionError,
|
|
APITimeoutError,
|
|
AsyncOpenAI,
|
|
RateLimitError,
|
|
)
|
|
from tenacity import (
|
|
retry,
|
|
retry_if_exception_type,
|
|
stop_after_attempt,
|
|
wait_exponential,
|
|
)
|
|
|
|
from lightrag.utils import (
|
|
wrap_embedding_func_with_attrs,
|
|
)
|
|
|
|
|
|
@wrap_embedding_func_with_attrs(embedding_dim=2048, max_token_size=8192)
|
|
@retry(
|
|
stop=stop_after_attempt(3),
|
|
wait=wait_exponential(multiplier=1, min=4, max=60),
|
|
retry=retry_if_exception_type((RateLimitError, APIConnectionError, APITimeoutError)),
|
|
)
|
|
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 = None,
|
|
input_type: str = 'passage', # query for retrieval, passage for embedding
|
|
trunc: str = 'NONE', # NONE or START or END
|
|
encode: Literal['float', 'base64'] = 'float', # float or base64
|
|
) -> np.ndarray:
|
|
if api_key:
|
|
os.environ['OPENAI_API_KEY'] = api_key
|
|
|
|
openai_async_client = AsyncOpenAI() if base_url is None else AsyncOpenAI(base_url=base_url)
|
|
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])
|