Repository: mem0ai/mem0
File: mem0/memory/graph_memory.py (Python)

Code before:

        extracted_entities = self.llm.generate_response(
            messages=messages,
            tools=_tools,
        )

        if extracted_entities["tool_calls"]:
            extracted_entities = extracted_entities["tool_calls"][0]["arguments"]["entities"]
        else:
            extracted_entities = []

        logger.debug(f"Extracted entities: {extracted_entities}")
        search_output_string = format_entities(search_output)
        update_memory_prompt = get_update_memory_messages(search_output_string, extracted_entities)

        _tools = [UPDATE_MEMORY_TOOL_GRAPH, ADD_MEMORY_TOOL_GRAPH, NOOP_TOOL]
        if self.llm_provider in ["azure_openai_structured", "openai_structured"]:
            _tools = [
                UPDATE_MEMORY_STRUCT_TOOL_GRAPH,
                ADD_MEMORY_STRUCT_TOOL_GRAPH,
                NOOP_STRUCT_TOOL,
            ]
        elif self.llm_provider == "gemini":
            # The `noop` ​​function n should be removed because it is unnecessary 
            # and causes the error: "should be non-empty for OBJECT type"
            _tools = [UPDATE_MEMORY_TOOL_GRAPH, ADD_MEMORY_TOOL_GRAPH]

        memory_updates = self.llm.generate_response(
            messages=update_memory_prompt,
            tools=_tools,
        )

        to_be_added = []

        for item in memory_updates["tool_calls"]:
            if item["name"] == "add_graph_memory":
                to_be_added.append(item["arguments"])
            elif item["name"] == "update_graph_memory":
                self._update_relationship(
                    item["arguments"]["source"],
                    item["arguments"]["destination"],
                    item["arguments"]["relationship"],
                    filters,
                )
            elif item["name"] == "noop":
                continue

        returned_entities = []

        for item in to_be_added:
            source = item["source"].lower().replace(" ", "_")

Review comment (suggestion):
Trying to add a `noop` ​​function here seems unnecessary, and will return an error that the value in the object cannot be empty (from the Gemini related library).

Code after:
            return []

        search_outputs_sequence = [[item["source"], item["relatationship"], item["destination"]] for item in search_output]
        bm25 = BM25Okapi(search_outputs_sequence)

        tokenized_query = query.split(" ")
        reranked_results = bm25.get_top_n(tokenized_query, search_outputs_sequence, n=5)

        search_results = []
        for item in reranked_results:
            search_results.append({"source": item[0], "relationship": item[1], "destination": item[2]})

        logger.info(f"Returned {len(search_results)} search results")

        return search_results

    def delete_all(self, filters):
        cypher = """
        MATCH (n {user_id: $user_id})
        DETACH DELETE n
        """
        params = {"user_id": filters["user_id"]}
        self.graph.query(cypher, params=params)

    def get_all(self, filters, limit=100):
        """
        Retrieves all nodes and relationships from the graph database based on optional filtering criteria.

        Args:
            filters (dict): A dictionary containing filters to be applied during the retrieval.
            limit (int): The maximum number of nodes and relationships to retrieve. Defaults to 100.
        Returns:
            list: A list of dictionaries, each containing:
                - 'contexts': The base data store response for each memory.
                - 'entities': A list of strings representing the nodes and relationships
        """

        # return all nodes and relationships
        query = """
        MATCH (n {user_id: $user_id})-[r]->(m {user_id: $user_id})
        RETURN n.name AS source, type(r) AS relationship, m.name AS target
        LIMIT $limit
        """
        results = self.graph.query(query, params={"user_id": filters["user_id"], "limit": limit})

        final_results = []
        for result in results:
            final_results.append(
                {
                    "source": result["source"],
                    "relationship": result["relationship"],