Issue with how crewai queries mem0 external memory

Dear CrewAI Community,
I am facing an issue with the way agents query external (mem0) memory, which renders the memory search ineffective.

Background:
I am trying out using mem0 as a common external memory for 2 crews. Very quickly, this is how it has been implemented in both

@CrewBase
class DocumentRetrievalCrew:
    """Document Retrieval Crew"""

    agents_config = "config/document_retrieval_crew/agents.yaml"
    tasks_config = "config/document_retrieval_crew/tasks.yaml"

    def __init__(self, model: str, domain: str):
        self.model_name = model
        self.llm = LLM(model=self.model_name)
        self.external_memory = ExternalMemory(
            embedder_config={
                "provider": "mem0", 
                "config": {"user_id": domain}
                }
            )
                      .
                      .
                      .

    @crew
    def crew(self) -> Crew:
        """Creates the Crew"""
        return Crew(
            agents=self.agents,
            tasks=self.tasks,
            process=Process.sequential,
            external_memory=self.external_memory,
            verbose=True,
        )

The domain is used to partition memories by setting their user_id as the domain name

Issue
I have noticed that while memories are added just fine, my crews always face issues querying the memories during a crew run. Looking at my mem0 dashboard, it seems like the agents are putting the entire task prompt wholesale into the query:

From mem0 dashboard:

Query:
"""
The domain is: <domain> Search the knowledge sources using the tools provided for 
the most relevant information to the query: <query> 
Only search the knowledge sources if the query is related to their descriptions.
"""

Since the entire task prompt contains a lot of extra, unnecessary information, the semantic search does not find memories relevant to this prompt, even though such memories do exist. As such, the memory searches always come out with 0 results:

Image from mem0 dashboard
image

I can’t even fix this in the agent prompt by instructing it to use focused queries during memory search, since memory searching in crewai doesn’t work like a tool. It seems like using the task prompt wholesale as a query for the memory search is built-in crewai behaviour.

Has anyone else faced this issue?

Possible solution:
The only possible solution I can think of is creating a custom Storage object to wrap the mem0 memory, and pass this object as the external_memory for my crew. This Storage object would then have to have a parser function which takes the Agent’s query and filters out irrelevant content before querying the mem0 vector database. However, this doesn’t seem like the most optimal solution.

Any help would be greatly appreciated!

Hi @Archit_Sharma great question and well formed.

I tend to preload my knowledge into mem0 (generally based on code like mem0/crewai_load_pdf.py at main · yqup/mem0 · GitHub)

This means the knowledge is available to the crew.

Does this help?

Thank you for your response, the PDF uploading script does make uploading content to memory quite convenient.

However, my issue was not with adding memories, but rather with my agents searching for memories while the crew is running. The agent always queries the memory vector database by putting its entire task prompt into the query. This way, the query is not focused and searching through the memory never works.

Got it!

If you have this issue you might want to build a custom mem0 search tool following the custom template here Tools - CrewAI. You could then pass it some parameter in the task or hard coded.
This might help the crew use the memory in a search.