适用于:
Databricks SQL
”Databricks Runtime
Important
此功能在 Beta 版中。
该 ai_search() 函数从一个或多个知识源检索信息。 知识源可以包括 AI搜索索引 或启用 Lakebase 搜索的同步表。
给定一个自然语言查询和为调用配置的知识源,该函数生成优化的搜索查询,检索并取消多个来源的结果,按相关性重新排序,并返回最相关的文档。 默认情况下,它还会在检索到的文档基础上综合出一个扎实的自然语言答案。
用于 ai_search 大规模丰富运营数据的相关上下文,构建批量检索增强生成(RAG)流水线,或将检索作为工具暴露给复合人工智能系统——所有这些都可以通过一次SQL函数调用实现。
数据安全
文档数据在 Databricks 安全外围内进行处理。 Databricks 不存储传递给 AI 函数调用的参数,但保留了元数据的运行细节,例如 Databricks 运行时版本。
要求
- Databricks Runtime 18.2 或更高版本。
- 一个或多个知识来源可供搜索: AI 搜索索引,或启用 Lakebase 搜索的同步表格。
- 要使用Lakebase同步表,先准备搜索。 参见 湖基配置。
- 如果你使用无服务器计算,必须将无服务器环境版本设置为3或更高,这样才能支持诸如
VARIANT。 - 该
ai_search功能可通过 Databricks 笔记本、SQL 编辑器、Databricks 工作流、作业或 Lakeflow 上的 Spark 声明式管道实现。
Syntax
ai_search(query, knowledge_sources [, instructions] [, options])
Arguments
-
query: ASTRING或VARIANT表达式。 自然语言搜索查询。VARIANT输入,例如另一个 AI 函数的输出,在内部被序列化为 JSON 字符串。 -
knowledge_sources: 或VARIANTSTRING表达式,包含用于搜索的知识源配置的 JSON 数组。 参见 知识源配置。 你可以指定最多10个知识来源。 -
instructions:最多可选项STRING表达式,最多可达4,000个字符。 引导查询生成、元数据过滤生成和重新排序的自然语言指令。 例如:'Prefer official documentation over internal articles when both cover the same topic.' -
options:可选MAP<STRING, STRING>的。 支持的密钥:-
'version':要使用的函数版本。 -
'generate_answer':'true'(默认值)或'false'。 当'true'时,函数从检索到的文档中合成一个有基础的自然语言答案,并在现场返回answer。 设置为'false'仅提交文件。 -
'generate_citations':'true'或'false'(默认)。 当'true'时,函数返回引用,显示模型用来支持其生成答案的检索区块。 该选项需要'generate_answer'。'true'
-
知识源配置
参数 knowledge_sources 是一个JSON数组。 每个元素都是一个 {type, config} 包络线。 该 type 字段标识如何 ai_search 连接到源,并且与面向客户的资源名称是分开的。 设置为 type 用于 vector_search AI 搜索索引,或者 lakebase_search 启用 Lakebase 搜索的 Lakebase 同步表。 字段 config 包含源特定配置。
| Key | Required | Description |
|---|---|---|
type |
是的 | 知识来源类型。 其中一种 vector_search (AI搜索索引)或 lakebase_search (启用Lakebase搜索的Lakebase同步表)。 |
config |
是的 | 包含源特定配置的对象。 参见 AI 搜索索引配置 和 vector_searchLakebase 配置lakebase_search。 |
单个呼叫可以合并 vector_search 多个 lakebase_search 源,最多可达10个源的限制。
AI 搜索索引配置
对于设置为 typevector_search的 config AI 搜索索引,接受以下键:
| Key | Required | Description |
|---|---|---|
index_name |
是的 | Unity 目录中 AI 搜索索引的三级名称,例如 catalog.schema.my_index。 |
text_col |
是的 | 索引中包含文档文本的列返回为 page_content。 |
doc_uri_col |
是的 | 索引中包含文档URI的列返回为 doc_uri。 |
filter_columns |
否 | 一个逗号分隔的字符串或JSON列数组,可用于元数据过滤。 省略时,列表是从索引模式中派生的,排除保留、文本和文档的URI列。 |
以下示例将一个AI搜索索引配置为知识来源:
[
{
"type": "vector_search",
"config": {
"index_name": "prod_catalog.docs.support_articles",
"text_col": "article_body",
"doc_uri_col": "article_url",
"filter_columns": "product,language"
}
}
]
要从原始文档构建 AI 搜索索引,可以在 ai_parse_documentai_prep_search Delta 表中创建可搜索的块。 然后从该表格 创建AI搜索索引 。 索引上线后,使用其三层名称为 index_name。
湖基配置
在你使用Lakebase同步表作为知识源之前,先准备好搜索:
- 启用Lakebase Search并安装扩展。 按照 Lakebase Search 在项目设置中启用 Lakebase Search,然后连接到项目的 Postgres 数据库,并通过运行
CREATE EXTENSION IF NOT EXISTS lakebase_vector CASCADE;S 安装扩展。lakebase_vector它提供了近似的最近邻向量搜索。ai_search启用 Lakebase 搜索是不可逆的。 - 把你的 Unity 目录表同步到项目里。 使用 同步表,将嵌入列映射到另一
vector(n)列,并在上面建立lakebase_ann索引。 尺寸n必须与你的嵌入模型输出相匹配。 - 用一个服务端点的模型离线生成嵌入。 在嵌入列中填充嵌入模型,例如通过 查询 嵌入
ai_query模型。 然后将该端点传入为embedding_model(见下文)。ai_search在搜索时将每个查询嵌入其中,因此查询与存储的文档嵌入共享一个向量空间。
对于设 type 为 lakebase_search的 config Lakebase 知识源,接受以下密钥:
| Key | Required | Description |
|---|---|---|
index_name |
是的 | 例如catalog.schema.my_synced_table,Lakebase同步表的Unity Catalog三级名称,用于存储嵌入块。 |
text_col |
是的 | 包含文档文本的表中返回的列为 page_content。 |
doc_uri_col |
是的 | 包含文档URI的表中返回的列为 doc_uri。 |
embedding_column |
是的 | 表格中的嵌入列用于搜索。 |
embedding_model |
是的 | 用于离线嵌入 embedding_column 的服务端点模型。
ai_search 在搜索时用相同的端点嵌入每个查询,因此必须与用于生成存储嵌入的模型匹配。 请参阅以下说明。 |
lakebase_project_id |
是的 | 例如 my-lakebase-project,托管同步表的 Lakebase 数据库项目(实例)名称。 这是项目名称,不是UUID。 |
filter_columns |
否 | 一个逗号分隔的字符串或JSON列数组,可用于元数据过滤。 省略时,列表由表模式导出,排除保留列、文本、文档URI和嵌入列。 设置为空数组([]),即使表中还有其他列,也禁用元数据过滤。 |
Important
embedding_model 必须是您用来生成离线存储嵌入 embedding_column 时所使用的同一个模型服务端点。
ai_search 在查询时将每个查询端点嵌入该端点;如果该端点与生成存储向量的模型不同,查询和文档嵌入会占据不同的向量空间,检索质量会大幅下降。
以下示例将Lakebase表配置为知识源。 用你用来离线嵌入表的端点替换 embedding_model :
[
{
"type": "lakebase_search",
"config": {
"index_name": "prod_catalog.docs.support_articles_synced",
"text_col": "article_body",
"doc_uri_col": "article_url",
"embedding_column": "article_embedding",
"embedding_model": "databricks-gte-large-en",
"lakebase_project_id": "my-lakebase-project"
}
}
]
Returns
VARIANT A,具有如下模式:
{
"document": [
{
"page_content": STRING, // Text content of the retrieved chunk
"doc_uri": STRING, // URI of the source document
"metadata": MAP // Additional metadata from the index
}
],
"answer": STRING, // Grounded answer synthesized from the retrieved
// documents, or null
"citations": [ // Present only when generate_citations is true
{
"document_index": INT // Position in the document array above, starting at 0
}
]
}
| 领域 | 类型 | Description |
|---|---|---|
document |
ARRAY |
检索的文档阵列,按相关性排序。 |
document[].page_content |
STRING |
检索到的片段文本内容。 |
document[].doc_uri |
STRING |
源文档的URI。 |
document[].metadata |
MAP |
索引中的额外元数据。 |
answer |
STRING |
这是从检索到的文件中综合而成的扎实自然语言回答。
null 当答案生成被禁用或未检索任何文档时。 |
citations |
ARRAY |
仅当 generate_citationstrue时才存在。 每个项目都通过其在document_index数组中的 document 基于0的位置()指向返回的块,因此你可以看到模型用了哪些块来支持它的答案。 空数组表示引用生成已运行但未产生引用。 当引用生成不可用或未被调用时,包括 query SQL NULL时,该值null为 。 这些引用是模型尽力选择的支持片段,而非任何具体论点的证据。 |
当 generate_citations 时,将 和 字段一起读answercitations。true 每种数值组合的含义不同:
- 非空
answercitations数组表示已生成答案,但未选择返回的块。 -
answer空citations数组的无引用表示引用生成已运行,但由于没有有基础的答案,因此没有产生引用。 - 空
citations值表示引用生成不可用或未被调用,包括当query是 SQLNULL时。
Examples
基本搜索
以下示例搜索一个AI搜索索引,返回排名文档和有根据的答案:
SELECT ai_search(
'How do I configure auto-scaling for my SQL warehouse?',
PARSE_JSON('[{
"type": "vector_search",
"config": {
"index_name": "prod_catalog.docs.support_articles",
"text_col": "article_body",
"doc_uri_col": "article_url",
"filter_columns": "product,language"
}
}]')
) AS result;
返回生成答案的辅助块
以下示例返回模型选择支持答案的检索块子集:
SELECT ai_search(
'How do I configure auto-scaling for my SQL warehouse?',
PARSE_JSON('[{
"type": "vector_search",
"config": {
"index_name": "prod_catalog.docs.support_articles",
"text_col": "article_body",
"doc_uri_col": "article_url"
}
}]'),
options => map('generate_citations', 'true')
) AS result;
document数组仍然包含完整的返回区块集合。 在每个引用中用 Multiple document_index 从该数组中选出支持的。 这些指标仅在单一回答中有效。 它们可以在调用之间、重新排序变化或索引更新时发生变化。
多源搜索含说明
以下示例搜索两个AI搜索索引,并用于 instructions 引导查询生成和重新排序:
SELECT ai_search(
'What are the networking requirements for serverless SQL warehouses?',
PARSE_JSON('[
{
"type": "vector_search",
"config": {"index_name": "prod_catalog.docs.public_docs", "text_col": "content", "doc_uri_col": "doc_url"}
},
{
"type": "vector_search",
"config": {"index_name": "prod_catalog.docs.internal_kb", "text_col": "body", "doc_uri_col": "source_uri"}
}
]'),
'Focus on firewall rules and VPC/VNet configuration. Prefer official documentation over internal articles when both cover the same topic.'
) AS result;
搜索湖底表
以下示例搜索一个Lakebase同步表,返回排名文档和有根据的答案:
SELECT ai_search(
'How do I configure auto-scaling for my SQL warehouse?',
PARSE_JSON('[{
"type": "lakebase_search",
"config": {
"index_name": "prod_catalog.docs.support_articles_synced",
"text_col": "article_body",
"doc_uri_col": "article_url",
"embedding_column": "article_embedding",
"embedding_model": "databricks-gte-large-en",
"lakebase_project_id": "my-lakebase-project"
}
}]')
) AS result;
将AI搜索索引和湖底表结合起来
以下示例在同一调用中搜索 AI 搜索索引和 Lakebase 表。 两个来源的结果会被去重,重新排序,并作为一个排名列表返回:
SELECT ai_search(
'What are the networking requirements for serverless SQL warehouses?',
PARSE_JSON('[
{
"type": "vector_search",
"config": {"index_name": "prod_catalog.docs.public_docs", "text_col": "content", "doc_uri_col": "doc_url"}
},
{
"type": "lakebase_search",
"config": {
"index_name": "prod_catalog.docs.internal_kb_synced",
"text_col": "body",
"doc_uri_col": "source_uri",
"embedding_column": "body_embedding",
"embedding_model": "databricks-gte-large-en",
"lakebase_project_id": "my-lakebase-project"
}
}
]')
) AS result;
丰富表格,提供检索和生成答案
以下示例将为每个支持工单提供相关文档和建议解决方案。 由于答案生成默认开启,建议分辨率直接在 answer 现场获取——无需单独生成步骤。
SELECT
ticket_id,
customer_description,
ai_search(
customer_description,
PARSE_JSON('[{
"type": "vector_search",
"config": {
"index_name": "support.docs.product_documentation",
"text_col": "content",
"doc_uri_col": "doc_url"
}
}]'),
'Find product documentation, known issues, and troubleshooting guides relevant to this support ticket.'
):answer::STRING AS suggested_resolution
FROM support.tickets.open_tickets;
要控制输出格式或使用特定模型,可以设置为'generate_answer''false'并将检索到的文档串联为 。ai_query
局限性
-
ai_search支持AI搜索索引("type": "vector_search")和Lakebase同步表("type": "lakebase_search")作为知识源。 - 你可以在每次调用中指定最多10个知识源,且支持的类型可以任意组合。
-
instructions论证限于4000字符。