mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-24 15:48:23 +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(
|
embedder_config: Optional[Dict[str, Any]] = Field(
|
||||||
default=None,
|
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")
|
@model_validator(mode="after")
|
||||||
def post_init_setup(self):
|
def post_init_setup(self):
|
||||||
self._set_knowledge()
|
self._set_knowledge()
|
||||||
|
|||||||
@@ -190,8 +190,18 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
|||||||
"""Set the embedding configuration for the knowledge storage.
|
"""Set the embedding configuration for the knowledge storage.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
embedder_config (Optional[Dict[str, Any]]): Configuration dictionary for the embedder.
|
embedder_config: Must include 'provider' and relevant configuration parameters.
|
||||||
If None or empty, defaults to the default embedding function.
|
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 = (
|
self.embedder = (
|
||||||
EmbeddingConfigurator().configure_embedder(embedder_config)
|
EmbeddingConfigurator().configure_embedder(embedder_config)
|
||||||
|
|||||||
@@ -36,6 +36,26 @@ def mock_vector_db():
|
|||||||
instance.reset.return_value = None
|
instance.reset.return_value = None
|
||||||
yield instance
|
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):
|
def test_agent_knowledge_with_custom_embedder(mock_vector_db):
|
||||||
agent = Agent(
|
agent = Agent(
|
||||||
role="test role",
|
role="test role",
|
||||||
|
|||||||
Reference in New Issue
Block a user