You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/doc/15-sql-functions/61-ai-functions/index.md
+53-8Lines changed: 53 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
title: 'AI Functions'
3
3
---
4
4
5
-
Databend can utilize the OpenAI `Code-Davinci-002` engine to translate natural language into SQL queries.
5
+
Databend leverages the OpenAI **Code-Davinci-002** engine to convert natural language into SQL queries.
6
6
7
-
By integrating OLAP and AI, Databend simplifies the process of writing SQL queries based on your table schema.
7
+
By integrating OLAP and AI, Databend streamlines the process of crafting SQL queries based on your table schema.
8
8
9
-
The `ai_to_sql` function allows us to effortlessly generate SQL queries using natural language.
9
+
The `ai_to_sql` function enables the effortless generation of SQL queries using natural language.
10
10
11
11
## Syntax
12
12
@@ -23,21 +23,66 @@ Please note that the generated SQL query may need to be adapted to match Databen
23
23
24
24
25
25
```sql
26
-
CREATEDATABASEopenai;
26
+
CREATEDATABASEIF NOT EXISTS openai;
27
27
USE openai;
28
28
29
29
CREATETABLEusers(
30
30
id INT,
31
31
name VARCHAR,
32
-
age INT
32
+
age INT,
33
+
country VARCHAR
33
34
);
34
35
35
-
SELECT*FROM ai_to_sql('List all users older than 30 years', '<openai-api-key>');
36
+
CREATETABLEorders(
37
+
order_id INT,
38
+
user_id INT,
39
+
product_name VARCHAR,
40
+
price DECIMAL(10,2),
41
+
order_date DATE
42
+
);
43
+
44
+
-- Insert sample data into the users table
45
+
INSERT INTO users VALUES (1, 'Alice', 31, 'USA'),
46
+
(2, 'Bob', 32, 'USA'),
47
+
(3, 'Charlie', 45, 'USA'),
48
+
(4, 'Diana', 29, 'USA'),
49
+
(5, 'Eva', 35, 'Canada');
50
+
51
+
-- Insert sample data into the orders table
52
+
INSERT INTO orders VALUES (1, 1, 'iPhone', 1000.00, '2022-03-05'),
53
+
(2, 1, 'OpenAI Plus', 20.00, '2022-03-06'),
54
+
(3, 2, 'OpenAI Plus', 20.00, '2022-03-07'),
55
+
(4, 2, 'MacBook Pro', 2000.00, '2022-03-10'),
56
+
(5, 3, 'iPad', 500.00, '2022-03-12'),
57
+
(6, 3, 'AirPods', 200.00, '2022-03-14');
58
+
```
59
+
60
+
AI-Powered SQL Query Generation:
61
+
```sql
62
+
-- Generate an SQL query using the ai_to_sql function
63
+
SELECT*FROM ai_to_sql(
64
+
'List the total amount spent by users from the USA who are older than 30 years, grouped by their names, along with the number of orders they made in 2022',
0 commit comments