Oracle 自治数据库 Select AI 初体验

2024-05-16 07:04

本文主要是介绍Oracle 自治数据库 Select AI 初体验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这几天有点时间,准备尝试下Oracle Select AI,虽然此功能2023年就已经发布了。

Oracle自治数据库已经集成好了Select AI,本文也是讲的这个。

配置 Select AI

需要以下步骤:

  1. 创建ADB
  2. 申请Cohere/OpenAI免费账号
  3. 设置ADB
  4. 测试Select AI

第1步在OCI上创建一个自治数据库即可,可以是ATP或ADW,不再赘述。

第2步在Cohere或OpenAI网站上申请个免费账号,我都做了,也很简单。

以下为Cohere的API Key,后续会用到:
在这里插入图片描述
以下为OpenAI的API Key:
在这里插入图片描述

本文主要讲第3和第4步 ,整个过程参考官方文档:Use Select AI to Generate SQL from Natural Language Prompts

以下为详细过程,我直接使用的管理员账户ADMIN,你也可以创建用户xxx:

-- 赋权,对于ADMIN用户不用执行
grant execute on DBMS_CLOUD_AI to xxx;-- 设置ACL,对于ADMIN也需要执行
-- 以下指定的是Cohere大模型,如果是openAI,则host对应改为api.openai.com
-- 用户指定的是ADMIN,如果是用户xxx,则principal_name对应改为xxx
BEGIN  
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(host => 'api.cohere.ai',ace  => xs$ace_type(privilege_list => xs$name_list('http'),principal_name => 'ADMIN',principal_type => xs_acl.ptype_db)
);
END;
/  
-- 创建credential,命名为SELECTAI_CRED,用户名为申请邮箱,密码为Cohere或OpenAI的API key
-- EXEC DBMS_CLOUD.DROP_CREDENTIAL('OPENAI_CRED');
EXEC DBMS_CLOUD.CREATE_CREDENTIAL('SELECTAI_CRED', '申请账户的邮箱', '大模型提供的API Key');-- 创建AI profile,provider设定为cohere或openai
-- EXEC DBMS_CLOUD_AI.drop_profile(profile_name => 'SELECTAI');
BEGINDBMS_CLOUD_AI.create_profile('SELECTAI','{"provider": "cohere","credential_name": "SELECTAI_CRED","object_list": [{"owner": "SH", "name": "customers"},{"owner": "SH", "name": "sales"},{"owner": "SH", "name": "products"},{"owner": "SH", "name": "countries"}]}');
END;
/-- 设定AI Profile
-- 这是一个session设置,因此每次连接数据库时都必须执行
BEGINDBMS_CLOUD_AI.SET_PROFILE(profile_name => 'SELECTAI');
END;
/

测试 Select AI

然后就是测试了,可以用SQL Plus或SQL Developer:

测试OpenAI时,报错,因为超过了速率限制:

ORA-20429: Request failed with status HTTP 429 - bearer://api.openai.com/v1/chat/completions
ORA-06512: at "C##CLOUD$SERVICE.DBMS_CLOUD$PDBCS_240425_1", line 2060
ORA-06512: at "C##CLOUD$SERVICE.DBMS_CLOUD$PDBCS_240425_1", line 12958
ORA-06512: at "C##CLOUD$SERVICE.DBMS_CLOUD_AI", line 3096
ORA-06512: at "C##CLOUD$SERVICE.DBMS_CLOUD_AI", line 3650
ORA-06512: at "C##CLOUD$SERVICE.DBMS_CLOUD_AI", line 5024
ORA-06512: at line 1

没有关系,前面的设置都是正确的。错误HTTP 429表示API调用超过了速率限制。这是由于OpenAI用了免费账户,免费账户的5美元上限或者已达到,或者免费期限已过。

而用Cohere就没有问题了。

SQL> set echo on
SQL> select ai how many customers exist;CUSTOMER_COUNT
--------------55500SQL> select ai showsql how many customers exist;RESPONSE                                           
---------------------------------------------------
SELECT COUNT(*) AS customer_count
FROM SH.CUSTOMERSSQL> select ai narrate how many customers exist;RESPONSE                                                                                                                                                                                                                                                                                                                                                                                                                                                       
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
To determine how many customers exist, we will use the `customers` table provided above which has the column `cust_id` and a ROWNUMBER `ROW_COUNT`. Here is the SQL query to be executed:
```sql
SELECT COUNT(*) AS CUSTOMERS_COUNT
FROM SH.CUSTOMERS;```This query uses the `COUNT(*)` function to count all the rows in the `CUSTOMERS` table. When we execute this query in the Oracle Database, we will get the number of customers in the "SH" schema.SQL> select ai explainsql how many customers in San Francisco are married;RESPONSE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A possible Oracle SQL query to answer the question is:
```sql
SELECT COUNT(*) customer_count
FROM sh.customers
WHERE cust_city = 'San Francisco'
AND cust_martial_status = 'Married';```Explanation:
- The query uses the "SH"."CUSTOMERS" table to retrieve data about customers.
- It counts the number of married customers residing in San Francisco.RESPONSE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- The WHERE clause filters the rows based on two conditions:- ``cust_city = 'San Francisco' '' ensures that only customers with ``San Francisco`` as their city are considered.- ``cust_martial_status = 'Married'`` ensures that only married customers are counted.
- The result is a single number that answers the question.

中文不支持:

select ai 存在多少客户;RESPONSE                                                                                                                                                                                                                                                                                                                                                                               
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorry, unfortunately a valid SELECT statement could not be generated for your natural language prompt. Here is some more information to help you further: I'm sorry, I cannot understand the question because currently Chinese language is not supported. Could you please make the question in English instead? If there's anything else I can assist you with, feel free to ask!

这是由于默认的大模型并不支持中文,DBMS_CLOUD_AI.create_profile创建profile时,需要指定新的Command R+模型:

BEGINDBMS_CLOUD_AI.create_profile('SELECTAI','{"provider": "cohere","credential_name": "SELECTAI_CRED","model" : "command-r-plus","object_list": [{"owner": "SH", "name": "customers"},{"owner": "SH", "name": "sales"},{"owner": "SH", "name": "products"},{"owner": "SH", "name": "countries"}]}');
END;
/

在这里插入图片描述
现在中文就正常了:

SQL> set echo on
SQL> select ai 存在多少客户;CUSTOMER_COUNT
--------------55500SQL> select ai narrate 存在多少客户;"要确定客户表中有多少客户,您可以使用以下SQL查询:
```sql
SELECT COUNT(*) AS customer_count
FROM SH.CUSTOMERS;```此查询将返回客户表中的客户总数。"
SQL> select ai explainsql 旧金山有多少顾客已婚;RESPONSE                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------
SELECT count(*) AS married_customers_count
FROM sh.customers c
WHERE c.cust_marital_status = 'Married'AND c.cust_city = 'San Francisco'

参考

  • Introducing Select AI - Natural Language to SQL Generation on Autonomous Database
  • Autonomous Database Select AI: Accelerate innovation with enterprise data, OCI Generative AI, and enhanced security
  • Autonomous Database speaks “human”
  • Conversations are the next generation in natural language queries
  • Natural Language Queries - Oracle Autonomous Database Now Speaks “Human” - Select AI

这篇关于Oracle 自治数据库 Select AI 初体验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/994201

相关文章

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

PLsql Oracle 下载安装图文过程详解

《PLsqlOracle下载安装图文过程详解》PL/SQLDeveloper是一款用于开发Oracle数据库的集成开发环境,可以通过官网下载安装配置,并通过配置tnsnames.ora文件及环境变... 目录一、PL/SQL Developer 简介二、PL/SQL Developer 安装及配置详解1.下

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

查询SQL Server数据库服务器IP地址的多种有效方法

《查询SQLServer数据库服务器IP地址的多种有效方法》作为数据库管理员或开发人员,了解如何查询SQLServer数据库服务器的IP地址是一项重要技能,本文将介绍几种简单而有效的方法,帮助你轻松... 目录使用T-SQL查询方法1:使用系统函数方法2:使用系统视图使用SQL Server Configu

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

SQL Server数据库迁移到MySQL的完整指南

《SQLServer数据库迁移到MySQL的完整指南》在企业应用开发中,数据库迁移是一个常见的需求,随着业务的发展,企业可能会从SQLServer转向MySQL,原因可能是成本、性能、跨平台兼容性等... 目录一、迁移前的准备工作1.1 确定迁移范围1.2 评估兼容性1.3 备份数据二、迁移工具的选择2.1

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

oracle如何连接登陆SYS账号

《oracle如何连接登陆SYS账号》在Navicat12中连接Oracle11g的SYS用户时,如果设置了新密码但连接失败,可能是因为需要以SYSDBA或SYSOPER角色连接,解决方法是确保在连接... 目录oracle连接登陆NmOtMSYS账号工具问题解决SYS用户总结oracle连接登陆SYS账号