File size: 10,249 Bytes
8e874f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | """
Parsing utilities for QAFD-RAG.
Provides string parsing, JSON extraction, and text cleaning functions.
"""
import html
import json
import logging
import re
from typing import Any, List, Optional, Union
logger = logging.getLogger("QAFD_RAG")
def locate_json_string_body_from_string(content: str) -> Optional[str]:
"""
Extract JSON object from a string containing mixed content.
Useful for parsing LLM responses that contain JSON within other text.
Parameters:
-----------
content : str
String potentially containing JSON
Returns:
--------
Optional[str]
Extracted JSON string, or None if not found
"""
try:
maybe_json_str = re.search(r"{.*}", content, re.DOTALL)
if maybe_json_str is not None:
maybe_json_str = maybe_json_str.group(0)
maybe_json_str = maybe_json_str.replace("\\n", "")
maybe_json_str = maybe_json_str.replace("\n", "")
maybe_json_str = maybe_json_str.replace("'", '"')
return maybe_json_str
except Exception:
pass
return None
def convert_response_to_json(response: str) -> dict:
"""
Convert an LLM response string to a JSON dictionary.
Parameters:
-----------
response : str
LLM response string containing JSON
Returns:
--------
dict
Parsed JSON dictionary
Raises:
-------
AssertionError
If no JSON structure is found
json.JSONDecodeError
If JSON parsing fails
"""
json_str = locate_json_string_body_from_string(response)
assert json_str is not None, f"Unable to parse JSON from response: {response}"
try:
data = json.loads(json_str)
return data
except json.JSONDecodeError as e:
logger.error(f"Failed to parse JSON: {json_str}")
raise e from None
def split_string_by_multi_markers(content: str, markers: List[str]) -> List[str]:
"""
Split a string by multiple marker strings.
Parameters:
-----------
content : str
String to split
markers : List[str]
List of marker strings to split by
Returns:
--------
List[str]
List of non-empty stripped substrings
"""
if not markers:
return [content]
results = re.split("|".join(re.escape(marker) for marker in markers), content)
return [r.strip() for r in results if r.strip()]
def clean_str(input: Any) -> str:
"""
Clean a string by unescaping HTML and removing control characters.
Parameters:
-----------
input : Any
Input value (returns unchanged if not a string)
Returns:
--------
str
Cleaned string
"""
if not isinstance(input, str):
return input
result = html.unescape(input.strip())
return re.sub(r"[\x00-\x1f\x7f-\x9f]", "", result)
def is_float_regex(value: str) -> bool:
"""
Check if a string represents a valid float number.
Parameters:
-----------
value : str
String to check
Returns:
--------
bool
True if string is a valid float representation
"""
return bool(re.match(r"^[-+]?[0-9]*\.?[0-9]+$", value))
def safe_unicode_decode(content: bytes) -> str:
"""
Safely decode bytes to string, handling unicode escape sequences.
Parameters:
-----------
content : bytes
Bytes content to decode
Returns:
--------
str
Decoded string with unicode escapes resolved
"""
unicode_escape_pattern = re.compile(r"\\u([0-9a-fA-F]{4})")
def replace_unicode_escape(match):
return chr(int(match.group(1), 16))
decoded_content = unicode_escape_pattern.sub(
replace_unicode_escape, content.decode("utf-8")
)
return decoded_content
def pack_user_ass_to_openai_messages(*args: str) -> List[dict]:
"""
Pack alternating user/assistant messages into OpenAI message format.
Parameters:
-----------
*args : str
Alternating user and assistant message contents
Returns:
--------
List[dict]
List of message dictionaries with role and content
"""
roles = ["user", "assistant"]
return [
{"role": roles[i % 2], "content": content} for i, content in enumerate(args)
]
def extract_all_blocks(content: str, code_format: str = None) -> List[str]:
"""
Extract code/text blocks from an LLM response.
Tries three strategies in order:
1. <Answer>...</Answer> XML tags
2. <sql>...</sql> XML tags
3. Markdown code fences (```sql ... ```)
Ported from CoFD-M/methods/CoFD/utils.py.
"""
blocks = []
possible_patterns = [
("<Answer>", "</Answer>"),
("<Answer>", "<Answer>"),
("<answer>", "</answer>"),
]
if code_format == "json":
for start_tag, end_tag in possible_patterns:
if start_tag in content and end_tag in content:
extracted = content.split(start_tag)[1].split(end_tag)[0].strip()
blocks.append(extracted)
return blocks
if code_format == "text":
if "<Answer>" in content and "</Answer>" in content:
extracted = content.split("<Answer>")[1].split("</Answer>")[0].strip()
blocks.append(extracted)
return blocks
for start_tag, end_tag in possible_patterns:
if start_tag in content and end_tag in content:
try:
extracted = content.split(start_tag)[1].split(end_tag)[0].strip()
blocks.append(extracted)
return blocks
except Exception:
continue
if code_format == "sql" and not blocks:
xml_matches = re.findall(
r'<sql>\s*(.*?)\s*</sql>', content, re.DOTALL | re.IGNORECASE
)
if xml_matches:
blocks.extend([m.strip() for m in xml_matches if m.strip()])
if blocks:
return blocks
start = 0
while True:
search_pattern = f"```{code_format}" if code_format else "```"
block_start = content.find(search_pattern, start)
if block_start == -1:
break
block_end = content.find("```", block_start + len(search_pattern))
if block_end == -1:
break
block = content[block_start + len(search_pattern):block_end].strip()
blocks.append(block)
start = block_end + len("```")
return blocks
def extract_schema_from_context(schema_context: str) -> dict:
"""
Extract tables and columns from QAFD-RAG text2sql schema_context output.
Parses the CSV entity blocks to find entities with entity_type 'column'
or 'complete_table', deduplicates across local/global sections.
Returns:
dict with 'tables' (list of str) and 'columns' (list of 'table.column' str)
"""
import csv
from io import StringIO
tables = set()
columns = set()
# Extract all CSV blocks from the context
csv_blocks = extract_all_blocks(schema_context, "csv")
for block in csv_blocks:
# Skip non-entity blocks (relationship summaries, sources, etc.)
if "entity_type" not in block:
continue
reader = csv.DictReader(StringIO(block))
for row in reader:
entity_type = row.get("entity_type", "").strip()
entity = row.get("entity", "").strip().strip('"')
if entity_type == "complete_table":
tables.add(entity)
elif entity_type == "column":
columns.add(entity)
return {
"tables": sorted(tables),
"columns": sorted(columns),
}
def extract_schema_from_clusters(clusters: list) -> dict:
"""
Extract tables and columns from raw QAFD-RAG cluster data.
Each cluster has an 'entities' (or 'nodes') list with entity_type and entity fields.
Returns:
dict with 'tables' (list of str) and 'columns' (list of 'table.column' str)
"""
tables = set()
columns = set()
for cluster in clusters:
entities = cluster.get("entities", cluster.get("nodes", []))
for entity in entities:
entity_type = entity.get("entity_type", entity.get("type", "")).strip()
entity_name = entity.get("entity", "").strip().strip('"')
if entity_type == "complete_table":
tables.add(entity_name)
elif entity_type == "column" and "." in entity_name:
columns.add(entity_name)
return {
"tables": sorted(tables),
"columns": sorted(columns),
}
def extract_schema_from_create_table(create_table_text: str) -> dict:
"""
Extract tables and columns from CREATE TABLE statements.
Parses:
CREATE TABLE `table_name` (
`col1` TYPE ...,
`col2` TYPE ...
);
Returns:
dict with 'tables' (list of str) and 'columns' (list of 'table.column' str)
"""
tables = set()
columns = set()
current_table = None
for line in create_table_text.splitlines():
stripped = line.strip()
# Match CREATE TABLE `name` or CREATE TABLE name
table_match = re.match(r'CREATE\s+TABLE\s+`?(\w+)`?\s*\(', stripped, re.IGNORECASE)
if table_match:
current_table = table_match.group(1)
tables.add(current_table)
continue
# Match column definition: `col_name` TYPE ...
if current_table and stripped.startswith('`'):
col_match = re.match(r'`(\w+)`\s+\w+', stripped)
if col_match:
col_name = col_match.group(1)
columns.add(f"{current_table}.{col_name}")
# End of CREATE TABLE
if stripped.startswith(');'):
current_table = None
return {
"tables": sorted(tables),
"columns": sorted(columns),
}
__all__ = [
"locate_json_string_body_from_string",
"convert_response_to_json",
"split_string_by_multi_markers",
"clean_str",
"is_float_regex",
"safe_unicode_decode",
"pack_user_ass_to_openai_messages",
"extract_all_blocks",
"extract_schema_from_context",
"extract_schema_from_clusters",
"extract_schema_from_create_table",
]
|