I Built an AI Data Agent Which Can Query Data and Answer Business Questions. Here’s How.

I Built an AI Data Agent Which Can Query Data and Answer Business Questions. Here’s How.

我构建了一个可以查询数据并回答业务问题的 AI 数据代理,以下是具体实现方法。

A step-by-step guide to building a data agent and conversational interface that let business users to explore data in natural language without SQL. 这是一份关于构建数据代理和对话式界面的分步指南,它让业务用户无需掌握 SQL,即可通过自然语言探索数据。


In my last article, Many Companies Use AI. Few Know How to Build an AI-Native Enterprise Data Platform, I discussed how to integrate AI into the enterprise data platforms. I also shared common problems in practice regarding AI applications in data engineering workflows and how to resolve them. 在我的上一篇文章《许多公司都在使用 AI,但很少有人知道如何构建 AI 原生企业数据平台》中,我讨论了如何将 AI 集成到企业数据平台中。我还分享了数据工程工作流中 AI 应用的常见问题以及解决方法。

In that article, I explained 3 key elements of a practical enterprise AI architecture – data agents, AI-powered QA and AI governance. In order to deep dive data agents, I created a demo called the Avocado Sales Analytics Agent. In this article, I’ll walk through the complete process of building it step by step. 在那篇文章中,我解释了实用企业 AI 架构的三个关键要素:数据代理、AI 驱动的问答系统以及 AI 治理。为了深入探讨数据代理,我创建了一个名为“牛油果销售分析代理”的演示项目。在本文中,我将逐步演示构建它的完整过程。


What Is a Data Agent?

什么是数据代理?

A data agent is an AI-powered conversational interface that enables business users to ask questions in plain language and receive accurate answers by querying data stored in a data warehouse. Instead of waiting for data analysts to write complex SQL queries and generate reports, users can simply type: “How much is total TPV in Southeast Asia last year?” and get an immediate answer like “$ 60 Billion”. 数据代理是一种由 AI 驱动的对话式界面,它使业务用户能够通过自然语言提问,并从数据仓库中查询数据以获得准确答案。用户无需等待数据分析师编写复杂的 SQL 查询并生成报告,只需输入:“去年东南亚的总交易额(TPV)是多少?”即可立即获得类似“600 亿美元”的回答。


Choosing the Right Approach

选择正确的方法

There are two ways to build a data agent. The first approach is to build from scratch with open-source orchestration frameworks such as LangGraph/LangChain, CrewAI and LlamaIndex. With this approach, you have complete control over the agent’s memory structures, strict business logic rules, and complex multi-agent execution loops. 构建数据代理有两种方式。第一种是从零开始,使用 LangGraph/LangChain、CrewAI 和 LlamaIndex 等开源编排框架进行构建。通过这种方式,你可以完全控制代理的内存结构、严格的业务逻辑规则以及复杂的多代理执行循环。

For beginners, the second approach of deploying a data agent within a cloud data platforms is more practical and faster to implement. Today, most major cloud data platforms provide native, out-of-the-box data agents. For example, Snowflake offers Snowflake Cortex Agents, Databricks Genie is the managed conversational data intelligence tool, and Microsoft Fabric Ecosystem has Fabric Data Agents. 对于初学者来说,第二种方法——在云数据平台内部署数据代理——更为实用且实现速度更快。如今,大多数主流云数据平台都提供原生的、开箱即用的数据代理。例如,Snowflake 提供 Snowflake Cortex Agents,Databricks Genie 是其生态系统内的托管式对话数据智能工具,而 Microsoft Fabric 生态系统则拥有 Fabric Data Agents。

To build the Avocado Sales Analytics Agent demo, I chose Google Cloud Platform (BigQuery) because it provides full access to its Conversational Analytics features during the free trial, and it can be set up easily using a personal Google account. For the source data, I used the Avocado Prices dataset from Kaggle. 为了构建“牛油果销售分析代理”演示,我选择了 Google Cloud Platform (BigQuery),因为它在免费试用期间提供了对其对话式分析功能的完全访问权限,并且可以使用个人 Google 账号轻松设置。对于源数据,我使用了 Kaggle 上的“牛油果价格”数据集。


Building a Data Agent With No Code

无代码构建数据代理

Google Cloud’s BigQuery provides the Conversational Analytics API, which enables us to build conversational data agents on top of BigQuery datasets. Before building the data agent, the first step is to download the Avocado Prices CSV file from Kaggle and upload it to BigQuery. After uploading the dataset, it’s very important to understand the data schema of the tables that are used to build the agent because the agent needs to understand the data model—including table names, column names, data types, relationships, and business meanings. Google Cloud 的 BigQuery 提供了对话式分析 API,使我们能够在 BigQuery 数据集之上构建对话式数据代理。在构建代理之前,第一步是从 Kaggle 下载牛油果价格 CSV 文件并将其上传到 BigQuery。上传数据集后,理解用于构建代理的表结构至关重要,因为代理需要理解数据模型——包括表名、列名、数据类型、关系和业务含义。

The next step is to build the data agent. You can navigate to BigQuery -> Agent, click “Create Agent”, then enter the agent name and description before selecting your dataset as the Knowledge source. The instructions are the most critical part because it guides the AI to query data correctly, avoid errors, and give accurate answers. 下一步是构建数据代理。你可以导航到 BigQuery -> Agent,点击“创建代理”,输入代理名称和描述,然后选择你的数据集作为知识源。指令(Instructions)是最关键的部分,因为它引导 AI 正确查询数据、避免错误并给出准确的答案。

Here are the principles of writing good instructions: 以下是编写良好指令的原则:

  • Be clear: Use simple, precise language. Do not use hard or vague phrases.
  • 保持清晰: 使用简单、精确的语言。不要使用晦涩或模糊的短语。
  • Give examples: Show the agent what good queries and responses look like.
  • 提供示例: 向代理展示什么是好的查询和响应。
  • Set boundaries: Specify what the agent should and should not do.
  • 设定边界: 明确代理应该做什么和不应该做什么。
  • Define the role: Clearly describe who the agent is and who the users are.
  • 定义角色: 清晰描述代理的身份以及用户的身份。

(The article continues with specific SQL rules and metric definitions for the agent…) (文章后续部分详细列出了该代理的 SQL 规则和指标定义……)