GPT Actions 库 - BigQuery

2024年7月9日
在 Github 中打开

这个特定的 GPT Action 概述了如何连接到 Google BigQuery,即 Google Cloud 的分析数据仓库。此 Action 接收用户的问题,扫描相关表以收集数据模式,然后编写 SQL 查询来回答用户的问题。

注意:这些说明返回的是可用的 SQL 语句,而不是结果本身。目前需要中间件才能返回 CSV 文件 – 我们将尽快发布关于此示例的说明

价值:用户现在可以利用 ChatGPT 的自然语言能力直接连接到 BigQuery 的 DWH。

示例用例:

  • 数据科学家可以使用 ChatGPT 的数据分析功能连接到表并运行数据分析
  • 公民数据用户可以询问有关其交易数据的基本问题
  • 用户可以更好地了解他们的数据和潜在的异常情况

在开始之前,请确保在您的应用环境中完成以下步骤

  • 设置 GCP 项目
  • 在该 GCP 项目中设置 BQ 数据集
  • 确保通过 ChatGPT 验证身份以访问 BigQuery 的用户有权访问该 BQ 数据集

创建自定义 GPT 后,将以下文本复制到“Instructions”面板中。有疑问?请查看入门示例,详细了解此步骤的工作原理。

**Context**: You are an expert at writing BigQuery SQL queries. A user is going to ask you a question. 

**Instructions**:
1. No matter the user's question, start by running `runQuery` operation using this query: "SELECT column_name, table_name, data_type, description FROM `{project}.{dataset}.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS`" 
-- Assume project = "<insert your default project here>", dataset = "<insert your default dataset here>", unless the user provides different values 
-- Remember to include useLegacySql:false in the json output
2. Convert the user's question into a SQL statement that leverages the step above and run the `runQuery` operation on that SQL statement to confirm the query works. Add a limit of 100 rows
3. Now remove the limit of 100 rows and return back the query for the user to see

**Additional Notes**: If the user says "Let's get started", explain that the user can provide a project or dataset, along with a question they want answered. If the user has no ideas, suggest that we have a sample flights dataset they can query - ask if they want you to query that

创建自定义 GPT 后,将以下文本复制到“Actions”面板中。有疑问?请查看入门示例,详细了解此步骤的工作原理。

openapi: 3.1.0
info:
  title: BigQuery API
  description: API for querying a BigQuery table.
  version: 1.0.0
servers:
  - url: https://bigquery.googleapis.com/bigquery/v2
    description: Google BigQuery API server
paths:
  /projects/{projectId}/queries:
    post:
      operationId: runQuery
      summary: Executes a query on a specified BigQuery table.
      description: Submits a query to BigQuery and returns the results.
      x-openai-isConsequential: false
      parameters:
        - name: projectId
          in: path
          required: true
          description: The ID of the Google Cloud project.
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                query:
                  type: string
                  description: The SQL query string.
                useLegacySql:
                  type: boolean
                  description: Whether to use legacy SQL.
                  default: false
      responses:
        '200':
          description: Successful query execution.
          content:
            application/json:
              schema:
                type: object
                properties:
                  kind:
                    type: string
                    example: "bigquery#queryResponse"
                  schema:
                    type: object
                    description: The schema of the results.
                  jobReference:
                    type: object
                    properties:
                      projectId:
                        type: string
                      jobId:
                        type: string
                  rows:
                    type: array
                    items:
                      type: object
                      properties:
                        f:
                          type: array
                          items:
                            type: object
                            properties:
                              v:
                                type: string
                  totalRows:
                    type: string
                    description: Total number of rows in the query result.
                  pageToken:
                    type: string
                    description: Token for pagination of query results.
        '400':
          description: Bad request. The request was invalid.
        '401':
          description: Unauthorized. Authentication is required.
        '403':
          description: Forbidden. The request is not allowed.
        '404':
          description: Not found. The specified resource was not found.
        '500':
          description: Internal server error. An error occurred while processing the request.

以下是关于如何使用此第三方应用设置身份验证的说明。有疑问?请查看入门示例,详细了解此步骤的工作原理。

在 ChatGPT 中设置身份验证之前,请在应用中执行以下步骤。

  • 转到 Google Cloud Console
  • 导航到 API 和服务 > 凭据
  • 创建新的 OAuth 凭据(或使用现有的凭据)
  • 找到您的 OAuth 客户端 ID 和客户端密钥,并安全地存储这两个值(请参见下面的屏幕截图)

gptactions_BigQuery_auth.png

在 ChatGPT 中,单击“身份验证”并选择 “OAuth”。输入以下信息。

在 ChatGPT 中设置身份验证后,请按照应用中的以下步骤完成 Action。

  • 从 GPT Action 复制回调 URL
  • 在“授权重定向 URI”(请参见上面的屏幕截图)中,添加您的回调 URL
  • 回调 URL 错误: 如果您在 ChatGPT 中收到回调 URL 错误,请密切注意上面的屏幕截图。您需要将回调 URL 直接添加到 GCP 中,以便操作能够正确验证身份
  • 架构调用了错误的项目或数据集: 如果 ChatGPT 调用了错误的项目或数据集,请考虑更新您的说明,使其更明确地说明 (a) 应该调用哪个项目/数据集,或者 (b) 要求用户在运行查询之前提供这些确切的详细信息

您希望我们优先考虑哪些集成?我们的集成中是否存在错误?在我们的 github 中提交 PR 或 issue,我们会查看。