vanna.AI 接口文档(三)

8
0
0
2025-02-20

vanna.AI 接口文档(三)

函数generate_plotly_code

函数概述:用于根据用户提出的问题、生成的 SQL 查询以及数据框架(DataFrame)的元数据,生成相应的 Plotly 绘图代码。该方法通过与大型语言模型(LLM)的交互,自动生成可视化所需的 Python 代码。

参数:

  • question:用户提出的问题。

  • sql:用于生成数据的 SQL 查询。

  • df_metadata:数据框架的元数据,通常包括数据类型和摘要信息。

  • **kwargs:其他可选参数。

流程解析:

  1. 方法定义:

    def generate_plotly_code( self, question: str = None, sql: str = None, df_metadata: str = None, **kwargs ) -> str:

  2. 构建系统消息:

    if question is not None:
        system_msg = f"The following is a pandas DataFrame that contains the results of the query that answers the question the user asked: '{question}'"
    else:
        system_msg = "The following is a pandas DataFrame "
    
    if sql is not None:
        system_msg += f"\n\nThe DataFrame was produced using this query: {sql}\n\n"
    
    system_msg += f"The following is information about the resulting pandas DataFrame 'df': \n{df_metadata}"
    

    根据提供的 questionsql,构建系统消息 system_msg,描述数据框架的来源和内容。

  3. 创建消息日志:

    message_log = [
        self.system_message(system_msg),
        self.user_message(
            "Can you generate the Python plotly code to chart the results of the dataframe? Assume the data is in a pandas dataframe called 'df'. If there is only one value in the dataframe, use an Indicator. Respond with only Python code. Do not answer with any explanations -- just the code."
        ),
    ]
    

    消息日志 message_log 包含两个消息:

    • 系统消息:描述数据框架的内容和来源。

    • 用户消息:请求生成用于绘制数据框架的 Plotly Python 代码,要求仅返回代码,无需解释。

  4. 提交提示并获取响应:

    plotly_code = self.submit_prompt(message_log, kwargs=kwargs)

    将消息日志提交给 LLM,获取生成的 Plotly 代码。

  5. 清理并提取代码:

    return self._sanitize_plotly_code(self._extract_python_code(plotly_code))

    从 LLM 的响应中提取 Python 代码,并进行清理,返回最终的 Plotly 代码字符串。

get_plotly_figure函数

函数概述:

这段代码定义了一个名为 get_plotly_figure 的函数,用于根据提供的 Plotly 绘图代码字符串和数据框(DataFrame)生成相应的 Plotly 图形对象(Figure)。该函数还支持在绘图时选择深色模式(dark mode)。

函数参数:

  • plotly_code(字符串):包含用于绘制图形的 Plotly Python 代码。

  • df(pandas.DataFrame):用于绘图的数据框。

  • dark_mode(布尔值,可选):指示是否使用深色模式的布尔值,默认为 True

函数功能:

  1. 执行绘图代码: 函数首先创建一个包含数据框 df、Plotly Express 模块 px 和 Plotly Graph Objects 模块 go 的本地字典 ldict。然后,使用 exec() 函数在该字典的命名空间中执行传入的 plotly_code

    ldict = {"df": df, "px": px, "go": go} try: exec(plotly_code, globals(), ldict) fig = ldict.get("fig", None) except Exception as e: # 异常处理代码

    exec() 是 Python 的内置函数,用于执行储存在字符串或编译对象中的 Python 代码。

    Python 文档

  2. 异常处理与默认绘图: 如果在执行 plotly_code 时发生异常,函数会根据数据框的列类型和数量,选择合适的默认绘图类型:

    • 两个或以上的数值列: 生成散点图(scatter plot)。

    • 一个数值列和至少一个分类列: 生成条形图(bar plot)。

    • 至少一个分类列且唯一值少于10个: 生成饼图(pie chart)。

    • 其他情况: 生成折线图(line plot)。

    except Exception as e:
        numeric_cols = df.select_dtypes(include=["number"]).columns.tolist()
        categorical_cols = df.select_dtypes(include=["object", "category"]).columns.tolist()
    
        if len(numeric_cols) >= 2:
            fig = px.scatter(df, x=numeric_cols[0], y=numeric_cols[1])
        elif len(numeric_cols) == 1 and len(categorical_cols) >= 1:
            fig = px.bar(df, x=categorical_cols[0], y=numeric_cols[0])
        elif len(categorical_cols) >= 1 and df[categorical_cols[0]].nunique() < 10:
            fig = px.pie(df, names=categorical_cols[0])
        else:
            fig = px.line(df)
    

  3. 应用深色模式: 如果 dark_mode 参数为 True,则使用 update_layout() 方法将图形的模板设置为 plotly_dark,以应用深色主题。

    if dark_mode:
        fig.update_layout(template="plotly_dark")
    

  4. 返回图形对象: 函数最终返回生成的 Plotly 图形对象 fig。如果在执行 plotly_code 后未能生成 fig,则返回 None