54 lines
2.2 KiB
Python

"""Runtime provider composition from secret-safe application settings."""
from ekp_core.config import Settings
from ekp_domain import IndexProfile
from ekp_embedding import QianfanEmbeddingProvider
from ekp_vector_store import MochowVectorStore, create_mochow_vector_store
BGE_LARGE_ZH_DIMENSIONS = 1024
DEMO_CHUNK_STRATEGY_VERSION = "structure_v1"
class ProviderConfigurationError(ValueError):
"""Raised when a runtime provider lacks required injected configuration."""
def create_index_profile(settings: Settings) -> IndexProfile:
"""Bind the Demo's chunk and embedding versions without vendor SDK types."""
return IndexProfile(
profile_id=settings.index_profile_id,
chunk_strategy_version=DEMO_CHUNK_STRATEGY_VERSION,
embedding_model=settings.embedding_model,
vector_dimension=BGE_LARGE_ZH_DIMENSIONS,
)
def create_embedding_provider(settings: Settings) -> QianfanEmbeddingProvider:
"""Create Qianfan only when its API key is supplied by the runtime."""
if settings.baidu_qianfan_api_key is None:
raise ProviderConfigurationError("BAIDU_QIANFAN_API_KEY is required")
return QianfanEmbeddingProvider(
api_key=settings.baidu_qianfan_api_key.get_secret_value(),
endpoint=settings.embedding_endpoint,
timeout_seconds=settings.embedding_timeout_seconds,
)
def create_vector_store(settings: Settings) -> MochowVectorStore:
"""Create the Mochow adapter for an explicitly configured database/table."""
if settings.baidu_mochow_api_key is None:
raise ProviderConfigurationError("BAIDU_MOCHOW_API_KEY is required")
if not settings.baidu_mochow_endpoint:
raise ProviderConfigurationError("BAIDU_MOCHOW_ENDPOINT is required")
if not settings.knowledge_vdb_db_name:
raise ProviderConfigurationError("KNOWLEDGE_VDB_DB_NAME is required")
return create_mochow_vector_store(
account=settings.baidu_mochow_account,
api_key=settings.baidu_mochow_api_key.get_secret_value(),
endpoint=settings.baidu_mochow_endpoint,
database_name=settings.knowledge_vdb_db_name,
table_name=settings.mochow_table,
batch_size=settings.mochow_batch_size,
timeout_seconds=settings.mochow_timeout_seconds,
)