【GenBI实战】python脚本实现基于DeepSeek api的数据查询和图表可视化

news/2025/3/1 9:25:51/

写在前面

生成式 BI (GenBI) 正在改变我们与数据交互的方式。它允许用户使用自然语言提出问题,并自动获得数据洞察,而无需编写复杂的 SQL 查询或手动创建图表。本文将带你动手实战,使用 Python 和 DeepSeek API (或其他类似的大语言模型 API) 实现一个简单的 GenBI 流程:

  1. 输入: Markdown 表格形式的数据和自然语言查询。
  2. 处理: 利用 DeepSeek API 的强大语言理解和代码生成能力,将自然语言查询转换为 Python 代码,并使用该代码处理 Markdown 表格数据。
  3. 输出: 生成可视化图表(使用 Plotly 库)。

通过这个实战项目,你将学习如何:

  • 使用 DeepSeek API (或其他 LLM API)。
  • 解析 Markdown 表格数据。
  • 将自然语言查询转换为 Python 代码。
  • 使用 Python 代码处理数据并生成可视化图表 (使用 Plotly)。

前提条件

  • Python 3.7 或更高版本
  • 安装必要的 Python 库:
    pip install requests pandas plotly
    
    如果没有DeepSeek的API_KEY, 可以替换成其他LLM,比如OpenAI, 文心一言, 通义千问, 智谱AI等。需要稍微修改对应的API调用代码。
  • DeepSeek API 密钥 (或其他 LLM API 密钥)

项目结构

genbi_project/
├── main.py        # 主程序
├── data.md        # 示例 Markdown 数据文件 (可选)
└── requirements.txt # 依赖库列表

步骤 1:设置 DeepSeek API 密钥 (或其他 LLM API 密钥)
首先,需要获取DeepSeek API的密钥。访问DeepSeek官网注册账号,然后在控制台中找到你的API key。
如果你没有DeepSeek的API key,可以使用其他大语言模型API, 例如:

  • OpenAI: 访问 https://platform.openai.com/ 注册并获取 API 密钥。
  • 文心一言 (ERNIE Bot): 访问 https://yiyan.baidu.com/ 注册并获取 API 密钥。
  • 通义千问: 访问https://tongyi.aliyun.com/注册并获取API key.
  • 智谱AI: 访问https://open.bigmodel.cn/注册并获取API key.

将获取到的 API 密钥设置为环境变量或直接在代码中设置(不推荐在代码中明文存储密钥):

python"># main.py (示例 - 使用环境变量)
import os# 从环境变量获取 API 密钥 (推荐)
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
# 或者直接设置 API 密钥 (不推荐)
# DEEPSEEK_API_KEY = "YOUR_DEEPSEEK_API_KEY"

替换其他LLM的API KEY和调用方式
如果使用其他LLM,需要做以下几处修改:

  1. API 密钥:DEEPSEEK_API_KEY 替换为你所用 LLM 的 API 密钥。
  2. API 调用:call_deepseek_api 函数替换为调用你所用 LLM API 的函数。 不同的 LLM API 有不同的调用方式(请求 URL、参数、返回结果格式等),需要参考其官方文档进行修改。
  3. Prompt: Prompt 的内容可能需要根据不同 LLM 的特点进行微调,以获得最佳效果。

步骤 2:定义 DeepSeek API 调用函数

创建一个函数来调用 DeepSeek API:

python"># main.py
import requestsdef call_deepseek_api(prompt, model="deepseek-coder", max_tokens=1000, temperature=0.5):"""调用 DeepSeek API。Args:prompt: 输入给模型的 Prompt。model: 使用的模型名称。max_tokens: 生成的最大 token 数。temperature: 控制生成结果的随机性。Returns:模型生成的文本,如果发生错误则返回 None。"""if DEEPSEEK_API_KEY is None:raise ValueError("DeepSeek API key not found.  Set the DEEPSEEK_API_KEY environment variable.")url = "https://api.deepseek.com/v1/chat/completions"  # 请根据 DeepSeek API 文档修改 URLheaders = {"Content-Type": "application/json","Authorization": f"Bearer {DEEPSEEK_API_KEY}",}data = {"model": model,"messages": [{"role": "user", "content": prompt}],"max_tokens": max_tokens,"temperature": temperature,}try:response = requests.post(url, headers=headers, json=data)response.raise_for_status()  # 如果请求失败,抛出异常return response.json()["choices"][0]["message"]["content"]except requests.exceptions.RequestException as e:print(f"Error calling DeepSeek API: {e}")return None

步骤 3:解析 Markdown 表格数据

创建一个函数来解析 Markdown 表格数据,并将其转换为 Pandas DataFrame:

python"># main.py
import pandas as pd
import redef parse_markdown_table(markdown_table):"""解析 Markdown 表格数据,并将其转换为 Pandas DataFrame。Args:markdown_table: Markdown 表格字符串。Returns:Pandas DataFrame,如果解析失败则返回 None。"""try:# 使用正则表达式分割行lines = markdown_table.strip().split('\n')header = [s.strip() for s in re.split(r"\|", lines[0])[1:-1]]# 移除表头下的分隔线lines = lines[2:]data = []for line in lines:# 使用正则表达式分割单元格, 考虑 | 前后可能有空格row = [s.strip() for s in re.split(r"\s*\|\s*", line)[1:-1]]data.append(row)df = pd.DataFrame(data, columns=header)return dfexcept Exception as e:print(f"Error parsing Markdown table: {e}")return None

步骤 4:构建 Prompt 并调用 DeepSeek API

创建一个函数来构建 Prompt,调用 DeepSeek API,并获取生成的 Python 代码:

python"># main.pydef generate_python_code(markdown_table, query):"""构建 Prompt,调用 DeepSeek API,并获取生成的 Python 代码。Args:markdown_table: Markdown 表格字符串。query: 自然语言查询。Returns:生成的 Python 代码(字符串形式),如果生成失败则返回 None。"""prompt = f"""
You are a helpful assistant that generates Python code to analyze data and create visualizations.
You are given a Markdown table and a natural language query.
Generate Python code (using pandas and plotly) to:
1.  Parse the Markdown table into a pandas DataFrame.
2.  Process the DataFrame to answer the query.
3.  Create a visualization (using plotly) of the result.
4.  Print the figure in JSON format using `fig.to_json()`. Do *not* use `fig.show()`.Markdown Table:
```markdown
{markdown_table}

Natural Language Query:
{query}

Python Code:

python">"""code = call_deepseek_api(prompt)return code

步骤 5:执行生成的 Python 代码并获取可视化结果

创建一个函数来执行生成的 Python 代码,并获取 Plotly 图表的 JSON 表示:

python"># main.py
import jsondef execute_code_and_get_visualization(code):"""执行生成的 Python 代码,并获取 Plotly 图表的 JSON 表示。Args:code: 要执行的 Python 代码。Returns:Plotly 图表的 JSON 表示(字符串形式),如果执行失败则返回 None。"""try:# 创建一个局部命名空间,用于执行代码local_vars = {}exec(code, {}, local_vars)# 检查是否有 'fig' 变量 (Plotly 图表对象)if 'fig' in local_vars:fig = local_vars['fig']# 将 Plotly 图表转换为 JSON 格式fig_json = fig.to_json()return fig_jsonelse:print("Error: No 'fig' variable found in the generated code.")return Noneexcept Exception as e:print(f"Error executing generated code: {e}")return None

步骤 6:主程序逻辑

python"># main.py
def main():"""主程序逻辑。"""# 示例 Markdown 表格数据markdown_table = """
| Region | Sales | Profit |
|---|---|---|
| North America | 1200000 | 240000 |
| Europe | 950000 | 190000 |
| Asia | 800000 | 160000 |
| South America | 500000 | 80000 |
| Africa | 300000 | 45000 |
"""# 示例自然语言查询query = "Show a bar chart of sales by region, sorted in descending order."# 生成 Python 代码code = generate_python_code(markdown_table, query)if code:print("Generated Python code:\n", code)# 执行代码并获取可视化结果visualization_json = execute_code_and_get_visualization(code)if visualization_json:print("\nVisualization (JSON format):\n", visualization_json)# (可选) 将 JSON 数据保存到文件with open("visualization.json", "w") as f:f.write(visualization_json)print("\nVisualization saved to visualization.json")# (可选) 在浏览器中显示图表 (需要额外的 JavaScript 代码)else:print("Failed to generate visualization.")else:print("Failed to generate Python code.")
if __name__ == "__main__":main()

完整的 main.py 代码:

python">import os
import requests
import pandas as pd
import re
import json
import plotly.express as px# 从环境变量获取 API 密钥 (推荐)
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY")
# 或者直接设置 API 密钥 (不推荐)
# DEEPSEEK_API_KEY = "YOUR_DEEPSEEK_API_KEY"def call_deepseek_api(prompt, model="deepseek-coder", max_tokens=1000, temperature=0.5):"""调用 DeepSeek API。Args:prompt: 输入给模型的 Prompt。model: 使用的模型名称。max_tokens: 生成的最大 token 数。temperature: 控制生成结果的随机性。Returns:模型生成的文本,如果发生错误则返回 None。"""if DEEPSEEK_API_KEY is None:raise ValueError("DeepSeek API key not found.  Set the DEEPSEEK_API_KEY environment variable.")url = "https://api.deepseek.com/v1/chat/completions"  # 请根据 DeepSeek API 文档修改 URLheaders = {"Content-Type": "application/json","Authorization": f"Bearer {DEEPSEEK_API_KEY}",}data = {"model": model,"messages": [{"role": "user", "content": prompt}],"max_tokens": max_tokens,"temperature": temperature,}try:response = requests.post(url, headers=headers, json=data)response.raise_for_status()  # 如果请求失败,抛出异常return response.json()["choices"][0]["message"]["content"]except requests.exceptions.RequestException as e:print(f"Error calling DeepSeek API: {e}")return None
def parse_markdown_table(markdown_table):"""解析 Markdown 表格数据,并将其转换为 Pandas DataFrame。Args:markdown_table: Markdown 表格字符串。Returns:Pandas DataFrame,如果解析失败则返回 None。"""try:# 使用正则表达式分割行lines = markdown_table.strip().split('\n')header = [s.strip() for s in re.split(r"\|", lines[0])[1:-1]]# 移除表头下的分隔线lines = lines[2:]data = []for line in lines:# 使用正则表达式分割单元格, 考虑 | 前后可能有空格row = [s.strip() for s in re.split(r"\s*\|\s*", line)[1:-1]]data.append(row)df = pd.DataFrame(data, columns=header)return dfexcept Exception as e:print(f"Error parsing Markdown table: {e}")return None
def generate_python_code(markdown_table, query):"""构建 Prompt,调用 DeepSeek API,并获取生成的 Python 代码。Args:markdown_table: Markdown 表格字符串。query: 自然语言查询。Returns:生成的 Python 代码(字符串形式),如果生成失败则返回 None。"""prompt = f"""
You are a helpful assistant that generates Python code to analyze data and create visualizations.
You are given a Markdown table and a natural language query.
Generate Python code (using pandas and plotly) to:
1.  Parse the Markdown table into a pandas DataFrame.
2.  Process the DataFrame to answer the query.
3.  Create a visualization (using plotly) of the result.
4.  Print the figure in JSON format using `fig.to_json()`. Do *not* use `fig.show()`.Markdown Table:
```markdown
{markdown_table}

Natural Language Query:
{query}

Python Code:

python">"""code = call_deepseek_api(prompt)return code
import jsondef execute_code_and_get_visualization(code):"""执行生成的 Python 代码,并获取 Plotly 图表的 JSON 表示。Args:code: 要执行的 Python 代码。Returns:Plotly 图表的 JSON 表示(字符串形式),如果执行失败则返回 None"""try:# 创建一个局部命名空间,用于执行代码local_vars = {}exec(code, {}, local_vars)# 检查是否有 'fig' 变量 (Plotly 图表对象)if 'fig' in local_vars:fig = local_vars['fig']# 将 Plotly 图表转换为 JSON 格式fig_json = fig.to_json()return fig_jsonelse:print("Error: No 'fig' variable found in the generated code.")return Noneexcept Exception as e:print(f"Error executing generated code: {e}")return None
def main():"""主程序逻辑。"""# 示例 Markdown 表格数据markdown_table = """
| Region | Sales | Profit |
|---|---|---|
| North America | 1200000 | 240000 |
| Europe | 950000 | 190000 |
| Asia | 800000 | 160000 |
| South America | 500000 | 80000 |
| Africa | 300000 | 45000 |
"""# 示例自然语言查询query = "Show a bar chart of sales by region, sorted in descending order."# 生成 Python 代码code = generate_python_code(markdown_table, query)if code:print("Generated Python code:\n", code)# 执行代码并获取可视化结果visualization_json = execute_code_and_get_visualization(code)if visualization_json:print("\nVisualization (JSON format):\n", visualization_json)# (可选) 将 JSON 数据保存到文件with open("visualization.json", "w") as f:f.write(visualization_json)print("\nVisualization saved to visualization.json")# (可选) 在浏览器中显示图表 (需要额外的 JavaScript 代码)else:print("Failed to generate visualization.")else:print("Failed to generate Python code.")
if __name__ == "__main__":main()

运行结果
成功运行后,控制台会输出生成的Python代码和图表的JSON格式数据。
程序会在目录下创建一个visualization.json文件。
DeepSeek API (或其他 LLM API) 生成的 Python 代码可能如下所示(实际生成的代码可能略有不同):

python">import pandas as pd
import plotly.express as px
import re# 解析 Markdown 表格 (与 parse_markdown_table 函数相同)
def parse_markdown_table(markdown_table):try:lines = markdown_table.strip().split('\n')header = [s.strip() for s in re.split(r"\|", lines[0])[1:-1]]lines = lines[2:]data = []for line in lines:row = [s.strip() for s in re.split(r"\s*\|\s*", line)[1:-1]]data.append(row)df = pd.DataFrame(data, columns=header)return dfexcept Exception as e:print(f"Error parsing Markdown table: {e}")return None# 将 Markdown 表格解析为 DataFrame
df = parse_markdown_table(markdown_table)# 将 'Sales' 列转换为数值类型
df['Sales'] = pd.to_numeric(df['Sales'])# 按 'Sales' 列降序排序
df_sorted = df.sort_values('Sales', ascending=False)# 创建柱状图
fig = px.bar(df_sorted, x='Region', y='Sales', title='Sales by Region (Sorted)')# 将图表转换为 JSON 格式 (重要:不要使用 fig.show())
# print(fig.to_json()) # 这行被注释掉,因为我们已经在 execute_code_and_get_visualization 函数中处理了

http://www.ppmy.cn/news/1575736.html

相关文章

嵌入式轻量化SDK设计,EasyRTC音视频通话SDK压缩至500K-800K

在物联网和智能硬件飞速发展的今天,嵌入式设备的实时音视频通信能力变得愈发重要。然而,受限于硬件资源,尤其是Flash存储空间的不足,传统音视频通信方案往往难以在嵌入式设备上实现高效集成。 例如,智能摄像头需要实时…

PHP如何与HTML结合使用?

PHP与HTML结合使用的主要方式是通过在HTML文件中嵌入PHP代码&#xff0c;从而实现动态内容的生成和网页的交互性。以下是详细的方法和最佳实践&#xff1a; 1. 嵌入PHP代码到HTML中 PHP代码可以直接嵌入到HTML文件中&#xff0c;通过<?php ?>标签来包裹PHP代码。服务…

【Linux】修改 core 文件大小和路径

在 Linux 系统中&#xff0c;默认情况下&#xff0c;核心转储文件&#xff08;core dump&#xff09;会生成在当前工作目录下。为了将核心转储文件生成在指定路径下&#xff0c;可以通过以下方法进行配置。 1. 设置核心转储文件路径 Linux 系统提供了两种方式来指定核心转储文…

性能测试测试策略制定|知名软件测评机构经验分享

随着互联网产品的普及&#xff0c;产品面对的用户量级也越来越大&#xff0c;能抗住指数级增长的瞬间访问量以及交易量是保障购物体验是否顺畅的至关重要的一环&#xff0c;而我们的性能测试恰恰也是为此而存在的。 性能测试是什么呢&#xff1f;性能测试要怎么测呢&#xff1f…

FPGA之硬件设计笔记-持续更新中

系列文章目录 提示&#xff1a;这里可以添加系列文章的所有文章的目录&#xff0c;目录需要自己手动添加 例如&#xff1a;第一章 Python 机器学习入门之pandas的使用 提示&#xff1a;写完文章后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 目录 系…

Kotlin 协程与Flow

Flow的&#xff1a; https://juejin.cn/post/7126730252633505829 协程的&#xff1a; https://juejin.cn/post/6908271959381901325 什么是协程 协程可以理解成线程中的线程&#xff0c;进程跟线程的关系&#xff0c;差不多就是线程跟协程的关系。 一个进程里可以有多个线…

deepseek自动化代码生成

使用流程 效果第一步&#xff1a;注册生成各种大模型的API第二步&#xff1a;注册成功后生成API第三步&#xff1a;下载vscode在vscode中下载agent&#xff0c;这里推荐使用cline 第四步&#xff1a;安装完成后&#xff0c;设置模型信息第一步选择API provider&#xff1a; Ope…

vue el-table-column 单元表格的 省略号 实现

要对 el-table-column 的某一列中的每个单元格值进行处理&#xff0c;使其在文本内容超出指定宽度时显示省略号&#xff08;…&#xff09;&#xff0c;可以通过以下方法实现&#xff1a; 使用 scoped slots&#xff1a;利用 Element UI 提供的 scoped slots 自定义单元格内容…