Oracle 自治数据库 Select AI 初体验

ops/2024/10/18 8:22:07/

这几天有点时间,准备尝试下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设定为cohereopenai
-- 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

http://www.ppmy.cn/ops/42037.html

相关文章

【recast-navigation-js】通过websocket获取navmesh数据并初始化

目录 说在前面目录结构websocket服务器前端结果 说在前面 操作系统:windows 11浏览器:edge版本 124.0.2478.97recast-navigation-js版本:0.29.0golang版本:1.21.5 目录结构 D:. │ go.mod │ go.sum │ main.go // websocket …

vue页面中的window.onhashchange事件无法触发分析

一、需求 对某个使用了Vue框架的页面,编写一些脚本操作修改DOM元素。需求之一就是监听URL地址变化,例如:从http://localhost:8080/#/abc切换到http://localhost:8080/#/def。URL有变化就触发执行某些操作。 二、问题 一般来说,…

吴恩达深度学习笔记:优化算法 (Optimization algorithms)2.9-2.10

目录 第二门课: 改善深层神经网络:超参数调试、正 则 化 以 及 优 化 (Improving Deep Neural Networks:Hyperparameter tuning, Regularization and Optimization)第二周:优化算法 (Optimization algorithms)2.9 学习率衰减(Learning rate decay) 第二门…

数据缓存,可以尝试RocksDB了

shigen坚持更新文章的博客写手,擅长Java、python、vue、shell等编程语言和各种应用程序、脚本的开发。记录成长,分享认知,留住感动。 个人IP:shigen shigen在最近的学习中,接触到了一款新的缓存数据库RocksDB&#xff…

STC8增强型单片机开发——串口调试UART

一、什么是串口 串口是一种在数据通讯中广泛使用的通讯接口,通常我们叫做UART (通用异步收发传输器Universal Asynchronous Receiver/Transmitter),其具有数据传输速度稳定、可靠性高、适用范围广等优点。在嵌入式系统中,串口常用于与外部设备…

基于 Spring Boot 博客系统开发(八)

基于 Spring Boot 博客系统开发(八) 本系统是简易的个人博客系统开发,为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。🌿🌿🌿 基于 Spring Boot 博客系统开发(七)&#x1f…

react的多级路由定义

在写实验室项目的时候,有一个需求,在二级路由页面点击按钮,跳转到详情列表页面,同时三级路由不用在导航栏显示,效果图如下: 前期的尝试: 在route,js文件这样定义的: {path: music,…

韵搜坊 -- java爬虫抓取数据

文章目录 三种抓取方式数据抓取的流程获取文章具体操作 获取用户获取图片jsoup操作 三种抓取方式 直接调用请求接口(最方便,这里使用该方法) HttpClient,OKHttp,RestTemplate,Hutool等网页渲染出明文内容后,从前端页面的内容抓取有些网站可能是动态请求…