|
1 | 1 | --- |
2 | | -title: 'AI Functions' |
| 2 | +title: 'Databend AI Functions using OpenAI' |
| 3 | +description: 'Learn how to use AI functions in Databend with the help of the OpenAI engine.' |
3 | 4 | --- |
4 | 5 |
|
5 | | -Databend leverages the OpenAI **Code-Davinci-002** engine to convert natural language into SQL queries. |
| 6 | +Databend integrates AI functions with the help of the [OpenAI](https://openai.com/) engine, allowing users to generate SQL queries and interact with databases using natural language. This makes it easier for users to work with databases and query data without in-depth knowledge of SQL syntax. |
6 | 7 |
|
7 | | -By integrating OLAP and AI, Databend streamlines the process of crafting SQL queries based on your table schema. |
| 8 | +## AI-Powered SQL Query Generation |
8 | 9 |
|
9 | | -The `ai_to_sql` function enables the effortless generation of SQL queries using natural language. |
10 | | - |
11 | | -## Syntax |
12 | | - |
13 | | -```sql |
14 | | -USE <your-database>; |
15 | | -SELECT * FROM ai_to_sql('<prompt>', '<openai-api-key>'); |
16 | | -``` |
17 | | - |
18 | | -## Example |
19 | | - |
20 | | -:::note |
21 | | -Please note that the generated SQL query may need to be adapted to match Databend's syntax and functionality, as it might be based on PostgreSQL-standard SQL. |
22 | | -::: |
23 | | - |
24 | | - |
25 | | -```sql |
26 | | -CREATE DATABASE IF NOT EXISTS openai; |
27 | | -USE openai; |
28 | | - |
29 | | -CREATE TABLE users( |
30 | | - id INT, |
31 | | - name VARCHAR, |
32 | | - age INT, |
33 | | - country VARCHAR |
34 | | -); |
35 | | - |
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>'); |
66 | | -``` |
67 | | - |
68 | | -Output: |
69 | | -```sql |
70 | | -*************************** 1. row *************************** |
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 | | -+---------+-------------+-------------+ |
88 | | -``` |
| 10 | +Databend leverages the OpenAI Code-Davinci-002 engine to convert natural language into SQL queries and provide assistance with database interactions. By integrating OLAP and AI, Databend streamlines the process of crafting SQL queries based on your table schema. The ai_to_sql function enables the effortless generation of SQL queries using natural language, and the chat capabilities of the OpenAI engine can assist in answering database-related questions. |
0 commit comments