11import argparse
22import logging
33import os
4- from typing import Union
4+ from typing import Any , Union
55
66logger = logging .getLogger ("mcp_neo4j_cypher" )
77logger .setLevel (logging .INFO )
@@ -169,3 +169,57 @@ def process_config(args: argparse.Namespace) -> dict[str, Union[str, int, None]]
169169 config ["path" ] = None
170170
171171 return config
172+
173+ def _value_sanitize (d : Any , list_limit : int = 128 ) -> Any :
174+ """
175+ Sanitize the input dictionary or list.
176+
177+ Sanitizes the input by removing embedding-like values,
178+ lists with more than 128 elements, that are mostly irrelevant for
179+ generating answers in a LLM context. These properties, if left in
180+ results, can occupy significant context space and detract from
181+ the LLM's performance by introducing unnecessary noise and cost.
182+
183+ Sourced from: https://github.com/neo4j/neo4j-graphrag-python/blob/main/src/neo4j_graphrag/schema.py#L88
184+
185+ Parameters
186+ ----------
187+ d : Any
188+ The input dictionary or list to sanitize.
189+ list_limit : int
190+ The limit for the number of elements in a list.
191+
192+ Returns
193+ -------
194+ Any
195+ The sanitized dictionary or list.
196+ """
197+ if isinstance (d , dict ):
198+ new_dict = {}
199+ for key , value in d .items ():
200+ if isinstance (value , dict ):
201+ sanitized_value = _value_sanitize (value )
202+ if (
203+ sanitized_value is not None
204+ ): # Check if the sanitized value is not None
205+ new_dict [key ] = sanitized_value
206+ elif isinstance (value , list ):
207+ if len (value ) < list_limit :
208+ sanitized_value = _value_sanitize (value )
209+ if (
210+ sanitized_value is not None
211+ ): # Check if the sanitized value is not None
212+ new_dict [key ] = sanitized_value
213+ # Do not include the key if the list is oversized
214+ else :
215+ new_dict [key ] = value
216+ return new_dict
217+ elif isinstance (d , list ):
218+ if len (d ) < list_limit :
219+ return [
220+ _value_sanitize (item ) for item in d if _value_sanitize (item ) is not None
221+ ]
222+ else :
223+ return None
224+ else :
225+ return d
0 commit comments