File size: 429 Bytes
c97d8e1
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import json
import os
from langchain_core.messages import AIMessage
from typing import List, Dict, Tuple
import re

def extract_json(message: AIMessage) -> List[dict]:
    text = message.content
    pattern = r"```json(.*?)```"
    matches = re.findall(pattern, text, re.DOTALL)
    try:
        return [json.loads(match.strip()) for match in matches]
    except Exception:
        raise ValueError(f"Failed to parse: {message}")