From 3146309fde5393641e9f888da7df4546615d1cd9 Mon Sep 17 00:00:00 2001 From: yangdx Date: Wed, 7 May 2025 10:52:26 +0800 Subject: [PATCH] Change function name from list_of_list_to_json to list_of_list_to_dict --- lightrag/operate.py | 14 +++++++------- lightrag/utils.py | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lightrag/operate.py b/lightrag/operate.py index 255b37cc..2f1a8a57 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -26,7 +26,7 @@ from .utils import ( CacheData, get_conversation_turns, use_llm_func_with_cache, - list_of_list_to_json, + list_of_list_to_dict, ) from .base import ( BaseGraphStorage, @@ -1549,7 +1549,7 @@ async def _get_node_data( file_path, ] ) - entities_context = list_of_list_to_json(entites_section_list) + entities_context = list_of_list_to_dict(entites_section_list) relations_section_list = [ [ @@ -1586,14 +1586,14 @@ async def _get_node_data( file_path, ] ) - relations_context = list_of_list_to_json(relations_section_list) + relations_context = list_of_list_to_dict(relations_section_list) text_units_section_list = [["id", "content", "file_path"]] for i, t in enumerate(use_text_units): text_units_section_list.append( [i, t["content"], t.get("file_path", "unknown_source")] ) - text_units_context = list_of_list_to_json(text_units_section_list) + text_units_context = list_of_list_to_dict(text_units_section_list) return entities_context, relations_context, text_units_context @@ -1871,7 +1871,7 @@ async def _get_edge_data( file_path, ] ) - relations_context = list_of_list_to_json(relations_section_list) + relations_context = list_of_list_to_dict(relations_section_list) entites_section_list = [ ["id", "entity", "type", "description", "rank", "created_at", "file_path"] @@ -1896,12 +1896,12 @@ async def _get_edge_data( file_path, ] ) - entities_context = list_of_list_to_json(entites_section_list) + entities_context = list_of_list_to_dict(entites_section_list) text_units_section_list = [["id", "content", "file_path"]] for i, t in enumerate(use_text_units): text_units_section_list.append([i, t["content"], t.get("file_path", "unknown")]) - text_units_context = list_of_list_to_json(text_units_section_list) + text_units_context = list_of_list_to_dict(text_units_section_list) return entities_context, relations_context, text_units_context diff --git a/lightrag/utils.py b/lightrag/utils.py index e4ef7699..2ed831b5 100644 --- a/lightrag/utils.py +++ b/lightrag/utils.py @@ -719,7 +719,25 @@ def truncate_list_by_token_size( return list_data -def list_of_list_to_json(data: list[list[str]]) -> list[dict[str, str]]: +def list_of_list_to_dict(data: list[list[str]]) -> list[dict[str, str]]: + """Convert a 2D string list (table-like data) into a list of dictionaries. + + The first row is treated as header containing field names. Subsequent rows become + dictionary entries where keys come from header and values from row data. + + Args: + data: 2D string array where first row contains headers and rest are data rows. + Minimum 2 columns required in data rows (rows with <2 elements are skipped). + + Returns: + List of dictionaries where each dict represents a data row with: + - Keys: Header values from first row + - Values: Corresponding row values (empty string if missing) + + Example: + Input: [["Name","Age"], ["Alice","23"], ["Bob"]] + Output: [{"Name":"Alice","Age":"23"}, {"Name":"Bob","Age":""}] + """ if not data or len(data) <= 1: return []