自然语言处理谷歌Bigquery数据库,text-to-SQL in Bigquery

实现目标:使用自然语言处理我在Bigquery中建立的数据库,并最终将结果用HTML 表格展现出来。

五分钟简易构建,用的第三方付费网站实现Text-to-SQL,调用的API接口,有7天免费试用。目前看来准确率尚可。网站链接:https://www.text2sql.ai/app

首先创建Google Cloud 项目,并创建自己的Bigquery数据库(或者使用公开数据库)

1. setup

# @title Setup
from google.colab import auth
from google.cloud import bigquery
from google.colab import data_table
import requests
import json
import pandas as pd

project = '**********' # 你的project ID
location = 'US' # Location inserted based on the query results selected to explore
client = bigquery.Client(project=project, location=location)
data_table.enable_dataframe_formatter()
auth.authenticate_user()

2. 调用API,获取SQL查询语句

#输入你想实现的命令(自然语言)
prompt = "最近3天的记录都有哪些" # @param{type: "string"}

# API endpoint URL
url = "https://www.text2sql.ai/api/sql/generate"

# 输入自己的token
token = "*******"

# Request headers
headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json"
}

# Request body (prompt and schema)
data = {
    "prompt": prompt ,
    "type": "bigquery",
    "schema": "*******" #填写你的数据库结构
}

# Convert data dictionary to JSON string
payload = json.dumps(data)

try:
    # Make POST request to the API endpoint
    response = requests.post(url, headers=headers, data=payload)

    # Check if request was successful (status code 200)
    if response.status_code == 200:
        # Parse JSON response
        result = response.json()

        # Extract SQL command from the response
        sql_command = result.get("output", "No SQL command generated")

        # Display the generated SQL command
        print("Generated SQL command:")
        print(sql_command)
    else:
        print(f"Request failed with status code {response.status_code}")
except requests.exceptions.RequestException as e:
    print(f"Request error: {e}")

3. Bigquery 查询

query_job = client.query(sql_command)

# 将查询结果转换为 Pandas DataFrame
results_df = query_job.to_dataframe()

# 显示查询结果
display(results_df)

4.生成HTML表格(可选)

html_table = results_df.to_html(index=False)  # 将 DataFrame 转换为 HTML 表格

# 创建 HTML 页面内容
html_content = f"""
<!DOCTYPE html>
<html>
<head>
    <title>Query Results</title>
    <style>
        table {{
            border-collapse: collapse;
            width: 100%;
        }}
        th, td {{
            border: 1px solid black;
            padding: 8px;
            text-align: left;
        }}
        th {{
            background-color: #f2f2f2;
        }}
    </style>
</head>
<body>
    <h2>Query Results</h2>
    {html_table}  <!-- 插入 HTML 表格 -->
</body>
</html>
"""

# 将 HTML 内容保存到文件中
with open('query_results.html', 'w') as f:
    f.write(html_content)

print(html_content)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值