Change function name from list_of_list_to_json to list_of_list_to_dict

This commit is contained in:
yangdx
2025-05-07 10:52:26 +08:00
parent edb3d6ac11
commit 3146309fde
2 changed files with 26 additions and 8 deletions

View File

@@ -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

View File

@@ -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 []