Fix type-checker errors in embedding_configurator.py and knowledge_storage.py

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-21 05:14:42 +00:00
parent 7fb76bb858
commit 13e1aa96de
2 changed files with 37 additions and 20 deletions

View File

@@ -7,13 +7,16 @@ import shutil
import warnings
from typing import Any, Dict, List, Optional, Union, cast
# Initialize with None to indicate module import status
chromadb = None
ClientAPI = None
OneOrMany = None
Settings = None
# Initialize module import status
CHROMADB_AVAILABLE = False
# Define placeholder types
class DummyClientAPI:
pass
class DummySettings:
pass
# Try to import chromadb-related modules with proper error handling
try:
import chromadb
@@ -24,6 +27,11 @@ try:
CHROMADB_AVAILABLE = True
except (ImportError, AttributeError) as e:
warnings.warn(f"Failed to import chromadb: {str(e)}. Knowledge functionality will be limited.")
# Use dummy classes when imports fail
chromadb = None
ClientAPI = DummyClientAPI
OneOrMany = Any
Settings = DummySettings
from crewai.knowledge.storage.base_knowledge_storage import BaseKnowledgeStorage
from crewai.utilities import EmbeddingConfigurator

View File

@@ -1,29 +1,38 @@
import os
import warnings
from typing import Any, Dict, Optional, cast
from typing import Any, Dict, Optional, cast, List, Union, Callable
# Initialize with None to indicate module import status
Documents = None
EmbeddingFunction = None
Embeddings = None
validate_embedding_function = None
CHROMADB_AVAILABLE = False
# Define placeholder types for when chromadb is not available
class EmbeddingFunction:
def __call__(self, texts):
raise NotImplementedError("Chromadb is not available")
Documents = List[str]
Embeddings = List[List[float]]
def validate_embedding_function(func):
return func
# Try to import chromadb-related modules with proper error handling
try:
from chromadb import Documents, EmbeddingFunction, Embeddings
from chromadb.api.types import validate_embedding_function
from chromadb.api.types import Documents as ChromaDocuments
from chromadb.api.types import EmbeddingFunction as ChromaEmbeddingFunction
from chromadb.api.types import Embeddings as ChromaEmbeddings
from chromadb.utils import validate_embedding_function as chroma_validate_embedding_function
# Override our placeholder types with the real ones
Documents = ChromaDocuments
EmbeddingFunction = ChromaEmbeddingFunction
Embeddings = ChromaEmbeddings
validate_embedding_function = chroma_validate_embedding_function
CHROMADB_AVAILABLE = True
except (ImportError, AttributeError) as e:
# This captures both ImportError and AttributeError (which can happen with NumPy 2.x)
warnings.warn(f"Failed to import chromadb: {str(e)}. Embedding functionality will be limited.")
# Define a simple embedding function interface for typehinting
class EmbeddingFunction:
def __call__(self, texts):
raise NotImplementedError("Chromadb is not available")
CHROMADB_AVAILABLE = False
def validate_embedding_function(func):
return func
class EmbeddingConfigurator: