Skip to content

Commit 639ab43

Browse files
authored
Update index.md
1 parent 0e06b47 commit 639ab43

File tree

1 file changed

+53
-8
lines changed
  • docs/doc/15-sql-functions/61-ai-functions

1 file changed

+53
-8
lines changed

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
title: 'AI Functions'
33
---
44

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.
66

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.
88

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.
1010

1111
## Syntax
1212

@@ -23,21 +23,66 @@ Please note that the generated SQL query may need to be adapted to match Databen
2323

2424

2525
```sql
26-
CREATE DATABASE openai;
26+
CREATE DATABASE IF NOT EXISTS openai;
2727
USE openai;
2828

2929
CREATE TABLE users(
3030
id INT,
3131
name VARCHAR,
32-
age INT
32+
age INT,
33+
country VARCHAR
3334
);
3435

35-
SELECT * FROM ai_to_sql('List all users older than 30 years', '<openai-api-key>');
36+
CREATE TABLE orders(
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',
65+
'<openai-api-key>');
3666
```
3767

3868
Output:
3969
```sql
4070
*************************** 1. row ***************************
41-
database: simple_example
42-
generated_sql: SELECT * FROM users WHERE age > 30;
71+
database: openai
72+
generated_sql: SELECT name, SUM(price) AS total_spent, COUNT(order_id) AS total_orders
73+
FROM users
74+
JOIN orders ON users.id = orders.user_id
75+
WHERE country = 'USA' AND age > 30 AND order_date BETWEEN '2022-01-01' AND '2022-12-31'
76+
GROUP BY name;
77+
```
78+
79+
Query Result:
80+
```sql
81+
+---------+-------------+-------------+
82+
| name | order_count | total_spent |
83+
+---------+-------------+-------------+
84+
| Bob | 2 | 2020.00 |
85+
| Alice | 2 | 1020.00 |
86+
| Charlie | 2 | 700.00 |
87+
+---------+-------------+-------------+
4388
```

0 commit comments

Comments
 (0)