File size: 956 Bytes
9cccf74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from utils.insight_utils import df_to_html, pil_to_base64
from builder.state import AgentState

def run_agent(app, tracer, message, history):
    history = history or []

    result: AgentState = app.invoke(
        {"question": message, "history": history},
        config={"callbacks": [tracer]}
    )

    bot_message = ""
    if result.get("narrative"):
        bot_message += f"**Datum:**\n{result['narrative']}\n\n"

    if result.get("sql"):
        bot_message += f"**SQL:**\n```sql\n{result['sql']}\n```\n"

    if chart_html := pil_to_base64(result.get("chart_pil")):
        bot_message += chart_html + "\n"

    if df_html := df_to_html(result.get("df", pd.DataFrame())):
        bot_message += df_html

    updated_history = history + [
        {"role": "user", "content": message},
        {"role": "assistant", "content": bot_message}
    ]

    return updated_history, updated_history, ""