写在前面
生成式 BI (GenBI) 正在改变我们与数据交互的方式。它允许用户使用自然语言提出问题,并自动获得数据洞察,而无需编写复杂的 SQL 查询或手动创建图表。本文将带你动手实战,使用 Python 和 DeepSeek API (或其他类似的大语言模型 API) 实现一个简单的 GenBI 流程:
- 输入: Markdown 表格形式的数据和自然语言查询。
- 处理: 利用 DeepSeek API 的强大语言理解和代码生成能力,将自然语言查询转换为 Python 代码,并使用该代码处理 Markdown 表格数据。
- 输出: 生成可视化图表(使用 Plotly 库)。
通过这个实战项目,你将学习如何:
- 使用 DeepSeek API (或其他 LLM API)。
- 解析 Markdown 表格数据。
- 将自然语言查询转换为 Python 代码。
- 使用 Python 代码处理数据并生成可视化图表 (使用 Plotly)。
前提条件
- Python 3.7 或更高版本
- 安装必要的 Python 库:
如果没有DeepSeek的API_KEY, 可以替换成其他LLM,比如OpenAI, 文心一言, 通义千问, 智谱AI等。需要稍微修改对应的API调用代码。pip install requests pandas plotly
- 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,需要做以下几处修改:
- API 密钥: 将
DEEPSEEK_API_KEY
替换为你所用 LLM 的 API 密钥。 - API 调用: 将
call_deepseek_api
函数替换为调用你所用 LLM API 的函数。 不同的 LLM API 有不同的调用方式(请求 URL、参数、返回结果格式等),需要参考其官方文档进行修改。 - 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 函数中处理了