MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / parse_value

Function parse_value

_cluster_utils.py:100–119  ·  view source on GitHub ↗

Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()

(value: str)

Source from the content-addressed store, hash-verified

98 # 输出 JSON 字符串(可选择写入文件)
99 return result
100def parse_value(value: str):
101 """Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()'"""
102 value = value.strip()
103
104 if value == "null":
105 return None
106 elif value == "true":
107 return True
108 elif value == "false":
109 return False
110 else:
111 # Try to convert to int or float
112 try:
113 if '.' in value: # If there's a dot, it might be a float
114 return float(value)
115 else:
116 return int(value)
117 except ValueError:
118 # If conversion fails, return the value as-is (likely a string)
119 return value.strip('"') # Remove surrounding quotes if they exist
120def extract_values_from_json(json_string, keys=["reasoning", "answer", "data"], allow_no_quotes=False):
121 """Extract key values from a non-standard or malformed JSON string, handling nested objects."""
122 extracted_values = {}

Callers 1

extract_values_from_jsonFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected