From 9fa6b91684a193bb9024eb359585481d1634ff5e Mon Sep 17 00:00:00 2001 From: Alonso Guevara Date: Wed, 9 Oct 2024 17:01:54 -0600 Subject: [PATCH] Chore/community context clean (#1262) * Update community_context.py to check conversation_history_context's value For the following code (line 90 - 96), conversation_history_context is concatenated with community_context, but the case where conversation_history_context is empty("") has not been considered. When conversation_history_context is empty (""), concatenation should not be performed, as it would result in community_context or each element in community_context having an extra "\n\n". Therefore, by introducing a context_prefix to check the state of conversation_history_context, concatenation can be handled appropriately. When conversation_history_context is empty (""), the following code will use "" for concatenation. When conversation_history_context is not empty (""), the functionality will be similar to the previous code. * Format and semver * Code cleanup --------- Co-authored-by: ZeyuTeng96 <96521059+ZeyuTeng96@users.noreply.github.com> --- .../patch-20241009221929632018.json | 4 ++++ .../global_search/community_context.py | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 .semversioner/next-release/patch-20241009221929632018.json diff --git a/.semversioner/next-release/patch-20241009221929632018.json b/.semversioner/next-release/patch-20241009221929632018.json new file mode 100644 index 0000000000..ec560627a9 --- /dev/null +++ b/.semversioner/next-release/patch-20241009221929632018.json @@ -0,0 +1,4 @@ +{ + "type": "patch", + "description": "Small cleanup in community context history building" +} diff --git a/graphrag/query/structured_search/global_search/community_context.py b/graphrag/query/structured_search/global_search/community_context.py index d63320c85b..f5991526c0 100644 --- a/graphrag/query/structured_search/global_search/community_context.py +++ b/graphrag/query/structured_search/global_search/community_context.py @@ -87,13 +87,21 @@ def build_context( context_name=context_name, random_state=self.random_state, ) - if isinstance(community_context, list): - final_context = [ - f"{conversation_history_context}\n\n{context}" - for context in community_context - ] - else: - final_context = f"{conversation_history_context}\n\n{community_context}" + # Prepare context_prefix based on whether conversation_history_context exists + context_prefix = ( + f"{conversation_history_context}\n\n" + if conversation_history_context + else "" + ) + + final_context = ( + [f"{context_prefix}{context}" for context in community_context] + if isinstance(community_context, list) + else f"{context_prefix}{community_context}" + ) + + # Update the final context data with the provided community_context_data final_context_data.update(community_context_data) - return (final_context, final_context_data) + + return final_context, final_context_data