|
1 | | ---- |
2 | | -title: 'Databend AI 功能' |
3 | | -sidebar_label: 'AI 功能' |
4 | | ---- |
| 1 | +# Databend AI 功能 |
5 | 2 |
|
6 | | -本指南邀请您探索 Databend 的内置函数与机器学习融合的领域。通过 SQL 查询轻松转换您的数据分析,揭示一系列自然语言任务 - 从理解文档到完成文本等等。 |
| 3 | +本指南介绍了 Databend 的内置 AI 函数,这些函数通过 SQL 查询实现自然语言处理任务,包括文本理解、生成等。 |
7 | 4 |
|
8 | | -## 数据、隐私和安全 |
| 5 | +:::warning |
| 6 | +数据隐私和安全 |
9 | 7 |
|
10 | | -Databend 依赖 [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/openai-service) 进行嵌入和文本补全,这意味着您的数据将被发送到 Azure OpenAI Service。使用这些功能时请务必谨慎。 |
| 8 | +Databend 使用 Azure OpenAI Service 进行嵌入和文本补全。当您使用这些函数时,您的数据将被发送到 Azure OpenAI。这些功能在 Databend Cloud 上默认可用。 |
11 | 9 |
|
12 | | -这些功能在 [Databend Cloud](https://databend.com) 上默认可用,使用我们的 Azure OpenAI 密钥。**如果您使用它们,您承认您的数据将被发送到 Azure OpenAI Service**,并且您同意 [Azure OpenAI 数据隐私](https://learn.microsoft.com/en-us/legal/cognitive-services/openai/data-privacy)。 |
| 10 | +**通过使用这些函数,您承认您的数据将被发送到 Azure OpenAI Service** 并同意 [Azure OpenAI 数据隐私](https://learn.microsoft.com/en-us/legal/cognitive-services/openai/data-privacy) 条款。 |
| 11 | +::: |
13 | 12 |
|
14 | | -## 什么是嵌入 (Embeddings)? |
15 | | - |
16 | | -嵌入是文本数据的向量表示,它捕获原始文本的语义和上下文含义。它们可用于比较和分析各种自然语言处理任务中的文本,例如文档相似性、聚类和推荐系统。 |
17 | | - |
18 | | -为了说明嵌入的工作原理,让我们考虑一个简单的例子。假设我们有以下句子: |
19 | | -1. `"The cat sat on the mat."` |
20 | | -2. `"The dog sat on the rug."` |
21 | | -3. `"The quick brown fox jumped over the lazy dog."` |
22 | | - |
23 | | -当为这些句子创建嵌入时,模型会将文本转换为高维向量,使得相似的句子在向量空间中更接近。 |
24 | | - |
25 | | -例如,句子 1 和句子 2 的嵌入将彼此更接近,因为它们共享相似的结构和含义(都涉及动物坐在某物上)。另一方面,句子 3 的嵌入将与句子 1 和句子 2 的嵌入相距更远,因为它具有不同的结构和含义。 |
26 | | - |
27 | | -嵌入可能如下所示(为说明目的而简化): |
| 13 | +## 关键 AI 函数 |
28 | 14 |
|
29 | | -1. `[0.2, 0.3, 0.1, 0.7, 0.4]` |
30 | | -2. `[0.25, 0.29, 0.11, 0.71, 0.38]` |
31 | | -3. `[-0.1, 0.5, 0.6, -0.3, 0.8]` |
| 15 | +| 函数 | 描述 | 何时使用 | |
| 16 | +|----------|-------------|------------| |
| 17 | +| [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion) | 根据提示生成文本 | • 内容生成<br/>• 问题解答<br/>• 摘要<br/>• 文本扩展 | |
| 18 | +| [ai_embedding_vector](/sql/sql-functions/ai-functions/ai-embedding-vector) | 将文本转换为向量表示 | • 语义搜索<br/>• 文档相似度<br/>• 内容推荐<br/>• 文本分类 | |
| 19 | +| [cosine_distance](/sql/sql-functions/vector-distance-functions/vector-cosine-distance) | 计算向量之间的相似度 | • 查找相似文档<br/>• 对搜索结果进行排序<br/>• 衡量文本相似度 | |
32 | 20 |
|
33 | | -在这个简化的例子中,您可以看到句子 1 和句子 2 的嵌入在向量空间中彼此更接近,而句子 3 的嵌入则更远。这说明了嵌入如何捕获语义关系,并可用于比较和分析文本数据。 |
34 | | - |
35 | | -## 什么是向量数据库? |
36 | | - |
37 | | -通常,嵌入向量存储在专门的向量数据库中,如 milvus、pinecone、qdrant 或 weaviate。Databend 还可以使用 ARRAY(FLOAT32) 数据类型存储嵌入向量,并使用 SQL 中的 cosine_distance 函数执行相似性计算。要使用 Databend 为文本文档创建嵌入,您可以直接在 SQL 查询中使用内置的 `ai_embedding_vector` 函数。 |
| 21 | +## 什么是嵌入 (Embeddings)? |
38 | 22 |
|
39 | | -## Databend AI 函数 |
| 23 | +嵌入是文本的向量表示,可以捕获语义。相似的文本在嵌入空间中具有更接近的向量,从而可以进行比较和分析,以执行诸如文档相似性和聚类之类的任务。 |
40 | 24 |
|
41 | | -Databend 提供了内置的 AI 函数,用于各种自然语言处理任务。本文档中涵盖的主要函数包括: |
| 25 | +## Databend 中的向量存储 |
42 | 26 |
|
43 | | -- [ai_embedding_vector](/sql/sql-functions/ai-functions/ai-embedding-vector): 为文本文档生成嵌入。 |
44 | | -- [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion): 根据给定的提示生成文本补全。 |
45 | | -- [cosine_distance](/sql/sql-functions/vector-distance-functions/vector-cosine-distance): 计算两个嵌入之间的余弦距离。 |
| 27 | +Databend 可以使用 `ARRAY(FLOAT NOT NULL)` 数据类型存储嵌入向量,并直接在 SQL 中使用 cosine_distance 函数执行相似度计算。 |
46 | 28 |
|
47 | | -## 生成嵌入 |
| 29 | +## 示例:文档相似度搜索 |
48 | 30 |
|
49 | | -让我们创建一个表来存储一些示例文本文档及其相应的嵌入: |
50 | 31 | ```sql |
| 32 | +-- 创建一个用于存储文档的表 |
51 | 33 | CREATE TABLE articles ( |
52 | 34 | id INT, |
53 | 35 | title VARCHAR, |
54 | 36 | content VARCHAR, |
55 | | - embedding ARRAY(FLOAT32) |
| 37 | + embedding ARRAY(FLOAT NOT NULL) |
56 | 38 | ); |
57 | | -``` |
58 | 39 |
|
59 | | -现在,让我们将一些示例文档插入到表中: |
60 | | -```sql |
| 40 | +-- 插入带有嵌入的文档 |
61 | 41 | INSERT INTO articles (id, title, content, embedding) |
62 | 42 | VALUES |
63 | | - (1, 'Python for Data Science', 'Python is a versatile programming language widely used in data science...', ai_embedding_vector('Python is a versatile programming language widely used in data science...')), |
64 | | - (2, 'Introduction to R', 'R is a popular programming language for statistical computing and graphics...', ai_embedding_vector('R is a popular programming language for statistical computing and graphics...')), |
65 | | - (3, 'Getting Started with SQL', 'Structured Query Language (SQL) is a domain-specific language used for managing relational databases...', ai_embedding_vector('Structured Query Language (SQL) is a domain-specific language used for managing relational databases...')); |
66 | | -``` |
| 43 | + (1, 'Python for Data Science', 'Python 是一种通用的编程语言...', |
| 44 | + ai_embedding_vector('Python 是一种通用的编程语言...')), |
| 45 | + (2, 'Introduction to R', 'R 是一种流行的统计编程语言...', |
| 46 | + ai_embedding_vector('R 是一种流行的统计编程语言...')); |
67 | 47 |
|
68 | | -## 计算余弦距离 |
69 | | - |
70 | | -现在,让我们使用 [cosine_distance](/sql/sql-functions/vector-distance-functions/vector-cosine-distance) 函数找到与给定查询最相似的文档: |
71 | | -```sql |
| 48 | +-- 查找与查询相似的文档 |
72 | 49 | SELECT |
73 | | - id, |
74 | | - title, |
75 | | - content, |
76 | | - cosine_distance(embedding, ai_embedding_vector('How to use Python in data analysis?')) AS similarity |
77 | | -FROM |
78 | | - articles |
79 | | -ORDER BY |
80 | | - similarity ASC |
81 | | - LIMIT 3; |
82 | | -``` |
83 | | - |
84 | | -结果: |
85 | | -```sql |
86 | | -+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+ |
87 | | -| id | title | content | similarity | |
88 | | -+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+ |
89 | | -| 1 | Python for Data Science | Python is a versatile programming language widely used in data science... | 0.1142081 | |
90 | | -| 2 | Introduction to R | R is a popular programming language for statistical computing and graphics... | 0.18741018 | |
91 | | -| 3 | Getting Started with SQL | Structured Query Language (SQL) is a domain-specific language used for managing relational databases... | 0.25137568 | |
92 | | -+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+ |
| 50 | + id, title, content, |
| 51 | + cosine_distance(embedding, ai_embedding_vector('如何在数据分析中使用 Python?')) AS similarity |
| 52 | +FROM articles |
| 53 | +ORDER BY similarity ASC |
| 54 | +LIMIT 3; |
93 | 55 | ``` |
94 | 56 |
|
95 | | -## 生成文本补全 |
96 | | - |
97 | | -Databend 还支持文本补全函数 [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion)。 |
98 | | - |
99 | | -例如,从上面的输出中,我们选择余弦距离最小的文档:“Python is a versatile programming language widely used in data science...”。 |
100 | | - |
101 | | -我们可以将其用作上下文,并将原始问题提供给 [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion) 函数以生成补全: |
| 57 | +## 示例:文本补全 |
102 | 58 |
|
103 | 59 | ```sql |
104 | | -SELECT ai_text_completion('Python is a versatile programming language widely used in data science...') AS completion; |
| 60 | +-- 为提示生成补全 |
| 61 | +SELECT ai_text_completion('用三点解释云数仓的优势:') AS completion; |
| 62 | + |
| 63 | +-- 结果可能是: |
| 64 | +-- 1. 可扩展性:云数仓可以根据需求轻松地向上或向下扩展, |
| 65 | +-- 无需预先进行容量规划。 |
| 66 | +-- 2. 成本效益:按需付费的定价模式降低了资本支出, |
| 67 | +-- 并允许企业仅为其使用的资源付费。 |
| 68 | +-- 3. 可访问性:云数仓使团队可以从任何地方访问数据, |
| 69 | +-- 从而促进远程工作和全球协作。 |
105 | 70 | ``` |
106 | 71 |
|
107 | | -结果: |
108 | | -```sql |
109 | | - |
110 | | -completion: and machine learning. It is known for its simplicity, readability, and ease of use. Python has a vast collection of libraries and frameworks that make it easy to perform complex tasks such as data analysis, visualization, and machine learning. Some of the popular libraries used in data science include NumPy, Pandas, Matplotlib, and Scikit-learn. Python is also used in web development, game development, and automation. Its popularity and versatility make it a valuable skill for programmers and data scientists. |
111 | | -``` |
| 72 | +## 构建 AI 问答系统 |
112 | 73 |
|
113 | | -您可以在我们的 [Databend Cloud](https://databend.com) 上体验这些功能,您可以在这里注册免费试用版并立即开始使用这些 AI 功能。 |
| 74 | +您可以使用 Databend 创建一个简单的问答系统,方法是: |
| 75 | +1. 存储带有嵌入的文档 |
| 76 | +2. 查找与问题相关的文档 |
| 77 | +3. 使用文本补全生成答案 |
114 | 78 |
|
115 | | -Databend 的 AI 功能旨在易于使用,即使对于不熟悉机器学习或自然语言处理的用户也是如此。借助 Databend,您可以快速轻松地将强大的 AI 功能添加到您的 SQL 查询中,并将您的数据分析提升到一个新的水平。 |
116 | | - |
117 | | -## 使用 Databend 构建 AI 问答系统 |
118 | | - |
119 | | -我们利用 [Databend Cloud](https://databend.com) 和 AI 函数为我们的文档构建了一个 AI 问答系统。 |
120 | | - |
121 | | -以下是构建它的分步指南: |
122 | | - |
123 | | -### 步骤 1:创建表 |
124 | | - |
125 | | -首先,创建一个具有以下结构的表来存储文档信息和嵌入: |
126 | | -```sql |
127 | | -CREATE TABLE doc ( |
128 | | - path VARCHAR, |
129 | | - content VARCHAR, |
130 | | - embedding ARRAY(FLOAT32 NOT NULL) |
131 | | -); |
132 | | -``` |
133 | | - |
134 | | -### 步骤 2:插入原始数据 |
135 | | - |
136 | | -将示例数据插入到表中,包括每个文档的路径和内容: |
137 | | -```sql |
138 | | -INSERT INTO doc (path, content) VALUES |
139 | | - ('ai-function', 'ai_embedding_vector, ai_text_completion, cosine_distance'), |
140 | | - ('string-function', 'ASCII, BIN, CHAR_LENGTH'); |
141 | | -``` |
142 | | - |
143 | | -### 步骤 3:生成嵌入 |
144 | | - |
145 | | -更新表以使用 [ai_embedding_vector](/sql/sql-functions/ai-functions/ai-embedding-vector) 函数为内容生成嵌入: |
146 | | -```sql |
147 | | -UPDATE doc SET embedding = ai_embedding_vector(content) |
148 | | -WHERE LENGTH(embedding) = 0; |
149 | | -``` |
150 | | - |
151 | | -### 步骤 4:提问并检索相关答案 |
152 | | - |
153 | | -```sql |
154 | | --- 将问题定义为 CTE(公共表表达式) |
155 | | -WITH question AS ( |
156 | | - SELECT 'Tell me the ai functions' AS q |
157 | | -), |
158 | | --- 计算问题的嵌入向量 |
159 | | -question_embedding AS ( |
160 | | - SELECT ai_embedding_vector((SELECT q FROM question)) AS q_vector |
161 | | -), |
162 | | --- 检索前 3 个最相关的文档 |
163 | | -top_3_docs AS ( |
164 | | - SELECT content, |
165 | | - cosine_distance((SELECT q_vector FROM question_embedding), embedding) AS dist |
166 | | - FROM doc |
167 | | - ORDER BY dist ASC |
168 | | - LIMIT 3 |
169 | | -), |
170 | | --- 合并前 3 个文档的内容 |
171 | | -combined_content AS ( |
172 | | - SELECT string_agg(content, ' ') AS aggregated_content |
173 | | - FROM top_3_docs |
174 | | -), |
175 | | --- 连接自定义提示、合并的内容和原始问题 |
176 | | -prompt AS ( |
177 | | - SELECT CONCAT( |
178 | | - 'Utilizing the sections provided from the Databend documentation, answer the questions to the best of your ability. ', |
179 | | - 'Documentation sections: ', |
180 | | - (SELECT aggregated_content FROM combined_content), |
181 | | - ' Question: ', |
182 | | - (SELECT q FROM question) |
183 | | - ) as p |
184 | | -) |
185 | | --- 将连接的文本传递给 ai_text_completion 函数以生成连贯且相关的响应 |
186 | | -SELECT ai_text_completion((SELECT p FROM prompt)) AS answer; |
187 | | -``` |
188 | | - |
189 | | -结果: |
190 | | -```sql |
191 | | -+------------------------------------------------------------------------------------------------------------------+ |
192 | | -| answer | |
193 | | -+------------------------------------------------------------------------------------------------------------------+ |
194 | | -| Answer: The ai functions mentioned in the Databend documentation are ai_embedding_vector and ai_text_completion. | |
195 | | -+------------------------------------------------------------------------------------------------------------------+ |
196 | | -``` |
| 79 | +在 [Databend Cloud](https://databend.com) 上通过免费试用体验这些 AI 功能。 |
0 commit comments