Fix ValidationError when using memory=True without OpenAI API key

- Add fallback embedding providers in EmbeddingConfigurator
- Modify RAGStorage and KnowledgeStorage to use fallback mechanism
- Add comprehensive tests for memory functionality without OpenAI API key
- Resolves issue #2943 by allowing memory=True with alternative embedding providers

Fallback hierarchy: OpenAI -> Ollama -> HuggingFace -> SentenceTransformers

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-03 18:36:58 +00:00
parent 2bd6b72aae
commit faddb7dca2
5 changed files with 180 additions and 15 deletions

View File

@@ -110,6 +110,40 @@ def test_crew_config_conditional_requirement():
with pytest.raises(ValueError):
Crew(process=Process.sequential)
def test_crew_creation_with_memory_true_no_openai_key():
"""Test that crew can be created with memory=True when no OpenAI API key is available."""
import os
from unittest.mock import patch
with patch.dict(os.environ, {}, clear=True):
if 'OPENAI_API_KEY' in os.environ:
del os.environ['OPENAI_API_KEY']
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory"
)
task = Task(
description="Test task",
expected_output="Test output",
agent=agent
)
crew = Crew(
agents=[agent],
tasks=[task],
process=Process.sequential,
memory=True
)
assert crew.memory is True
assert crew._short_term_memory is not None
assert crew._entity_memory is not None
assert crew._long_term_memory is not None
config = json.dumps(
{
"agents": [