File size: 1,793 Bytes
8f78c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import json
from typing import Optional
import pandas as pd
import simplejson


def json_to_dict(response: str) -> dict:
    """Convert a JSON string to a Python dictionary.
    Args:
        response (str): JSON string to convert.
    Returns:
        dict: Parsed JSON as a dictionary.
    Raises:
        ValueError: If the JSON string is invalid.
    """

    # extract dict from json
    try:
        match = re.search(r"\{.*}", response, re.DOTALL)
        if match is None:
            raise ValueError("No valid JSON found in the response.")
        match_response = match.group()
        match_response = json.loads(match_response)
    except json.JSONDecodeError:
        try:
            match_response = simplejson.loads(response)  # More forgiving JSON parser
        except simplejson.JSONDecodeError as e:
            raise ValueError(f"Invalid JSON response: {e}")

    return match_response  # Return as a structured dictionary


def json_to_pandas(json_data: str, key: Optional[str] = None) -> pd.DataFrame:
    """Convert JSON data to a pandas DataFrame."""
    try:
        dic = json_to_dict(json_data)
        if key:
            dic = dic[key]
        df = pd.DataFrame(dic)
        return df
    except ValueError as e:
        raise ValueError(f"Invalid JSON data: {e}")


def extract_and_return_data_table(chat_output, key="Medications"):
    """Extract a pandas data frame out of the chat.
    Try rule-based first and use LLM if it fails."""

    if chat_output:
        if "content" in chat_output[-1]:
            chat_output = chat_output[-1]["content"]

    print(f"chat output: {chat_output}type: {type(chat_output)}")

    try:
        df = json_to_pandas(chat_output, key=key)
    except ValueError as e:
        raise ValueError(e)

    return df