fix: Config can be instantiated with without validation errors

This commit is contained in:
Young Han 2025-10-31 11:42:23 -07:00
parent 8f0f4a26a3
commit cac2912ce6
4 changed files with 16 additions and 5 deletions

View file

@ -243,7 +243,7 @@ For general MongoDB documentation, visit [MongoDB Documentation](https://docs.mo
| Field | Type | Required | Default | Description |
|-------|------|----------|---------|-------------|
| `connection_string` | `<class 'str'>` | No | | MongoDB Atlas connection string (e.g., mongodb+srv://user:pass@cluster.mongodb.net/) |
| `connection_string` | `str \| None` | No | | MongoDB Atlas connection string (e.g., mongodb+srv://user:pass@cluster.mongodb.net/) |
| `database_name` | `<class 'str'>` | No | llama_stack | Database name to use for vector collections |
| `index_name` | `<class 'str'>` | No | vector_index | Name of the vector search index |
| `path_field` | `<class 'str'>` | No | embedding | Field name for storing embeddings |

View file

@ -14,6 +14,7 @@ async def get_adapter_impl(config: MongoDBVectorIOConfig, deps: dict[Api, Provid
# Handle the deps resolution - if files API exists, pass it, otherwise None
files_api = deps.get(Api.files)
impl = MongoDBVectorIOAdapter(config, deps[Api.inference], files_api)
models_api = deps.get(Api.models)
impl = MongoDBVectorIOAdapter(config, deps[Api.inference], files_api, models_api)
await impl.initialize()
return impl

View file

@ -20,8 +20,9 @@ class MongoDBVectorIOConfig(BaseModel):
"""
# MongoDB Atlas connection details
connection_string: str = Field(
description="MongoDB Atlas connection string (e.g., mongodb+srv://user:pass@cluster.mongodb.net/)"
connection_string: str | None = Field(
default=None,
description="MongoDB Atlas connection string (e.g., mongodb+srv://user:pass@cluster.mongodb.net/)",
)
database_name: str = Field(default="llama_stack", description="Database name to use for vector collections")

View file

@ -215,7 +215,7 @@ class MongoDBIndex(EmbeddingIndex):
"index": self.config.index_name,
"queryVector": embedding.tolist(),
"path": self.config.path_field,
"numCandidates": k * 10, # Get more candidates for better results
"numCandidates": min(k * 10, 1000), # Cap at 1000 to prevent excessive candidates
"limit": k,
}
},
@ -398,12 +398,14 @@ class MongoDBVectorIOAdapter(OpenAIVectorStoreMixin, VectorIO, VectorStoresProto
config: MongoDBVectorIOConfig,
inference_api,
files_api=None,
models_api=None,
) -> None:
# Handle the case where files_api might be a ProviderSpec that needs resolution
resolved_files_api = files_api
super().__init__(files_api=resolved_files_api, kvstore=None)
self.config = config
self.inference_api = inference_api
self.models_api = models_api
self.client: MongoClient | None = None
self.database: Database | None = None
self.cache: dict[str, VectorStoreWithIndex] = {}
@ -418,6 +420,13 @@ class MongoDBVectorIOAdapter(OpenAIVectorStoreMixin, VectorIO, VectorStoresProto
if self.config.persistence:
self.kvstore = await kvstore_impl(self.config.persistence)
# Validate connection string
if not self.config.connection_string:
raise ValueError(
"MongoDB connection_string is required but not provided. "
"Please set MONGODB_CONNECTION_STRING environment variable or provide it in config."
)
# Connect to MongoDB with optimized settings for RAG
self.client = MongoClient(
self.config.connection_string,