Spaces:
Runtime error
Runtime error
Commit ·
af079bc
1
Parent(s): c47aa3e
refactor: update LLM handler initialization parameters
Browse files- Adjust constructor parameter handling for better flexibility
- Improve API key and configuration management
- mapper/llm_handler.py +48 -3
mapper/llm_handler.py
CHANGED
|
@@ -17,7 +17,7 @@ class LLMHandler:
|
|
| 17 |
|
| 18 |
def __init__(self,
|
| 19 |
api_key: Optional[str] = None,
|
| 20 |
-
model_name: str = "llama-3.1-
|
| 21 |
temperature: float = 0.0,
|
| 22 |
max_tokens: int = 2048,
|
| 23 |
max_retries: int = 3):
|
|
@@ -164,7 +164,7 @@ Here are some example mappings from our template database:
|
|
| 164 |
# Parse response
|
| 165 |
content = response.content.strip()
|
| 166 |
|
| 167 |
-
# Extract JSON from response (handle code blocks)
|
| 168 |
if "```json" in content:
|
| 169 |
json_start = content.find("```json") + 7
|
| 170 |
json_end = content.find("```", json_start)
|
|
@@ -176,6 +176,16 @@ Here are some example mappings from our template database:
|
|
| 176 |
if json_end != -1:
|
| 177 |
content = content[json_start:json_end].strip()
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
mapping = json.loads(content)
|
| 180 |
|
| 181 |
# Validate mapping
|
|
@@ -187,7 +197,42 @@ Here are some example mappings from our template database:
|
|
| 187 |
|
| 188 |
except json.JSONDecodeError as e:
|
| 189 |
logger.error(f"Failed to parse LLM response as JSON (attempt {attempt + 1}): {e}")
|
| 190 |
-
logger.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
except Exception as e:
|
| 192 |
logger.error(f"LangChain LLM API call failed (attempt {attempt + 1}): {e}")
|
| 193 |
|
|
|
|
| 17 |
|
| 18 |
def __init__(self,
|
| 19 |
api_key: Optional[str] = None,
|
| 20 |
+
model_name: str = "llama-3.1-8b-instant",
|
| 21 |
temperature: float = 0.0,
|
| 22 |
max_tokens: int = 2048,
|
| 23 |
max_retries: int = 3):
|
|
|
|
| 164 |
# Parse response
|
| 165 |
content = response.content.strip()
|
| 166 |
|
| 167 |
+
# Extract JSON from response (handle code blocks and clean up)
|
| 168 |
if "```json" in content:
|
| 169 |
json_start = content.find("```json") + 7
|
| 170 |
json_end = content.find("```", json_start)
|
|
|
|
| 176 |
if json_end != -1:
|
| 177 |
content = content[json_start:json_end].strip()
|
| 178 |
|
| 179 |
+
# Find JSON object in response
|
| 180 |
+
json_start = content.find("{")
|
| 181 |
+
json_end = content.rfind("}") + 1
|
| 182 |
+
if json_start != -1 and json_end > json_start:
|
| 183 |
+
content = content[json_start:json_end]
|
| 184 |
+
|
| 185 |
+
# Clean up common JSON issues
|
| 186 |
+
content = content.replace("'", '"') # Replace single quotes with double quotes
|
| 187 |
+
content = content.replace('\n', ' ') # Remove newlines
|
| 188 |
+
|
| 189 |
mapping = json.loads(content)
|
| 190 |
|
| 191 |
# Validate mapping
|
|
|
|
| 197 |
|
| 198 |
except json.JSONDecodeError as e:
|
| 199 |
logger.error(f"Failed to parse LLM response as JSON (attempt {attempt + 1}): {e}")
|
| 200 |
+
logger.error(f"Raw response content: {repr(content)}")
|
| 201 |
+
|
| 202 |
+
# Try alternative parsing methods
|
| 203 |
+
try:
|
| 204 |
+
# Method 1: Use regex to extract key-value pairs
|
| 205 |
+
import re
|
| 206 |
+
pattern = r'"([^"]+)"\s*:\s*"([^"]+)"'
|
| 207 |
+
matches = re.findall(pattern, content)
|
| 208 |
+
if matches:
|
| 209 |
+
# Remove duplicates by keeping only the first occurrence of each target column
|
| 210 |
+
mapping = {}
|
| 211 |
+
used_targets = set()
|
| 212 |
+
for source, target in matches:
|
| 213 |
+
if target not in used_targets:
|
| 214 |
+
mapping[source] = target
|
| 215 |
+
used_targets.add(target)
|
| 216 |
+
else:
|
| 217 |
+
logger.debug(f"Skipping duplicate target mapping: {source} -> {target}")
|
| 218 |
+
|
| 219 |
+
if self._validate_mapping(mapping, gdf_columns, cdf_columns):
|
| 220 |
+
logger.info(f"Successfully parsed mapping using regex with {len(mapping)} field mappings (removed duplicates)")
|
| 221 |
+
return mapping
|
| 222 |
+
|
| 223 |
+
# Method 2: Try to fix common JSON issues
|
| 224 |
+
fixed_content = content
|
| 225 |
+
# Fix trailing commas
|
| 226 |
+
fixed_content = re.sub(r',\s*}', '}', fixed_content)
|
| 227 |
+
fixed_content = re.sub(r',\s*]', ']', fixed_content)
|
| 228 |
+
|
| 229 |
+
mapping = json.loads(fixed_content)
|
| 230 |
+
if self._validate_mapping(mapping, gdf_columns, cdf_columns):
|
| 231 |
+
logger.info(f"Successfully parsed mapping using fixed JSON with {len(mapping)} field mappings")
|
| 232 |
+
return mapping
|
| 233 |
+
|
| 234 |
+
except Exception as parse_error:
|
| 235 |
+
logger.debug(f"Alternative parsing failed: {parse_error}")
|
| 236 |
except Exception as e:
|
| 237 |
logger.error(f"LangChain LLM API call failed (attempt {attempt + 1}): {e}")
|
| 238 |
|