mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-10 00:28:31 +00:00
Enhance error handling and type annotations for optional chromadb dependency
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -49,9 +49,10 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
|||||||
search efficiency.
|
search efficiency.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
collection = None # Type will be chromadb.Collection when available
|
collection: Optional[Any] = None # Will be chromadb.Collection when available
|
||||||
collection_name: Optional[str] = "knowledge"
|
collection_name: Optional[str] = "knowledge"
|
||||||
app = None # Type will be ClientAPI when available
|
app: Optional[Any] = None # Will be ClientAPI when available
|
||||||
|
embedder: Optional[Any] = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -68,6 +69,13 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
|||||||
filter: Optional[dict] = None,
|
filter: Optional[dict] = None,
|
||||||
score_threshold: float = 0.35,
|
score_threshold: float = 0.35,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
|
if not CHROMADB_AVAILABLE:
|
||||||
|
logging.warning(
|
||||||
|
"ChromaDB is not installed. Search functionality limited. "
|
||||||
|
"Install with 'pip install crewai[chromadb]' to enable full functionality."
|
||||||
|
)
|
||||||
|
return []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with suppress_logging():
|
with suppress_logging():
|
||||||
if self.collection:
|
if self.collection:
|
||||||
@@ -89,8 +97,8 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
|||||||
return results
|
return results
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
except (ImportError, NameError, AttributeError, Exception):
|
except (ImportError, NameError, AttributeError, Exception) as e:
|
||||||
# Return empty results if chromadb is not available or collection is not initialized
|
logging.error(f"Error during knowledge search operation: {str(e)}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def initialize_knowledge_storage(self):
|
def initialize_knowledge_storage(self):
|
||||||
@@ -216,8 +224,15 @@ class KnowledgeStorage(BaseKnowledgeStorage):
|
|||||||
OpenAIEmbeddingFunction,
|
OpenAIEmbeddingFunction,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
logging.warning(
|
||||||
|
"OPENAI_API_KEY not found. Vector operations will be limited."
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
return OpenAIEmbeddingFunction(
|
return OpenAIEmbeddingFunction(
|
||||||
api_key=os.getenv("OPENAI_API_KEY"), model_name="text-embedding-3-small"
|
api_key=api_key, model_name="text-embedding-3-small"
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import logging
|
import logging
|
||||||
|
|||||||
@@ -66,6 +66,15 @@ class RAGStorage(BaseRAGStorage):
|
|||||||
self.embedder_config = configurator.configure_embedder(self.embedder_config)
|
self.embedder_config = configurator.configure_embedder(self.embedder_config)
|
||||||
|
|
||||||
def _initialize_app(self):
|
def _initialize_app(self):
|
||||||
|
if not CHROMADB_AVAILABLE:
|
||||||
|
logging.warning(
|
||||||
|
"ChromaDB is not installed. RAG storage functionality will be limited. "
|
||||||
|
"Install with 'pip install crewai[chromadb]' to enable full functionality."
|
||||||
|
)
|
||||||
|
self.app = None
|
||||||
|
self.collection = None
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import chromadb
|
import chromadb
|
||||||
from chromadb.config import Settings
|
from chromadb.config import Settings
|
||||||
@@ -82,16 +91,13 @@ class RAGStorage(BaseRAGStorage):
|
|||||||
self.collection = self.app.get_collection(
|
self.collection = self.app.get_collection(
|
||||||
name=self.type, embedding_function=self.embedder_config
|
name=self.type, embedding_function=self.embedder_config
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
logging.info(f"Collection not found, creating new one: {str(e)}")
|
||||||
self.collection = self.app.create_collection(
|
self.collection = self.app.create_collection(
|
||||||
name=self.type, embedding_function=self.embedder_config
|
name=self.type, embedding_function=self.embedder_config
|
||||||
)
|
)
|
||||||
except ImportError:
|
except Exception as e:
|
||||||
import logging
|
logging.error(f"Failed to initialize ChromaDB: {str(e)}")
|
||||||
logging.warning(
|
|
||||||
"ChromaDB is not installed. RAG storage functionality will be limited. "
|
|
||||||
"Install with 'pip install crewai[chromadb]' to enable full functionality."
|
|
||||||
)
|
|
||||||
self.app = None
|
self.app = None
|
||||||
self.collection = None
|
self.collection = None
|
||||||
|
|
||||||
@@ -134,6 +140,13 @@ class RAGStorage(BaseRAGStorage):
|
|||||||
filter: Optional[dict] = None,
|
filter: Optional[dict] = None,
|
||||||
score_threshold: float = 0.35,
|
score_threshold: float = 0.35,
|
||||||
) -> List[Any]:
|
) -> List[Any]:
|
||||||
|
if not CHROMADB_AVAILABLE:
|
||||||
|
logging.warning(
|
||||||
|
"ChromaDB is not installed. Search functionality limited. "
|
||||||
|
"Install with 'pip install crewai[chromadb]' to enable full functionality."
|
||||||
|
)
|
||||||
|
return []
|
||||||
|
|
||||||
if not hasattr(self, "app"):
|
if not hasattr(self, "app"):
|
||||||
self._initialize_app()
|
self._initialize_app()
|
||||||
|
|
||||||
@@ -198,8 +211,15 @@ class RAGStorage(BaseRAGStorage):
|
|||||||
OpenAIEmbeddingFunction,
|
OpenAIEmbeddingFunction,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
if not api_key:
|
||||||
|
logging.warning(
|
||||||
|
"OPENAI_API_KEY not found. Vector operations will be limited."
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
return OpenAIEmbeddingFunction(
|
return OpenAIEmbeddingFunction(
|
||||||
api_key=os.getenv("OPENAI_API_KEY"), model_name="text-embedding-3-small"
|
api_key=api_key, model_name="text-embedding-3-small"
|
||||||
)
|
)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import logging
|
import logging
|
||||||
|
|||||||
Reference in New Issue
Block a user