Skip to content

Commit 90905e1

Browse files
authored
add ai functions to guides (#283)
1 parent 035f564 commit 90905e1

File tree

4 files changed

+207
-222
lines changed

4 files changed

+207
-222
lines changed

Diff for: docs/guides/40-load-data/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Databend offers several easy ways to load data; this guide will show you how to
66

77
import IndexOverviewList from '@site/src/components/IndexOverviewList';
88

9-
<IndexOverviewList />
9+
<IndexOverviewList />

Diff for: docs/guides/51-ai-functions/_category_.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "AI-Based Functions"
3+
}

Diff for: docs/guides/51-ai-functions/index.md

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
title: 'Databend AI-Based Functions'
3+
sidebar_label: 'AI-Based Functions'
4+
---
5+
6+
## What are Databend AI functions?
7+
8+
Databend AI functions are built-in functions that use machine learning to perform various natural language processing tasks, such as document embeddings, text completions, and more.
9+
10+
These functions can be used in SQL queries to add powerful AI capabilities to your data analysis.
11+
12+
## Data, Privacy, and Security
13+
14+
Databend relies on [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/ai-services/openai-service) for embeddings and text completions, which means your data will be sent to Azure OpenAI Service. Exercise caution when using these functions.
15+
16+
These functions are available by default on [Databend Cloud](https://databend.com) using our Azure OpenAI key. **If you use them, you acknowledge that your data will be sent to Azure OpenAI Service**, and you agree to the [Azure OpenAI Data Privacy](https://learn.microsoft.com/en-us/legal/cognitive-services/openai/data-privacy).
17+
18+
## What are Embeddings?
19+
20+
Embeddings are vector representations of text data that capture the semantic meaning and context of the original text. They can be used to compare and analyze a text in various natural language processing tasks, such as document similarity, clustering, and recommendation systems.
21+
22+
To illustrate how embeddings work, let's consider a simple example. Suppose we have the following sentences:
23+
1. `"The cat sat on the mat."`
24+
2. `"The dog sat on the rug."`
25+
3. `"The quick brown fox jumped over the lazy dog."`
26+
27+
When creating embeddings for these sentences, the model will convert the text into high-dimensional vectors in such a way that similar sentences are closer together in the vector space.
28+
29+
For instance, the embeddings of sentences 1 and 2 will be closer to each other because they share a similar structure and meaning (both involve an animal sitting on something). On the other hand, the embedding of sentence 3 will be farther from the embeddings of sentences 1 and 2 because it has a different structure and meaning.
30+
31+
The embeddings could look like this (simplified for illustration purposes):
32+
33+
1. `[0.2, 0.3, 0.1, 0.7, 0.4]`
34+
2. `[0.25, 0.29, 0.11, 0.71, 0.38]`
35+
3. `[-0.1, 0.5, 0.6, -0.3, 0.8]`
36+
37+
In this simplified example, you can see that the embeddings of sentences 1 and 2 are closer to each other in the vector space, while the embedding of sentence 3 is farther away. This illustrates how embeddings can capture semantic relationships and be used to compare and analyze text data.
38+
39+
## What is a Vector Database?
40+
41+
Typically, embedding vectors are stored in specialized vector databases like milvus, pinecone, qdrant, or weaviate. Databend can also store embedding vectors using the ARRAY(FLOAT32) data type and perform similarity computations with the cosine_distance function in SQL. To create embeddings for a text document using Databend, you can use the built-in `ai_embedding_vector` function directly in your SQL query.
42+
43+
## Databend AI Functions
44+
45+
Databend provides built-in AI functions for various natural language processing tasks. The main functions covered in this document are:
46+
47+
- [ai_embedding_vector](/sql/sql-functions/ai-functions/ai-embedding-vector): Generates embeddings for text documents.
48+
- [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion): Generates text completions based on a given prompt.
49+
- [cosine_distance](/sql/sql-functions/ai-functions/ai-cosine-distance): Calculates the cosine distance between two embeddings.
50+
51+
## Generating Embeddings
52+
53+
Let's create a table to store some sample text documents and their corresponding embeddings:
54+
```sql
55+
CREATE TABLE articles (
56+
id INT,
57+
title VARCHAR,
58+
content VARCHAR,
59+
embedding ARRAY(FLOAT32)
60+
);
61+
```
62+
63+
Now, let's insert some sample documents into the table:
64+
```sql
65+
INSERT INTO articles (id, title, content, embedding)
66+
VALUES
67+
(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...')),
68+
(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...')),
69+
(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...'));
70+
```
71+
72+
## Calculating Cosine Distance
73+
74+
Now, let's find the documents that are most similar to a given query using the [cosine_distance](/sql/sql-functions/ai-functions/ai-cosine-distance) function:
75+
```sql
76+
SELECT
77+
id,
78+
title,
79+
content,
80+
cosine_distance(embedding, ai_embedding_vector('How to use Python in data analysis?')) AS similarity
81+
FROM
82+
articles
83+
ORDER BY
84+
similarity ASC
85+
LIMIT 3;
86+
```
87+
88+
Result:
89+
```sql
90+
+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+
91+
| id | title | content | similarity |
92+
+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+
93+
| 1 | Python for Data Science | Python is a versatile programming language widely used in data science... | 0.1142081 |
94+
| 2 | Introduction to R | R is a popular programming language for statistical computing and graphics... | 0.18741018 |
95+
| 3 | Getting Started with SQL | Structured Query Language (SQL) is a domain-specific language used for managing relational databases... | 0.25137568 |
96+
+------+--------------------------+---------------------------------------------------------------------------------------------------------+------------+
97+
```
98+
99+
## Generating Text Completions
100+
101+
Databend also supports a text completion function, [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion).
102+
103+
For example, from the above output, we choose the document with the smallest cosine distance: "Python is a versatile programming language widely used in data science...".
104+
105+
We can use this as context and provide the original question to the [ai_text_completion](/sql/sql-functions/ai-functions/ai-text-completion) function to generate a completion:
106+
107+
```sql
108+
SELECT ai_text_completion('Python is a versatile programming language widely used in data science...') AS completion;
109+
```
110+
111+
Result:
112+
```sql
113+
114+
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.
115+
```
116+
117+
You can experience these functions on our [Databend Cloud](https://databend.com), where you can sign up for a free trial and start using these AI functions right away.
118+
119+
Databend's AI functions are designed to be easy to use, even for users who are not familiar with machine learning or natural language processing. With Databend, you can quickly and easily add powerful AI capabilities to your SQL queries and take your data analysis to the next level.
120+
121+
## Build an AI Q&A System with Databend
122+
123+
We have utilized [Databend Cloud](https://databend.com) and AI functions to build an AI Q&A system for our documentation. You can try it out at https://ask.databend.rs.
124+
125+
Here's a step-by-step guide to how https://ask.databend.rs was built:
126+
127+
### Step 1: Create Table
128+
129+
First, create a table with the following structure to store document information and embeddings:
130+
```sql
131+
CREATE TABLE doc (
132+
path VARCHAR,
133+
content VARCHAR,
134+
embedding ARRAY(FLOAT32)
135+
);
136+
```
137+
138+
### Step 2: Insert Raw Data
139+
140+
Insert sample data into the table, including the path and content for each document:
141+
```sql
142+
INSERT INTO doc (path, content) VALUES
143+
('ai-function', 'ai_embedding_vector, ai_text_completion, cosine_distance'),
144+
('string-function', 'ASCII, BIN, CHAR_LENGTH');
145+
```
146+
147+
### Step 3: Generate Embeddings
148+
149+
Update the table to generate embeddings for the content using the [ai_embedding_vector](/sql/sql-functions/ai-functions/ai-embedding-vector) function:
150+
```sql
151+
UPDATE doc SET embedding = ai_embedding_vector(content)
152+
WHERE LENGTH(embedding) = 0;
153+
```
154+
155+
### Step 4: Ask a Question and Retrieve Relevant Answers
156+
157+
```sql
158+
-- Define the question as a CTE (Common Table Expression)
159+
WITH question AS (
160+
SELECT 'Tell me the ai functions' AS q
161+
),
162+
-- Calculate the question's embedding vector
163+
question_embedding AS (
164+
SELECT ai_embedding_vector((SELECT q FROM question)) AS q_vector
165+
),
166+
-- Retrieve the top 3 most relevant documents
167+
top_3_docs AS (
168+
SELECT content,
169+
cosine_distance((SELECT q_vector FROM question_embedding), embedding) AS dist
170+
FROM doc
171+
ORDER BY dist ASC
172+
LIMIT 3
173+
),
174+
-- Combine the content of the top 3 documents
175+
combined_content AS (
176+
SELECT string_agg(content, ' ') AS aggregated_content
177+
FROM top_3_docs
178+
),
179+
-- Concatenate a custom prompt, the combined content, and the original question
180+
prompt AS (
181+
SELECT CONCAT(
182+
'Utilizing the sections provided from the Databend documentation, answer the questions to the best of your ability. ',
183+
'Documentation sections: ',
184+
(SELECT aggregated_content FROM combined_content),
185+
' Question: ',
186+
(SELECT q FROM question)
187+
) as p
188+
)
189+
-- Pass the concatenated text to the ai_text_completion function to generate a coherent and relevant response
190+
SELECT ai_text_completion((SELECT p FROM prompt)) AS answer;
191+
```
192+
193+
Result:
194+
```sql
195+
+------------------------------------------------------------------------------------------------------------------+
196+
| answer |
197+
+------------------------------------------------------------------------------------------------------------------+
198+
| Answer: The ai functions mentioned in the Databend documentation are ai_embedding_vector and ai_text_completion. |
199+
+------------------------------------------------------------------------------------------------------------------+
200+
```

0 commit comments

Comments
 (0)