From 638a8f03f054f2b10b2e0e4451c9a3316961e7af Mon Sep 17 00:00:00 2001 From: Joshua Harper <82404128+JH427@users.noreply.github.com> Date: Sat, 10 Aug 2024 08:50:38 -0400 Subject: [PATCH] Sanitize agent roles to ensure valid directory names (#1037) --- src/crewai/memory/storage/rag_storage.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/crewai/memory/storage/rag_storage.py b/src/crewai/memory/storage/rag_storage.py index 5270e9c026..7d3758ab74 100644 --- a/src/crewai/memory/storage/rag_storage.py +++ b/src/crewai/memory/storage/rag_storage.py @@ -47,7 +47,7 @@ def __init__(self, type, allow_reset=True, embedder_config=None, crew=None): os.environ["OPENAI_API_KEY"] = "fake" agents = crew.agents if crew else [] - agents = [agent.role for agent in agents] + agents = [self._sanitize_role(agent.role) for agent in agents] agents = "_".join(agents) config = { @@ -77,6 +77,12 @@ def __init__(self, type, allow_reset=True, embedder_config=None, crew=None): self.app.llm = FakeLLM() if allow_reset: self.app.reset() + + def _sanitize_role(self, role: str) -> str: + """ + Sanitizes agent roles to ensure valid directory names. + """ + return role.replace('\n', '').replace(' ', '_').replace('/', '_') def save(self, value: Any, metadata: Dict[str, Any]) -> None: self._generate_embedding(value, metadata)