mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Fix CI failures: restore missing error classes and resolve type issues
Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -1,27 +1,27 @@
|
|||||||
import os
|
import os
|
||||||
from typing import Any, Dict, Optional, cast, Protocol, TypeVar, Sequence
|
from typing import Any, Dict, Optional, cast, Protocol, Sequence, TYPE_CHECKING, TypeVar, List, Union
|
||||||
|
|
||||||
from crewai.utilities.errors import ChromaDBRequiredError
|
from crewai.utilities.errors import ChromaDBRequiredError
|
||||||
|
|
||||||
T = TypeVar('T')
|
if TYPE_CHECKING:
|
||||||
|
from numpy import ndarray
|
||||||
|
from numpy import dtype, floating, signedinteger, unsignedinteger
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from chromadb import Documents, EmbeddingFunction as ChromaEmbeddingFunction, Embeddings
|
from chromadb import Documents, EmbeddingFunction, Embeddings
|
||||||
from chromadb.api.types import validate_embedding_function
|
from chromadb.api.types import validate_embedding_function
|
||||||
HAS_CHROMADB = True
|
HAS_CHROMADB = True
|
||||||
|
|
||||||
EmbeddingFunction = ChromaEmbeddingFunction
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_CHROMADB = False
|
HAS_CHROMADB = False
|
||||||
|
|
||||||
class EmbeddingFunction(Protocol[T]):
|
Documents = List[str] # type: ignore
|
||||||
|
Embeddings = List[List[float]] # type: ignore
|
||||||
|
|
||||||
|
class EmbeddingFunction(Protocol): # type: ignore
|
||||||
"""Protocol for embedding functions when ChromaDB is not available."""
|
"""Protocol for embedding functions when ChromaDB is not available."""
|
||||||
def __call__(self, input: Sequence[str]) -> Sequence[Sequence[float]]: ...
|
def __call__(self, input: List[str]) -> List[List[float]]: ...
|
||||||
|
|
||||||
Documents = Any
|
def validate_embedding_function(func: Any) -> None: # type: ignore
|
||||||
Embeddings = Any
|
|
||||||
|
|
||||||
def validate_embedding_function(func: Any) -> None:
|
|
||||||
"""Stub for validate_embedding_function when ChromaDB is not available."""
|
"""Stub for validate_embedding_function when ChromaDB is not available."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -266,7 +266,7 @@ class EmbeddingConfigurator:
|
|||||||
"IBM Watson dependencies are not installed. Please install them to use Watson embedding."
|
"IBM Watson dependencies are not installed. Please install them to use Watson embedding."
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
class WatsonEmbeddingFunction(EmbeddingFunction):
|
class WatsonEmbeddingFunction:
|
||||||
def __call__(self, input: Documents) -> Embeddings:
|
def __call__(self, input: Documents) -> Embeddings:
|
||||||
if isinstance(input, str):
|
if isinstance(input, str):
|
||||||
input = [input]
|
input = [input]
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
"""Custom error classes for CrewAI."""
|
"""Custom error classes for CrewAI."""
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
class ChromaDBRequiredError(ImportError):
|
class ChromaDBRequiredError(ImportError):
|
||||||
"""Error raised when ChromaDB is required but not installed."""
|
"""Error raised when ChromaDB is required but not installed."""
|
||||||
|
|
||||||
@@ -14,3 +17,46 @@ class ChromaDBRequiredError(ImportError):
|
|||||||
"Please install it with 'pip install crewai[storage]'"
|
"Please install it with 'pip install crewai[storage]'"
|
||||||
)
|
)
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseOperationError(Exception):
|
||||||
|
"""Base exception class for database operation errors."""
|
||||||
|
|
||||||
|
def __init__(self, message: str, original_error: Optional[Exception] = None):
|
||||||
|
"""Initialize the database operation error.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message: The error message to display
|
||||||
|
original_error: The original exception that caused this error, if any
|
||||||
|
"""
|
||||||
|
super().__init__(message)
|
||||||
|
self.original_error = original_error
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseError:
|
||||||
|
"""Standardized error message templates for database operations."""
|
||||||
|
|
||||||
|
INIT_ERROR: str = "Database initialization error: {}"
|
||||||
|
SAVE_ERROR: str = "Error saving task outputs: {}"
|
||||||
|
UPDATE_ERROR: str = "Error updating task outputs: {}"
|
||||||
|
LOAD_ERROR: str = "Error loading task outputs: {}"
|
||||||
|
DELETE_ERROR: str = "Error deleting task outputs: {}"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def format_error(cls, template: str, error: Exception) -> str:
|
||||||
|
"""Format an error message with the given template and error.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
template: The error message template to use
|
||||||
|
error: The exception to format into the template
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The formatted error message
|
||||||
|
"""
|
||||||
|
return template.format(str(error))
|
||||||
|
|
||||||
|
|
||||||
|
class AgentRepositoryError(Exception):
|
||||||
|
"""Exception raised when an agent repository is not found."""
|
||||||
|
|
||||||
|
...
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import importlib
|
|||||||
import sys
|
import sys
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from crewai.utilities.errors import ChromaDBRequiredError
|
||||||
|
|
||||||
|
|
||||||
def test_import_without_chromadb():
|
def test_import_without_chromadb():
|
||||||
"""Test that crewai can be imported without chromadb."""
|
"""Test that crewai can be imported without chromadb."""
|
||||||
@@ -31,7 +33,7 @@ def test_memory_storage_without_chromadb():
|
|||||||
|
|
||||||
assert not HAS_CHROMADB
|
assert not HAS_CHROMADB
|
||||||
|
|
||||||
with pytest.raises(ImportError) as excinfo:
|
with pytest.raises(ChromaDBRequiredError) as excinfo:
|
||||||
storage = RAGStorage()
|
storage = RAGStorage()
|
||||||
storage._initialize_app()
|
storage._initialize_app()
|
||||||
|
|
||||||
@@ -48,7 +50,7 @@ def test_knowledge_storage_without_chromadb():
|
|||||||
|
|
||||||
assert not HAS_CHROMADB
|
assert not HAS_CHROMADB
|
||||||
|
|
||||||
with pytest.raises(ImportError) as excinfo:
|
with pytest.raises(ChromaDBRequiredError) as excinfo:
|
||||||
storage = KnowledgeStorage()
|
storage = KnowledgeStorage()
|
||||||
storage.initialize_knowledge_storage()
|
storage.initialize_knowledge_storage()
|
||||||
|
|
||||||
@@ -65,7 +67,7 @@ def test_embedding_configurator_without_chromadb():
|
|||||||
|
|
||||||
assert not HAS_CHROMADB
|
assert not HAS_CHROMADB
|
||||||
|
|
||||||
with pytest.raises(ImportError) as excinfo:
|
with pytest.raises(ChromaDBRequiredError) as excinfo:
|
||||||
configurator = EmbeddingConfigurator()
|
configurator = EmbeddingConfigurator()
|
||||||
configurator.configure_embedder()
|
configurator.configure_embedder()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user