Skip to content

Commit 3887222

Browse files
authored
docs: add example to ai funtions (#10988)
1 parent d2382e1 commit 3887222

File tree

1 file changed

+60
-0
lines changed
  • docs/doc/15-sql-functions/61-ai-functions

1 file changed

+60
-0
lines changed

docs/doc/15-sql-functions/61-ai-functions/index.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,63 @@ Result:
128128

129129

130130
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. 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.
131+
132+
133+
## Example(https://ask.databend.rs)
134+
135+
We have used [Databend Cloud](https://databend.com) and AI functions to build a demo site: [https://ask.databend.rs](https://ask.databend.rs). We've embedded the entire https://databend.rs website, allowing you to ask questions related to it.
136+
137+
:::note
138+
You can also deploy Databend and configure the `openai_api_key`.
139+
:::
140+
141+
Here's how https://ask.databend.rs works:
142+
143+
### Create Table
144+
145+
Create a table with the following structure:
146+
```sql
147+
CREATE TABLE doc (
148+
path VARCHAR,
149+
content VARCHAR,
150+
embedding ARRAY(FLOAT32)
151+
);
152+
```
153+
154+
### Insert raw data
155+
156+
Insert sample data into the table:
157+
```sql
158+
INSERT INTO doc (path, content) VALUES
159+
('ai-function', 'ai_embedding_vector, ai_text_completion, cosine_distance'),
160+
('string-function', 'ASCII, BIN, CHAR_LENGTH');
161+
```
162+
163+
### Generate Embeddings
164+
165+
Update the table to generate embeddings using [ai_embedding_vector](./02-ai-embedding-vector.md) for the content:
166+
```sql
167+
UPDATE doc SET embedding = ai_embedding_vector(content)
168+
WHERE LENGTH(embedding) = 0;
169+
```
170+
171+
### Ask a Question and Get the Answer
172+
173+
1. Get the question embedding vector:
174+
```sql
175+
SELECT ai_embedding_vector('How to use AI Functions?');
176+
```
177+
178+
2. Retrieve the top 3 most similar documents to the question using [cosine_distance](./04-ai-cosine-distance.md):
179+
```sql
180+
SELECT content, cosine_distance(q_vector, embedding) as dist
181+
FROM doc
182+
ORDER BY dist ASC
183+
LIMIT 3;
184+
```
185+
186+
3. Concatenate the content of the top 3 documents and complete them using [ai_text_completion](./03-ai-text-completion.md):
187+
```sql
188+
SELECT ai_text_completion(<top 3 content snippets>)
189+
```
190+

0 commit comments

Comments
 (0)