mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
feat: Add validation and improve documentation for embedder_config
- Add validation for embedder_config in Agent class - Add test cases for invalid embedder configurations - Improve docstrings with examples and error cases Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -117,9 +117,29 @@ class Agent(BaseAgent):
|
||||
)
|
||||
embedder_config: Optional[Dict[str, Any]] = Field(
|
||||
default=None,
|
||||
description="Embedder configuration for the agent.",
|
||||
description="Embedder configuration for the agent. Must include 'provider' and relevant configuration parameters.",
|
||||
)
|
||||
|
||||
@validator("embedder_config")
|
||||
def validate_embedder_config(cls, v):
|
||||
"""Validate embedder configuration.
|
||||
|
||||
Args:
|
||||
v: The embedder configuration to validate.
|
||||
|
||||
Returns:
|
||||
The validated embedder configuration.
|
||||
|
||||
Raises:
|
||||
ValueError: If the embedder configuration is invalid.
|
||||
"""
|
||||
if v is not None:
|
||||
if not isinstance(v, dict) or "provider" not in v:
|
||||
raise ValueError("embedder_config must contain 'provider' key")
|
||||
if "config" not in v:
|
||||
raise ValueError("embedder_config must contain 'config' key")
|
||||
return v
|
||||
|
||||
@model_validator(mode="after")
|
||||
def post_init_setup(self):
|
||||
self._set_knowledge()
|
||||
|
||||
@@ -190,8 +190,18 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
||||
"""Set the embedding configuration for the knowledge storage.
|
||||
|
||||
Args:
|
||||
embedder_config (Optional[Dict[str, Any]]): Configuration dictionary for the embedder.
|
||||
If None or empty, defaults to the default embedding function.
|
||||
embedder_config: Must include 'provider' and relevant configuration parameters.
|
||||
For example:
|
||||
{
|
||||
'provider': 'openai',
|
||||
'config': {
|
||||
'api_key': 'your-key',
|
||||
'model': 'text-embedding-3-small'
|
||||
}
|
||||
}
|
||||
|
||||
Raises:
|
||||
ValueError: If no configuration is provided or if the configuration is invalid.
|
||||
"""
|
||||
self.embedder = (
|
||||
EmbeddingConfigurator().configure_embedder(embedder_config)
|
||||
|
||||
@@ -36,6 +36,26 @@ def mock_vector_db():
|
||||
instance.reset.return_value = None
|
||||
yield instance
|
||||
|
||||
def test_agent_invalid_embedder_config():
|
||||
"""Test that an invalid embedder configuration raises a ValueError."""
|
||||
with pytest.raises(ValueError, match="embedder_config must contain 'provider' key"):
|
||||
Agent(
|
||||
role="test role",
|
||||
goal="test goal",
|
||||
backstory="test backstory",
|
||||
knowledge_sources=[StringKnowledgeSource(content="test content")],
|
||||
embedder_config={"invalid": "config"}
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="embedder_config must contain 'config' key"):
|
||||
Agent(
|
||||
role="test role",
|
||||
goal="test goal",
|
||||
backstory="test backstory",
|
||||
knowledge_sources=[StringKnowledgeSource(content="test content")],
|
||||
embedder_config={"provider": "custom"}
|
||||
)
|
||||
|
||||
def test_agent_knowledge_with_custom_embedder(mock_vector_db):
|
||||
agent = Agent(
|
||||
role="test role",
|
||||
|
||||
Reference in New Issue
Block a user