|
| 1 | +""" |
| 2 | +Copyright 2024, Zep Software, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import functools |
| 18 | +import inspect |
| 19 | +from collections.abc import Awaitable, Callable |
| 20 | +from typing import Any, TypeVar |
| 21 | + |
| 22 | +from graphiti_core.driver.driver import GraphProvider |
| 23 | +from graphiti_core.helpers import semaphore_gather |
| 24 | +from graphiti_core.search.search_config import SearchResults |
| 25 | + |
| 26 | +F = TypeVar('F', bound=Callable[..., Awaitable[Any]]) |
| 27 | + |
| 28 | + |
| 29 | +def handle_multiple_group_ids(func: F) -> F: |
| 30 | + """ |
| 31 | + Decorator for FalkorDB methods that need to handle multiple group_ids. |
| 32 | + Runs the function for each group_id separately and merges results. |
| 33 | + """ |
| 34 | + |
| 35 | + @functools.wraps(func) |
| 36 | + async def wrapper(self, *args, **kwargs): |
| 37 | + group_ids_func_pos = get_parameter_position(func, 'group_ids') |
| 38 | + group_ids_pos = ( |
| 39 | + group_ids_func_pos - 1 if group_ids_func_pos is not None else None |
| 40 | + ) # Adjust for zero-based index |
| 41 | + group_ids = kwargs.get('group_ids') |
| 42 | + |
| 43 | + # If not in kwargs and position exists, get from args |
| 44 | + if group_ids is None and group_ids_pos is not None and len(args) > group_ids_pos: |
| 45 | + group_ids = args[group_ids_pos] |
| 46 | + |
| 47 | + # Only handle FalkorDB with multiple group_ids |
| 48 | + if ( |
| 49 | + hasattr(self, 'clients') |
| 50 | + and hasattr(self.clients, 'driver') |
| 51 | + and self.clients.driver.provider == GraphProvider.FALKORDB |
| 52 | + and group_ids |
| 53 | + and len(group_ids) > 1 |
| 54 | + ): |
| 55 | + # Execute for each group_id concurrently |
| 56 | + driver = self.clients.driver |
| 57 | + |
| 58 | + async def execute_for_group(gid: str): |
| 59 | + # Remove group_ids from args if it was passed positionally |
| 60 | + filtered_args = list(args) |
| 61 | + if group_ids_pos is not None and len(args) > group_ids_pos: |
| 62 | + filtered_args.pop(group_ids_pos) |
| 63 | + |
| 64 | + return await func( |
| 65 | + self, |
| 66 | + *filtered_args, |
| 67 | + **{**kwargs, 'group_ids': [gid], 'driver': driver.clone(database=gid)}, |
| 68 | + ) |
| 69 | + |
| 70 | + results = await semaphore_gather( |
| 71 | + *[execute_for_group(gid) for gid in group_ids], |
| 72 | + max_coroutines=getattr(self, 'max_coroutines', None), |
| 73 | + ) |
| 74 | + |
| 75 | + # Merge results based on type |
| 76 | + if isinstance(results[0], SearchResults): |
| 77 | + return SearchResults.merge(results) |
| 78 | + elif isinstance(results[0], list): |
| 79 | + return [item for result in results for item in result] |
| 80 | + elif isinstance(results[0], tuple): |
| 81 | + # Handle tuple outputs (like build_communities returning (nodes, edges)) |
| 82 | + merged_tuple = [] |
| 83 | + for i in range(len(results[0])): |
| 84 | + component_results = [result[i] for result in results] |
| 85 | + if isinstance(component_results[0], list): |
| 86 | + merged_tuple.append( |
| 87 | + [item for component in component_results for item in component] |
| 88 | + ) |
| 89 | + else: |
| 90 | + merged_tuple.append(component_results) |
| 91 | + return tuple(merged_tuple) |
| 92 | + else: |
| 93 | + return results |
| 94 | + |
| 95 | + # Normal execution |
| 96 | + return await func(self, *args, **kwargs) |
| 97 | + |
| 98 | + return wrapper # type: ignore |
| 99 | + |
| 100 | + |
| 101 | +def get_parameter_position(func: Callable, param_name: str) -> int | None: |
| 102 | + """ |
| 103 | + Returns the positional index of a parameter in the function signature. |
| 104 | + If the parameter is not found, returns None. |
| 105 | + """ |
| 106 | + sig = inspect.signature(func) |
| 107 | + for idx, (name, _param) in enumerate(sig.parameters.items()): |
| 108 | + if name == param_name: |
| 109 | + return idx |
| 110 | + return None |
0 commit comments