fix: add json parse step

This commit is contained in:
Novice 2025-03-28 10:39:59 +08:00
parent 14b65efcf7
commit cb664f8c97
3 changed files with 19 additions and 4 deletions

View File

@ -362,7 +362,7 @@ class LLMGenerator:
)
prompt_messages = [UserPromptMessage(content=prompt)]
model_parameters = {"max_tokens": max_tokens, "temperature": 0.01}
model_parameters = model_config.get("model_parameters", {})
try:
response = cast(

View File

@ -198,14 +198,17 @@ class LLMNode(BaseNode[LLMNodeData]):
structured_output = {}
try:
structured_output = parse_partial_json(result_text)
except json.JSONDecodeError:
except (json.JSONDecodeError, ValueError):
# Try to find JSON string within triple backticks
_json_markdown_re = re.compile(r"```(json)?(.*)", re.DOTALL)
match = _json_markdown_re.search(result_text)
# If no match found, assume the entire string is a JSON string
# Else, use the content within the backticks
json_str = result_text if match is None else match.group(2)
try:
structured_output = parse_partial_json(json_str)
except (json.JSONDecodeError, ValueError) as e:
raise LLMNodeError(f"Failed to parse structured output: {e}")
outputs["structured_output"] = structured_output
yield RunCompletedEvent(
run_result=NodeRunResult(

View File

@ -78,4 +78,16 @@ def parse_partial_json(s: str, *, strict: bool = False) -> Any:
# If we got here, we ran out of characters to remove
# and still couldn't parse the string as JSON, so return the parse error
# for the original string.
try:
return json.loads(s, strict=strict)
except json.JSONDecodeError:
return extract_json(s)
def extract_json(response: str):
try:
json_start = response.index("{")
json_end = response.rfind("}")
return json.loads(response[json_start : json_end + 1])
except (json.JSONDecodeError, ValueError):
raise ValueError("output is not a valid json str")