使用 Select AI Agent 构建自治代理
Select AI Agent(自治代理框架)使您能够在 Autonomous AI Database 中构建交互式和自治代理,同时结合规划、工具使用、反射和内存来提供多回合工作流。
注意:
从 19.29 版开始的 Oracle Database 19c 和从 23.26 版开始的 Oracle Database 26ai 中提供了对 Select AI Agent 和 DBMS_CLOUD_AI_AGENT 软件包的支持。关于选择 AI 代理
Select AI Agent(自治代理框架)是一个用于在 Autonomous AI Database 中创建和管理交互式和自治代理的程序。代理关于请求、呼叫工具、反思结果以及使用由 AI 配置文件指定的 LLM 和 ReAct(Reasoning and Acting) 代理模式提供支持的短期和长期内存来维护上下文。
选择 AI Agent 可使用 RAG 和自然语言到 SQL (NL2SQL)、定制 PL/SQL 过程以及外部 REST API 等内置工具来完成任务。该框架保留多回合内存,在对话中维护上下文。这些功能共同支持与企业数据和工作流集成的可扩展、情境感知的生成式 AI。
DBMS_CLOUD_AI_AGENT 软件包封装了管理、编排和安全边界。有关详细信息,请参见 DBMS_CLOUD_AI_AGENT Package 。
选择 AI 代理的功能
其主要功能包括集成智能、灵活的工具、情景感知对话和更快的部署。
-
集成智能:
将规划、工具使用和反思相结合,让座席可以推理任务、选择和运行工具、观察结果、调整计划并改善整个对话的响应。座席计划步骤、运行工具、评估观察结果,并在结果不符合预期时更新方法。此循环可提高准确性、减少返工并保持对话正常进行。
-
灵活的工具:
支持和使用内置 RAG 和 NL2SQL、定制 PL/SQL 过程以及外部 REST 服务,无需编排组件或单独的基础设施,因此您可以在数据库中保留核心逻辑,同时根据需要集成外部功能。
-
感知上下文的对话:
保持短期和长期记忆,在多回合会话中保持上下文,个性化响应,存储偏好,并支持在多回合会话中进行更正和确认的人工控制。短期记忆使当前的对话保持一致。长期记忆记录偏好和先前的结果,支持人类审查员的后续互动和监督。
-
可扩展且安全:
在 Autonomous AI Database 中运行,继承安全控制、审计和性能,减少数据移动,并标准化企业部署和受监管环境的治理。代理可以从数据库安全性、审计和性能特征中受益。密切处理数据可减少移动,并与治理实践保持一致。
-
更快的开发:
使用熟悉的 SQL 和 PL/SQL 定义代理、任务和工具,重用现有过程并更快地发布功能,同时保持逻辑接近运营数据和团队,而无需构建单独的基础设施。
ReAct 代理模式
Select AI Agent 使用 ReAct (Reasoning and Acting) 代理模式,在此模式中,代理将有关请求的原因、选择工具、执行操作以及评估结果以实现目标。
-
查询:用户询问问题或陈述请求。座席会读取它,提取关键详细信息,并准备计划后续步骤。
-
思想和行动:代理关于选项的原因,选择工具,并运行它来收集数据或根据需要更改任务的状态。
-
观察:观察包括工具或查询结果、确认消息和错误。这些成为代理人下一轮推理的投入。座席记录观察结果并检查结果是否支持下一步或最终响应。
-
最终响应:在足够成功的思考和观察之后,代理会做出明确的回答,解释重要的决策,并分享任何后续步骤或后续操作。
选择 AI 代理体系结构
Select AI Agent 将工作分为四个层:Planning、Tool Use、Reflection 和 Memory Management。这些层协调推理、工具运行、评估和上下文多向交互。
Planning :Planning 将解释用户请求,将其分解为有序操作,选择候选工具,并使用会话上下文、先前结果和相关知识拟定计划。代理分析请求,确定缺少的详细信息,并建议有序的操作序列。它选择适合策略、数据范围和预期结果的工具。
工具使用:工具使用会为每个操作选择并运行工具。支持的类型包括 RAG、NL2SQL、可以在创建工具时添加的定制 PL/SQL 过程,以及 Web 搜索和电子邮件等外部 REST 服务。每个步骤都调用具有参数的工具。内置工具可处理检索和 SQL 生成。定制 PL/SQL 封装域逻辑。REST 工具连接到外部服务。
反思:反思根据预期评估工具结果,并进行最终响应。代理将观察值与目标值进行比较。如果结果看起来错误,或者存在工具调用错误或用户未批准的结果,则代理会修改推理、选择其他工具或更新计划,然后再重试。当结果不合适时,它会调整计划,选择不同的工具,或者在继续之前提出澄清问题。可以使用 USER_CLOUD_AI_CONVERSATION_PROMPTS 查询 AI 代理思想。有关更多信息,请参见 DBMS_CLOUD_AI Views 。
内存管理:内存管理按代理团队存储会话上下文和知识。短期内存存储每个代理团队最近的消息和中间结果。长期记忆记录偏好、历史和策略,改善连续性、个性化和计划。长期记忆在各个会话中保留有用的知识,并随着时间的推移提高座席团队的指导和响应质量。
选择 AI 代理术语
了解与 Select AI Agent(自治代理框架)相关的概念和术语。
以下是与 Select AI Agent 相关的术语:
| 术语 | 定义 |
|---|---|
| 代理操作 |
在代理的上下文中,操作是触发具有参数的工具的指令。平台运行该工具,处理错误,并返回下一个推理步骤循环的观察结果。 操作将计划连接到执行。它们定义要运行的工具和输入。 |
| 代理 |
座席根据定义的目的执行任务。代理是已配置的员工,其原因包括请求、选择工具、运行步骤、评估结果以及生成基于数据库上下文的响应。 代理封装特定作业的行为、策略和可用工具,例如返回处理或知识检索。 |
| 座席团队 |
执行代理工作流的一个或多个代理。团队负责协调责任、共享上下文并运行代理 - 任务对,以可靠地完成多步骤交互。 小组在专门人员之间分配工作,并按其贡献排序。团队保持共享上下文并生成统一的响应。 您可以使用以下历史记录视图查看代理和团队运行详细信息: |
| MCP 服务器 |
MCP 服务器(Model Context Protocol Server,模型上下文协议服务器)通过一致的协议公开模型功能、上下文检索和扩展,为应用提供了一种标准化的方法来与生成式 AI 模型和工具进行交互。 使用 Select AI Agent,可以定义可通过 Autonomous AI Database MCP Server 公开的工具。这些工具可以包括用于 SQL 生成、RAG、通知(电子邮件和 Slack)和 Web 搜索的内置工具,以及定制工具。定制工具可以公开数据库中定义的函数。 |
| 观察 |
从工具运行返回的数据:代理在反射期间记录和评估的行、消息或错误。观察指导下一个推理步骤,并为最终反应提供证据。 |
| 任务 |
任务表示工作单位。它引导工具选择、参数映射和执行策略,生成下游步骤可以读取和汇总的结果。 任务指定目标、输入、工具选择和护栏。它们返回后面的步骤可以使用的结构化输出。 您可以查询 |
| 工具 |
工具执行诸如更新数据、检索文档或调用外部服务之类的操作。工具接受参数,可以运行确定性或非确定性,并返回观测进行推理。 工具封装了可重复的操作。它们可控制和观察副作用,支持审计和调试。 在 Select AI Agent 框架中,工具表示代理在任务处理期间可以使用的功能构建块。工具可以是:
代理根据任务的需求选择工具,并使用输出(称为观察)推理、决定后续步骤或生成响应。将记录工具元数据和运行历史记录,以支持调试、可观察性和安全性。 您可以查询 |
选择 AI 代理用例
Select AI Agent 支持跨角色的实际场景:连接外部服务,使用 PL/SQL 工具构建以任务为中心的自动化,以及使用自然语言交互实现对数据的对话访问。
使用场景包括:
集成外部 API 以实现扩充响应:
连接 REST 服务,例如发运状态、知识文章或消息传递。该座席将外部结果与数据库事实相结合,并及时返回切实可行的指导。
使用 PL/SQL 工具构建特定于任务的代理:
创建专属座席,调用 PL/SQL 工具进行更新、验证或转换,保持逻辑接近数据,提高性能,并统一简化各个环境的维护。将现有 PL/SQL 包装以进行更新和检查。代理在事务控制范围内协调任务、强制执行参数并记录结果。
启用对企业数据的对话访问:
通过基于数据库对象的自然语言问题公开数据。使用 NL2SQL 检索、应用策略并明确呈现调查结果,同时遵守安全性和监管要求。NL2SQL 将已批准对象上的问题转换为 SQL。代理汇总结果和后续步骤,同时遵循对象列表和访问规则。
使用 Select AI Agent 的先决条件
在使用 Select AI Agent 之前,您必须具有使用 DBMS_CLOUD_AI_AGENT 软件包的特权以及 Select AI 所需的所有其他特权。
要使用 Select AI Agent,必须执行以下操作:
-
为 Select AI 执行先决条件并授予权限。请参见 Configure AI Profiles Using DBMS_CLOUD_AI fr 说明。
-
至少定义了一个代理团队。请参见CREATE_AGENT Procedure 。
-
(可选)创建
conversation_id以跟踪多回合上下文。
为选择 AI 代理授予权限
要配置 DBMS_CLOUD_AI_AGENT,请执行以下操作:
将 DBMS_CLOUD_AI_AGENT 软件包上的 EXECUTE 权限授予要使用 Select AI Agent 的用户。
缺省情况下,只有系统管理员具有 EXECUTE 权限。管理员可以将 EXECUTE 权限授予其他用户。
运行选择 AI 代理的权限示例
以下示例将 EXECUTE 特权授予 ADB_USER:
GRANT EXECUTE on DBMS_CLOUD_AI_AGENT to ADB_USER;使用 Select AI Agent 的示例
浏览示例,了解如何针对电影分析、日志分析和客户支持等常见任务构建、配置和与 Select AI Agent 交互。
示例:创建代理
创建代理以执行定义的任务。
此示例创建一个名为 Customer_Return_Agent 的代理,负责处理与产品退货相关的对话。
此示例说明如何使用 Google 作为 AI 提供程序(如在名为 GOOGLE 的 AI 配置文件中指定的)。AI 配置文件标识代理用于推理和响应的 LLM。role 属性提供了指导代理的说明。
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
agent_name => 'CustomerAgent',
attributes =>'{
"profile_name": "GOOGLE",
"role": "You are an experienced customer agent who deals with customers return request."
}'
);
END;
/ 示例:创建内置工具
创建内置工具,例如 SQL、RAG、Websearch、Email 和 Slack。这些工具使代理和任务能够查询数据、检索知识、搜索 Web 和发送通知。
内置工具(例如 SQL 和 RAG)包括内部指令。您可以根据需要附加用户提供的说明来针对您的用例定制行为。附加指令可以添加特定于任务的上下文(例如,将范围查询添加到特定方案或帮助强制实施响应格式)。这可以提高与专业情景的相关性。编写不当或规范性指令过高可能会降低工具性能。保持说明的简短,具体,并与工具的目的一致。
此示例创建一个 SQL 工具,用于将自然语言查询转换为 SQL 语句。SQL 工具通过将提示映射到 SQL 查询来帮助代理回答与数据相关的问题。
此示例显示了如何使用 OCI 作为 AI 提供程序(如在名为 nl2sql_profile 的 AI 配置文件中指定)。AI 配置文件标识代理用于推理和响应的 LLM。在此示例中,AI 配置文件 nl2sql_profile 定义了座席可以查询的 SH 方案表集,从而允许使用自然语言访问常用销售历史记录数据,例如客户、产品、促销和国家/地区。SQL 工具使用 AI 概要信息中指定的数据库对象,确保生成的 SQL 语句保持准确且与 SH 数据集相关。
BEGIN
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name => 'nl2sql_profile',
attributes => '{"provider": "oci",
"credential_name": "GEN1_CRED",
"oci_compartment_id": "ocid1.compartment.oc1..aaaa...",
object_list": [{"owner": "SH", "name": "customers"},
{"owner": "SH", "name": "countries"},
{"owner": "SH", "name": "supplementary_demographics"},
{"owner": "SH", "name": "profits"},
{"owner": "SH", "name": "promotions"},
{"owner": "SH", "name": "products"}]
}');
end;
/
EXEC DBMS_CLOUD_AI_AGENT.DROP_TOOL('SQL');
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'SQL',
attributes => '{"tool_type": "SQL",
"tool_params": {"profile_name": "nl2sql_profile"}}'
);
END;
/此示例创建 RAG(检索增强生成)工具。利用 RAG 工具,座席可以在企业文档中检索和接地响应,从而提高基于知识的答案的准确性。
此示例说明如何使用凭证定义 RAG_PROFILE、向量索引以及指定配置文件参数。然后,在对象存储中为文档嵌入创建向量索引 RAG_INDEX,并创建链接到概要信息的 RAG_TOOL。
BEGIN
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name =>'RAG_PROFILE',
attributes =>'{"provider": "oci",
"credential_name": "GENAI_CRED",
"vector_index_name": "RAG_INDEX",
"oci_compartment_id": "ocid1.compartment.oc1..aaaa..",
"temperature": 0.2,
"max_tokens": 3000
}');
END;
/
BEGIN
DBMS_CLOUD_AI.CREATE_VECTOR_INDEX(
index_name => 'RAG_INDEX',
attributes => '{"vector_db_provider": "oracle",
"location": "https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/my_namespace/my_bucket/my_data_folder",
"object_storage_credential_name": "OCI_CRED",
"profile_name": "RAG_PROFILE",
"chunk_overlap":128,
"chunk_size":1024
}');
END;
/
EXEC DBMS_CLOUD_AI_AGENT.DROP_TOOL('RAG_TOOL');
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'RAG_TOOL',
attributes => '{"tool_type": "RAG",
"tool_params": {"profile_name": "RAG_PROFILE"}}'
);
END;
/
此示例创建一个 Websearch 工具,用于从 Internet 检索详细信息。Websearch 工具使代理能够从 Web 中查找信息,例如产品价格或说明。
OPENAI_CRED 并创建 Websearch 工具,描述其用途并将其链接到身份证明。
注意:
OpenAI 是唯一支持的 Websearch AI 提供程序。BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'api.openai.com',
ace => xs$ace_type(privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db)
);
END;
/
BEGIN
DBMS_CLOUD.CREATE_CREDENTIAL(
credential_name => 'OPENAI_CRED',
username => 'OPENAI',
password => '<OPENAI_API_KEY>'
);
END;
/
EXEC DBMS_CLOUD_AI_AGENT.DROP_TOOL('Websearch');
BEGIN
DBMS_CLOUD_AI_AGENT.create_tool(
tool_name => 'Websearch',
attributes => '{"instruction": "This tool can be used for searching the details about topics mentioned in notes and prepare a summary about prices, details on web",
"tool_type": "WEBSEARCH",
"tool_params": {"credential_name": "OPENAI_CRED"}}'
);
END;
/
此示例创建电子邮件通知工具。通过电子邮件工具,代理可以在其工作流中发送通知电子邮件。
此示例说明如何使用口令创建身份证明 EMAIL_CRED,允许数据库用户访问 SMTP,以及创建类型为 EMAIL 的通知工具,包括 SMTP 详细信息、发件人地址和收件人地址。
BEGIN
DBMS_CLOUD.CREATE_CREDENTIAL(
credential_name => 'EMAIL_CRED',
username => '<username>',
password => '<password>');
END;
/
-- Allow SMTP access for user
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'smtp.email.us-ashburn-1.oci.oraclecloud.com',
lower_port => 587,
upper_port => 587,
ace => xs$ace_type(privilege_list => xs$name_list('SMTP'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db));
END;
/
EXEC DBMS_CLOUD_AI_AGENT.DROP_TOOL('Email');
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'EMAIL',
attributes => '{"tool_type": "NOTIFICATION",
"tool_params": {"notification_type" : "EMAIL",
"credential_name": "EMAIL_CRED",
"recipient": "example_recipient@oracle.com",
"smtp_host": "smtp.email.us-ashburn-1.oci.oraclecloud.com",
"sender": "example_sender@oracle.com"}}'
);
END;
/
此示例创建 Slack 通知工具。借助 Slack 工具,座席可以将通知直接传送到 Slack 工作区通道。
此示例说明如何为 Slack 添加 ACL 条目以及创建通知工具(类型为 SLACK),将它链接到 SLACK_CRED 和目标通道。
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE (
host => 'slack.com',
lower_port => 443,
upper_port => 443,
ace => xs$ace_type(
privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db));
END;
/
BEGIN
DBMS_CLOUD_AI_AGENT.create_tool(
tool_name => 'slack',
attributes => '{"tool_type": "SLACK",
"tool_params": {"credential_name": "SLACK_CRED",
"channel": "<channel_number>"}}'
);
END;
/示例:创建任务
创建座席可以执行的任务。
注意:
只有 DBA 才能授予 EXECUTE 特权和网络 ACL 过程。
此示例创建 Generate_Email_Task,指示 LLM 使用结构化数据生成标准确认电子邮件。
以下示例说明了如何使用 instruction 属性,并提供了有关电子邮件应包含的内容的说明。
BEGIN DBMS_CLOUD_AI_AGENT.DROP_TASK('Generate_Email_Task');
EXCEPTION WHEN OTHERS THEN NULL; END;
/
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'Generate_Email_Task',
attributes => '{"instruction": "Use the customer information and product details to generate an email in a professional format. The email should:' ||
'1. Include a greeting to the customer by name' ||
'2. Specify the item being returned, the order number, and the reason for the return' ||
'3. If it is a refund, state the refund will be issued to the credit card on record.' ||
'4. If it is a replacement, state that the replacement will be shipped in 3-5 business days."}'
);
END;此示例创建一个基于请求检索日志的 FETCH_LOGS_TASK。
此示例说明如何创建使用 log_fetcher 工具检索日志的任务。此定制工具指定 PL/SQL 过程 fetch_logs。任务指令指定任务需要完成的任务。属性 enable_human_tool 设置为 true,以便人员可以介入以指导或批准任务流(如果需要)。有关完整示例,请参见。
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name =>'FETCH_LOGS_TASK',
attributes =>'{
"instruction": "Fetch the log entries from the database based on user request: {query}",
"tools": ["log_fetcher"],
"enable_human_tool" : "true"
}'
);
END;
/此示例创建一个 ANALYZE_LOG_TASK,用于分析提取的日志。
以下示例说明如何使用 FETCH_LOGS_TASK 作为 input 属性。名为 ANALYZE_LOG_TASK 的任务在收集日志以将 FETCH_LOGS_TASK 输出作为输入后启动。任务指令指定任务需要完成的任务。属性 enable_human_tool 设置为 false,表示不存在人工循环审查。
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name =>'ANALYZE_LOG_TASK',
attributes =>'{"instruction": "Analyze the fetched log entries retrieved based on user request: {query} ' ||
'Generate a detailed report include issue analysis and possible solution.",
"input" : "FETCH_LOGS_TASK",
"enable_human_tool" : "false"
}'
);
END;
/示例:创建座席团队
创建座席团队以完成您的任务。
此示例创建 ReturnAgency 团队并包括单个代理 Customer_Return_Agent。任务 Return_And_Price_Match 已分配给该代理。此任务通过询问原因和更新数据库中的订单状态来管理退货请求。process 设置为 sequential 以按定义的顺序运行任务。
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TEAM(
team_name => 'ReturnAgency',
attributes => '{"agents": [{"name":"Customer_Return_Agent","task" : "Return_And_Price_Match"}],
"process": "sequential"}');
END;
/
此示例创建 Ops_Issues_Solution_Team 团队,并包括两个用于处理日志分析和故障排除的代理:Data_Engineer 和 Ops_Manager。Data_Engineer 代理运行任务 fetch_logs_task,Ops_Manager 代理运行任务 analyze_log_task。process 设置为 sequential 以按定义的顺序运行任务。
BEGIN
DBMS_CLOUD_AI_AGENT.create_team(
team_name => 'Ops_Issues_Solution_Team',
attributes => '{"agents": [{"name":"Data_Engineer","task" : "fetch_logs_task"},
{"name":"Ops_Manager","task" : "analyze_log_task"}],
"process": "sequential"
}');
END;
/
示例:使用内置工具创建电影分析代理
此示例说明如何使用 Select AI Agent 创建电影分析代理。在此示例中,设置了一个电影分析代理,用于检索数据、回答问题、搜索 Web 以及通过电子邮件发送分析或发送 Slack 通知。
以下示例假定数据可供您使用。
此示例创建 MOVIE_ANALYST 代理并使用多个内置工具,例如 SQL、RAG、WEBSEARCH 和 NOTIFICATION 类型的工具:EMAIL 和 SLACK。代理使用自然语言提示回答与电影相关的问题。
在本示例中,DBA 为以下项授予 EXECUTE 特权: DBMS_CLOUD_AI_AGENT、DBMS_CLOUD_AI、DBMS_CLOUD_PIPELINE 软件包、AI 提供者的 ACL 访问、SMTP 访问和 Slack 访问,然后首先创建 MOVIE_ANALYST 代理。
创建代理
您创建了一个名为 MOVIE_ANALYST 的座席,其中包含个人资料 (GROK) 以及回答有关电影、演员和流派的问题的角色。
创建任务
您可以创建一个名为 ANALYZE_MOVIE_TASK 的任务,其中包含用于回答与电影相关的查询的说明。
创建团队
您可以创建一个 MOVIE_AGENT_TEAM 团队,将 MOVIE_ANALYST 作为代理,将任务设置为 ANALYZE_MOVIE_TASK,并将其设置为活动团队。
您以后可以附加 SQL、RAG、Websearch 或 Notification 等工具来扩展其功能。
运行 Select AI Agent Team
现在,您可以使用 select ai agent 作为提示的前缀来运行代理团队。
创建 Tools
SQL:使用 NL2SQL 概要文件将问题转换为 SQL 查询和其他受支持的 Select AI 操作。RAG:从存储的文档中检索基于知识的上下文。WEBSEARCH:在线收集电影详情或价格。NOTIFICATION:EMAIL:将用户提示、电影报告或评论的答案发送给收件人。SLACK:将用户提示、摘要或更新的答案直接发送到 Slack。
完整的示例如下所示:
--Grants EXECUTE privilege to ADB_USER
--
GRANT EXECUTE on DBMS_CLOUD_AI_AGENT to ADB_USER;
GRANT EXECUTE on DBMS_CLOUD_AI to ADB_USER;
GRANT EXECUTE on DBMS_CLOUD_PIPELINE to ADB_USER;
-- Websearch tool accesses OPENAI endpoint, allow ACL access
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'api.openai.com',
ace => xs$ace_type(privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db)
);
END;
/
-- To allow Email tool in Autonomous Database, allow SMTP access
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'smtp.email.us-ashburn-1.oci.oraclecloud.com',
lower_port => 587,
upper_port => 587,
ace => xs$ace_type(privilege_list => xs$name_list('SMTP'),
principal_name => 'ADB_USER,
principal_type => xs_acl.ptype_db));
END;
/
-- Allow ACL access to Slack
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE (
host => 'slack.com',
lower_port => 443,
upper_port => 443,
ace => xs$ace_type(
privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db));
END;
/
PL/SQL procedure successfully completed.
--Create an agent
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
agent_name => 'MOVIE_ANALYST',
attributes => '{"profile_name": "GROK",
"role": "You are an AI Movie Analyst. Your can help answer a variety of questions related to movies. "
}'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'ANALYZE_MOVIE_TASK',
attributes => '{"instruction": "Help the user with their request about movies. User question: {query}",
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TEAM(
team_name => 'MOVIE_AGENT_TEAM',
attributes => '{"agents": [{"name":"MOVIE_ANALYST","task" : "ANALYZE_MOVIE_TASK"}],
"process": "sequential"
}');
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('MOVIE_AGENT_TEAM');
PL/SQL procedure successfully completed.
select ai agent who are you?;
RESPONSE
--------------------------------------------------------------------------------
I'm MOVIE_ANALYST, an AI Movie Analyst here to assist with any questions or topi
cs related to movies. Whether you need information on films, actors, directors,
genres, or recommendations, I'm ready to help. What can I assist you with regard
ing movies?
-- SQL TOOL
BEGIN
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name =>'nl2sql_profile',
attributes => '{"provider": "oci",
"credential_name": "GENAI_CRED",
"oci_compartment_id" : "ocid1.compartment.oc1..aaaaa...",
"object_list": [{"owner": "ADB_USER", "name": "GENRE"},
{"owner": "ADB_USER", "name": "CUSTOMER"},
{"owner": "ADB_USER", "name": "WATCH_HISTORY"},
{"owner": "ADB_USER", "name": "STREAMS"},
{"owner": "ADB_USER", "name": "MOVIES"},
{"owner": "ADB_USER", "name": "ACTORS"}]
}');
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'SQL',
attributes => '{"tool_type": "SQL",
"tool_params": {"profile_name": "nl2sql_profile"}}'
);
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.drop_task('ANALYZE_MOVIE_TASK');
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'ANALYZE_MOVIE_TASK',
attributes =>'{"instruction": "Help the user with their request about movies. User question: {query}. ' ||
'You can use SQL tool to search the data from database",
"tools": ["SQL"],
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.CLEAR_TEAM;
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('MOVIE_AGENT_TEAM');
PL/SQL procedure successfully completed.
-- SQL tool retrieves the movie with the highest popularity view count from the watch_history table
select ai agent what is the most popular movie?;
RESPONSE
----------------------------------------------------------------
The most popular movie is "Laugh Out Loud" released in 2008.
-- RAG TOOL
BEGIN
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name => 'RAG_PROFILE',
attributes =>'{"provider": "oci",
"credential_name": "GENAI_CRED",
"vector_index_name": "RAG_INDEX",
"oci_compartment_id": "ocid1.compartment.oc1..aaaaa...",
"temperature": 0.2,
"max_tokens": 3000
}');
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI.CREATE_VECTOR_INDEX(
index_name => 'RAG_INDEX',
attributes => '{"vector_db_provider": "oracle",
"location": "https://swiftobjectstorage.us-phoenix-1.oraclecloud.com/v1/my_namespace/my_bucket/my_data_folder",
"object_storage_credential_name": "MY_OCI_CRED",
"profile_name": "RAG_PROFILE",
"vector_dimension": 1024,
"vector_distance_metric": "cosine",
"chunk_overlap":128,
"chunk_size":1024
}');
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'RAG_TOOL',
attributes => '{"tool_type": "RAG",
"tool_params": {"profile_name": "RAG_PROFILE"}}'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name =>'ANALYZE_MOVIE_TASK',
attributes => '{"instruction": "Help the user with their request about movies. User question: {query}. ' ||
'You can use RAG tool to search the information from the knowledge base user give.",
"tools": ["RAG_TOOL"],
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.CLEAR_TEAM;
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('MOVIE_AGENT_TEAM');
PL/SQL procedure successfully completed.
-- Rag seach the object store to find review or comments of Movie Laugh out Loud
select ai agent Please find the comments of Movie Laugh out Loud;
RESPONSE
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The comments for the movie "Laugh Out Loud" (2008) are as follows:
1. A lighthearted comedy that delivers plenty of laughs, though the plot is fairly predictable.
2. The performances are fun, especially from the lead actor who keeps the energy high.
3. Some jokes feel a bit outdated, but overall it is an enjoyable watch for a casual movie night.
4. Good chemistry between the cast members, which makes the humor more natural.
5. Not a groundbreaking comedy, but it does what it promises makes you laugh out loud.
-- WEBSEARCH TOOL
BEGIN
DBMS_CLOUD.CREATE_CREDENTIAL(
credential_name => 'OPENAI_CRED',
username => 'OPENAI',
password => '<API_KEY>'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'WEBSEARCH_TOOL',
attributes => '{"instruction": "This tool can be used for searching the details about topics mentioned in notes and prepare a summary about prices, details on web",
"tool_type": "WEBSEARCH",
"tool_params": {"credential_name": "OPENAI_CRED"}}'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'ANALYZE_MOVIE_TASK',
attributes => '{"instruction": "Help the user with their request about movies. User question: {query}. ' ||
'You can use WEBSEARCH_TOOL tool to search the information from internet.",
"tools": ["WEBSEARCH_TOOL"],
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.CLEAR_TEAM;
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('MOVIE_AGENT_TEAM');
PL/SQL procedure successfully completed.
select ai agent What is the most popular movie of 2023?;
RESPONSE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Determining the most popular movie of 2023 can depend on various metrics such as box office earnings, streaming viewership, critical acclaim, or audience reception. Based on global box office data, which is often a strong indicator of popularity, the most successful movie of 2023 is "Barbie," directed by Greta Gerwig. Released in July 2023, it grossed over $1.4 billion worldwide, making it the highest-grossing film of the year and one of the biggest cultural phenomena, often discussed alongside "Oppenheimer" due to the "Barbenheimer" trend. Its widespread appeal, marketing, and social media buzz further solidify its status as the most popular movie of 2023. However, if you are looking for popularity based on a different metric (like streaming numbers or awards), please let me know, and I can adjust the analysis accordingly.
-- NOTIFICATION TOOL WITH EMAIL TYPE
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'EMAIL',
attributes => q'[{"tool_type": "Notification",
"tool_params": {"notification_type" : "EMAIL",
"credential_name": "EMAIL_CRED",
"recipient": "example@oracle.com",
"smtp_host": "smtp.email.us-ashburn-1.oci.oraclecloud.com",
"sender": "example@oracle.com"}}]'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'ANALYZE_MOVIE_TASK',
attributes =>'{"instruction": "Help the user with their request about movies. User question: {query}. ' ||
'You can use EMAIL TOOL tool to send email to the user.",
"tools": ["EMAIL"],
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.CLEAR_TEAM;
PL/SQL procedure successfully completed.
EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('MOVIE_AGENT_TEAM');
PL/SQL procedure successfully completed.
select ai agent Please help me write a review of Movie "Barbie" and send the review to my email;
RESPONSE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I have written a review for the movie "Barbie" (2023) and sent it to your email. Please check your inbox for the detailed review. If you have any additional feedback or would like me to revise the review, let me know!
-- NOTIFICATION TOOL WITH SLACK TYPE
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'SLACK_TOOL',
attributes => '{"tool_type": "SLACK",
"tool_params": {"credential_name": "SLACK_CRED",
"channel": "<channel_number>"}}'
);
END;
/
PL/SQL procedure successfully completed.
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'ANALYZE_MOVIE_TASK',
attributes => '{"instruction": "Help the user with their request about movies. User question: {query}. ' ||
'You can use SLACK TOOL to send the notification to the user.",
"tools": ["SLACK_TOOL"],
"enable_human_tool" : "true"
}'
);
END;
/
PL/SQL procedure successfully completed.
select ai agent Please help me find the top 3 most-watched movies of 2023 and send them to me on slack;
RESPONSE
------------------------------------------------------------------------------------------------------------------------------------
I have sent the list of the top 3 most-watched movies of 2023 to you via Slack. Please check your Slack notifications for the details.
示例:创建产品退货代理
此示例说明如何使用 Select AI Agent 创建多回合会话代理。在此示例中,设置一个客户服务代理来处理产品退货并更新数据库中的退货状态。
此示例创建一个名为 Customer_Return_Agent 的代理和一个名为 Update_Order_Status_Tool 的工具,然后定义一个任务和一个团队来处理产品退货。
在本示例中,DBA 授予 DBMS_CLOUD_AI_AGENT 和 DBMS_CLOUD_AI 的 EXECUTE 权限后,首先创建客户示例数据、客户订单状态和用于更新客户订单状态的函数。然后,创建一个名为 Customer_Return_Agent 的代理。
创建代理
创建一个名为 Customer_Return_Agent 的代理,该代理具有配置文件 (OCI_GENAI_GROK) 和角色以管理返回请求。
创建 Tools
然后,创建名为 Update_Order_Status_Tool 的代理工具来更新数据库中订单的状态。
创建任务
您创建了一个名为 Handle_Product_Return_Task 的任务来指导流:询问原因(不再需要,到达太晚,盒子断开或有缺陷)。继续执行有缺陷的返回流。
创建团队
您创建了一个名为 Return_Agency_Team、以 Customer_Return_Agent 作为代理的代理团队,并将其设置为活动团队。
运行 Select AI Agent Team
现在,您可以使用 select ai agent 作为提示的前缀来运行代理团队。
完整的示例如下所示:
--Grants EXECUTE privilege to ADB_USER
--
GRANT EXECUTE on DBMS_CLOUD_AI_AGENT to ADB_USER;
GRANT EXECUTE on DBMS_CLOUD_AI to ADB_USER;
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
agent_name => 'Customer_Return_Agent',
attributes => '{"profile_name": "OCI_GENAI_GROK",
"role": "You are an experienced customer return agent who deals with customers return requests."}');
END;
/
PL/SQL procedure successfully completed.
--Sample customer data
CREATE TABLE CUSTOMERS (
customer_id NUMBER(10) PRIMARY KEY,
name VARCHAR2(100),
email VARCHAR2(100),
phone VARCHAR2(20),
state VARCHAR2(2),
zip VARCHAR2(10)
);
INSERT INTO CUSTOMERS (customer_id, name, email, phone, state, zip) VALUES
(1, 'Alice Thompson', 'alice.thompson@example.com', '555-1234', 'NY', '10001'),
(2, 'Bob Martinez', 'bob.martinez@example.com', '555-2345', 'CA', '94105'),
(3, 'Carol Chen', 'carol.chen@example.com', '555-3456', 'TX', '73301'),
(4, 'David Johnson', 'david.johnson@example.com', '555-4567', 'IL', '60601'),
(5, 'Eva Green', 'eva.green@example.com', '555-5678', 'FL', '33101');
--create customer order status
CREATE TABLE CUSTOMER_ORDER_STATUS (
customer_id NUMBER(10),
order_number VARCHAR2(20),
status VARCHAR2(30),
product_name VARCHAR2(100)
);
INSERT INTO CUSTOMER_ORDER_STATUS (customer_id, order_number, status, product_name) VALUES
(2, '7734', 'delivered', 'smartphone charging cord'),
(1, '4381', 'pending_delivery', 'smartphone protective case'),
(2, '7820', 'delivered', 'smartphone charging cord'),
(3, '1293', 'pending_return', 'smartphone stand (metal)'),
(4, '9842', 'returned', 'smartphone backup storage'),
(5, '5019', 'delivered', 'smartphone protective case'),
(2, '6674', 'pending_delivery', 'smartphone charging cord'),
(1, '3087', 'returned', 'smartphone stand (metal)'),
(3, '7635', 'pending_return', 'smartphone backup storage'),
(4, '3928', 'delivered', 'smartphone protective case'),
(5, '8421', 'pending_delivery', 'smartphone charging cord'),
(1, '2204', 'returned', 'smartphone stand (metal)'),
(2, '7031', 'pending_delivery', 'smartphone backup storage'),
(3, '1649', 'delivered', 'smartphone protective case'),
(4, '9732', 'pending_return', 'smartphone charging cord'),
(5, '4550', 'delivered', 'smartphone stand (metal)'),
(1, '6468', 'pending_delivery', 'smartphone backup storage'),
(2, '3910', 'returned', 'smartphone protective case'),
(3, '2187', 'delivered', 'smartphone charging cord'),
(4, '8023', 'pending_return', 'smartphone stand (metal)'),
(5, '5176', 'delivered', 'smartphone backup storage');
--Create a update customer order status function
CREATE OR REPLACE FUNCTION UPDATE_CUSTOMER_ORDER_STATUS (
p_customer_name IN VARCHAR2,
p_order_number IN VARCHAR2,
p_status IN VARCHAR2
) RETURN CLOB IS
v_customer_id customers.customer_id%TYPE;
v_row_count NUMBER;
BEGIN
-- Find customer_id from customer_name
SELECT customer_id
INTO v_customer_id
FROM customers
WHERE name = p_customer_name;
UPDATE customer_order_status
SET status = p_status
WHERE customer_id = v_customer_id
AND order_number = p_order_number;
v_row_count := SQL%ROWCOUNT;
IF v_row_count = 0 THEN
RETURN 'No matching record found to update.';
ELSE
RETURN 'Update successful.';
END IF;
EXCEPTION
WHEN OTHERS THEN
RETURN 'Error: ' || SQLERRM;
END;
--Create Tool
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'Update_Order_Status_Tool',
attributes => '{"instruction": "This tool updates the database to reflect return status change. Always confirm user name and order number with user before update status",
"function" : "update_customer_order_status"}',
description => 'Tool for updating customer order status in database table.'
);
END;
/
--Create Task
BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'Handle_Product_Return_Task',
attributes => '{"instruction": "Process a product return request from a customer:{query}' ||
'1. Ask customer the order reason for return (no longer needed, arrived too late, box broken, or defective)' ||
'2. If no longer needed:' ||
' a. Inform customer to ship the product at their expense back to us.' ||
' b. Update the order status to return_shipment_pending using Update_Order_Status_Tool.' ||
'3. If it arrived too late:' ||
' a. Ask customer if they want a refund.' ||
' b. If the customer wants a refund, then confirm refund processed and update the order status to refund_completed' ||
'4. If the product was defective or the box broken:' ||
' a. Ask customer if they want a replacement or a refund' ||
' b. If a replacement, inform customer replacement is on its way and they will receive a return shipping label for the defective product, then update the order status to replaced' ||
' c. If a refund, inform customer to print out the return shipping label for the defective product, return the product, and update the order status to refund' ||
'5. After the completion of a return or refund, ask if you can help with anything else.' ||
' End the task if user does not need help on anything else",
"tools": ["Update_Order_Status_Tool"]}'
);
END;
/
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TEAM(
team_name => 'Return_Agency_Team',
attributes => '{"agents": [{"name":"Customer_Return_Agent","task" : "Handle_Product_Return_Task"}],
"process": "sequential"}');
END;
/
PL/SQL procedure successfully completed.
SQL> EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('Return_Agency_Team');
PL/SQL procedure successfully completed.
--Validate that the agent team is set in the current session with get_team
SQL> SELECT DBMS_CLOUD_AI_AGENT.GET_TEAM from DUAL;
--Interact with the agent
SQL> select ai agent I want to return a smartphone case;
RESPONSE
Could you please tell me the reason for returning the smartphone case? Is it no longer needed, arrived too late, box broken, or defective?
SQL> select ai agent the item is defective
RESPONSE
I'm sorry to hear that the smartphone case is defective. Would you like a replacement or a refund?
SQL> select ai agent I will need a replacement
RESPONSE
Can you please confirm your name and order number so I can update the status and process the replacement?
SQL> select ai agent I''m Bob Martinez and my order number is 7820
RESPONSE
Is there anything else I can help you with?
SQL> select ai agent No, I''m all set. Thanks
RESPONSE
Thank you for reaching out, Bob. I'm glad I could assist with the return of your smartphone case and stand. If you have any further questions or need assistance in the future, feel free to contact us. Have a great day!如果使用 Web SQL 客户机(例如 Database Actions ),请使用 DBMS_CLOUD_AI_AGENT.RUN_TEAM 函数运行代理团队,并在函数中提供提示。
注意:
请勿在 Database Actions 或 APEX Service 中使用 DBMS_CLOUD_AI_AGENT.SET_TEAM。而是使用 DBMS_CLOUD_AI_AGENT.RUN_TEAM 的 team_name 参数指定代理团队。
以下示例创建一个对话 ID 以跟踪客户对话历史记录,然后使用 DBMS_CLOUD_AI_AGENT.RUN_TEAM 函数与产品退货代理进行交互。
CREATE OR REPLACE PACKAGE my_globals IS
l_team_cov_id varchar2(4000);
END my_globals;
/
-- Create conversation
DECLARE
l_team_cov_id varchar2(4000);
BEGIN
l_team_cov_id := DBMS_CLOUD_AI.create_conversation();
my_globals.l_team_cov_id := l_team_cov_id;
DBMS_OUTPUT.PUT_LINE('Created conversation with ID: ' || my_globals.l_team_cov_id);
END;
--Interact with the agent
DECLARE
v_response VARCHAR2(4000);
BEGIN
v_response := DBMS_CLOUD_AI_AGENT.RUN_TEAM(
team_name => 'Return_Agency_Team',
user_prompt => 'I want to return a smartphone case',
params => '{"conversation_id": "' || my_globals.l_team_cov_id || '"}'
);
DBMS_OUTPUT.PUT_LINE(v_response);
END;
示例:提取和分析日志报告
此示例说明如何使用两个代理 - 任务对创建代理团队来检索日志和分析日志。
- 用于故障排除的双代理工作流:Data_Engineer 和 Ops_Manager
- 一个工具:
log-fetcher
注意:
只有 DBA 可以运行 EXECUTE 特权和网络 ACL 过程。
在本示例中,DBA 为 AI 提供者的 DBMS_CLOUD_AI_AGENT 软件包、DBMS_CLOUD_AI 软件包和 ACL 访问授予 EXECUTE 特权后,首先创建 Data_Engineer 和 Ops_Manager 代理。
创建代理
创建名为 Data_Engineer 的代理,其配置文件 (GOOGLE) 使用 Google 作为 AI 提供者和角色来检索和处理复杂数据。
使用配置文件 (OPENAI) 创建名为 Ops_Manager 的代理,该配置文件使用 OpenAI 作为 AI 提供者和角色来分析数据。
创建工具栏
然后,创建代理工具:log_fetcher:返回给定日期之后的日志条目。这使用定制过程 fetch_logs。
创建的任务
您可以定义两个任务 fetch_logs_task 和 analyze_log_task 来指导流。fetch_logs_task 调用 log_fetcher 以根据请求检索日志。analyze_log_task 分析提取的日志。
创建团队
创建具有 Data_Engineer 和 Ops_Manager 的 Ops_Issues_Solution_Team 团队以按顺序运行。
运行 Select AI Agent
现在,您可以设置活动团队,并使用 select ai agent 作为提示的前缀来运行代理团队。
完整的示例如下所示:
--Grants EXECUTE privilege to ADB_USER
--
GRANT EXECUTE on DBMS_CLOUD_AI_AGENT to ADB_USER;
GRANT EXECUTE on DBMS_CLOUD_AI to ADB_USER;
-- Grant Network ACL for OpenAI endpoint
BEGIN
DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'api.openai.com',
ace => xs$ace_type(privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db)
);
END;
/
--Grant Network ACL for Google endpoint
BEGIN
DBMS_NETWORK_ACL_ADB_USER.APPEND_HOST_ACE(
host => 'generativelanguage.googleapis.com',
ace => xs$ace_type(privilege_list => xs$name_list('http'),
principal_name => 'ADB_USER',
principal_type => xs_acl.ptype_db)
);
END;
/
PL/SQL procedure successfully completed.
--Create a table with Database logs and insert sample data
SQL> CREATE TABLE app_logs (
log_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
log_timestamp DATE,
log_message VARCHAR2(4000)
);
Table created.
SQL> INSERT INTO app_logs (log_timestamp, log_message) VALUES (
TO_DATE('2025-03-22 03:15:45', 'YYYY-MM-DD HH24:MI:SS'),
'INFO: Database Cluster: Failover completed successfully. Standby promoted to primary. Downtime duration: 33 seconds. Correlation ID: dbfailover102.'
);
1 row created.
SQL> INSERT INTO app_logs (log_timestamp, log_message) VALUES (
TO_DATE('2025-03-23 08:44:10', 'YYYY-MM-DD HH24:MI:SS'),
'INFO: Switchover Process: Synchronization restored. Performing scheduled switchover. Correlation ID: dbswitchover215.'
);
1 row created.
SQL> INSERT INTO app_logs (log_timestamp, log_message) VALUES (
TO_DATE('2025-03-24 03:15:12', 'YYYY-MM-DD HH24:MI:SS'),
'ERROR: Database Cluster: Primary database unreachable, initiating failover to standby. Correlation ID: dbfailover102.'
);
1 row created.
SQL> COMMIT;
Commit complete.
-- create data engineer agent
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
agent_name => 'Data_Engineer',
attributes => '{"profile_name": "GOOGLE",
"role": "You are a specialized data ingestion engineer with expertise in ' ||
'retrieving and processing data from complex database systems."
}'
);
END;
/
PL/SQL procedure successfully completed.
-- create ops manager agent
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_AGENT(
agent_name => 'Ops_Manager',
attributes => '{"profile_name": "OPENAI",
"role": "You are an experienced Ops manager who excels at analyzing ' ||
'complex log data, diagnosing the issues."
}'
);
END;
/
PL/SQL procedure successfully completed.
-- create log_fetcher tool
-- fetch_logs is a customized procedure.
-- Please make sure you have created your own procedure before use it in the tool
--Create a customized fetch_logs procedure
SQL> CREATE OR REPLACE FUNCTION fetch_logs(since_date IN DATE) RETURN CLOB IS
l_logs CLOB;
BEGIN
SELECT JSON_ARRAYAGG(log_message RETURNING CLOB)
INTO l_logs
FROM app_logs
WHERE log_timestamp >= since_date
ORDER BY log_timestamp;
RETURN l_logs;
EXCEPTION
WHEN OTHERS THEN
RETURN 'Error fetching logs: ' || SQLERRM;
END fetch_logs;
/
Function created.
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TOOL(
tool_name => 'log_fetcher',
attributes => '{"instruction": "retrieves and returns all log messages with a LOG_TIMESTAMP greater than or equal to the input date.",
"function": "fetch_logs"}'
);
END;
/
PL/SQL procedure successfully completed.
-- create task with log fetcher tool
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.CREATE_TASK(
task_name => 'fetch_logs_task',
attributes => '{"instruction": "Fetch the log entries from the database based on user request: {query}}.",
"tools": ["log_fetcher"]}'
);
END;
/
PL/SQL procedure successfully completed.
-- create task with predefined rag and slack tool
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.create_task(
task_name => 'analyze_log_task',
attributes => '{"instruction": "Analyze the fetched log entries retrieved based on user request: {query} ' ||
'Generate a detailed report include issue analysis and possible solution.",
"input" : "fetch_logs_task"
}'
);
END;
/
PL/SQL procedure successfully completed.
-- create team and set agent team
SQL> BEGIN
DBMS_CLOUD_AI_AGENT.create_team(
team_name => 'Ops_Issues_Solution_Team',
attributes => '{"agents": [{"name":"Data_Engineer","task" : "fetch_logs_task"},
{"name":"Ops_Manager","task" : "analyze_log_task"}],
"process": "sequential"
}');
END;
/
PL/SQL procedure successfully completed.
SQL> EXEC DBMS_CLOUD_AI_AGENT.SET_TEAM('Ops_Issues_Solution_Team');
PL/SQL procedure successfully completed.
SQL> select ai agent fetch and analyze the logs after 03/15 2025;
RESPONSE
-----------------------------------------------------------------------------------------------------------------------------------------------
1. Issue: High volume of 500 Internal Server Errors between 03/22 and 03/24.
Solution: Review server application logs to identify failing components; add better exception handling and fallback mechanisms to prevent service crashes.
2. Issue: Increased response time on /api/v1/user and /checkout, peaking on 03/25.
Solution: Profile backend queries and services, apply caching where applicable, and offload static content to a CDN.
3. Issue: Detected brute-force login attack with over 500 failed POST attempts from a single IP.
Solution: Add rate-limiting, temporary IP bans, and CAPTCHA on the /login endpoint to prevent credential stuffing.
4. Issue: Suspicious User-Agent headers such as curl/7.58.0 or empty headers mimicking mobile devices.
Solution: Block malformed or uncommon User-Agent strings and log them for threat intelligence analysis.
5. Issue: DDoS-like traffic spike from distributed IPs observed on 03/20.
Solution: Enable DDoS protection at the CDN or cloud provider level, and configure autoscaling to absorb burst traffic.示例:创建定制的 HTTP 工具
此示例定义一个 PL/SQL 函数,该函数发送 HTTP GET 请求并返回响应文本。然后,该函数将添加到“Select AI Agent(选择 AI 代理)”工具中。
您可以创建一个定制工具,该工具使用 DBMS_CLOUD.SEND_REQUEST 过程发送 REST 请求并返回作为文本的响应。此示例说明如何编写 PL/SQL 函数并将其添加为用于代理任务的工具。
函数 get_url_content 将 GET 请求发送到目标 URL 并以 CLOB 形式返回响应。DBMS_CLOUD_AI_AGENT.CREATE_TOOL 使用 get_url_content 函数并创建名为 HTTP_TOOL 的工具,当任务需要从 URL 提取内容时,Select AI 代理可以调用该工具。
CREATE OR REPLACE FUNCTION get_url_content (
url IN CLOB
) RETURN CLOB
AS
l_resp DBMS_CLOUD_TYPES.RESP;
l_result CLOB;
BEGIN
l_resp := DBMS_CLOUD.SEND_REQUEST(
credential_name => NULL,
method => 'GET',
uri => url
);
l_result := DBMS_VECTOR_CHAIN.UTL_TO_TEXT(DBMS_CLOUD.GET_RESPONSE_RAW(l_resp);
RETURN l_result;
END get_url_content;
/
BEGIN
DBMS_CLOUD_AI_AGENT.create_tool(
tool_name => 'HTTP_TOOL',
attributes => '{
"instruction": "This tool fetches and returns the plain text content from the specified URL. ",
"function": "get_url_content"
}'
);
END;
/示例:查看最近团队运行的代理提示和响应
此示例通过显示使用历史视图和对话数据运行的最近座席团队的提示、响应和座席想法,帮助调试最近的“选择 AI 代理”活动。
此示例从最近的 Select AI Agent 团队运行中检索并显示详细日志。该示例使用 USER_AI_AGENT_TEAM_HISTORY、USER_AI_AGENT_TASK_HISTORY 和 USER_CLOUD_AI_CONVERSATION_PROMPTS 视图显示每个代理的团队名称、任务、关联的提示和响应。使用此查询可查看输入提示和最新团队运行生成的响应,从而对问题进行故障排除。
WITH latest_team AS (
SELECT team_exec_id, team_name, start_date
FROM user_ai_agent_team_history
ORDER BY start_date DESC
FETCH FIRST 1 ROW ONLY
),
latest_task AS (
SELECT team_exec_id, task_name, agent_name, conversation_params, start_date,
ROW_NUMBER() OVER (PARTITION BY team_exec_id, task_name, agent_name
ORDER BY start_date DESC) as rn
FROM user_ai_agent_task_history
)
SELECT
team.team_name,
task.task_name,
task.agent_name,
p.prompt,
p.prompt_response
FROM latest_team team
JOIN latest_task task
ON team.team_exec_id = task.team_exec_id
AND task.rn = 1
LEFT JOIN user_cloud_ai_conversation_prompts p
ON p.conversation_id = JSON_VALUE(task.conversation_params, '$.conversation_id')
ORDER BY task.start_date DESC NULLS LAST,
p.created DESC NULLS LAST;USER_AI_AGENT_TOOL_HISTORY、USER_AI_AGENT_TASK_HISTORY 和 USER_AI_AGENT_TASK_HISTORY 视图。--View the tool history
select * from USER_AI_AGENT_TOOL_HISTORY
order by START_DATE desc
--View the task history
select * from USER_AI_AGENT_TASK_HISTORY
order by START_DATE desc
--View the team history
select * from USER_AI_AGENT_TEAM_HISTORY
order by START_DATE desc示例:恢复从 WAITING_FOR_HUMAN 状态运行的代理团队
此示例说明在任务历史记录状态为 WAITING_FOR_HUMAN 时如何恢复代理团队运行。当代理团队进入 WAITING_FOR_HUMAN 状态时,它将暂停,直到用户提供下一个输入或确认。可以根据使用代理团队的方式恢复运行:在 SQL 命令行上或通过 DBMS_CLOUD_AI_AGENT.RUN_TEAM 恢复。
如果在 SQL 命令行上使用 SELECT AI AGENT <prompt>,则选择 AI 代理将自动恢复暂停的团队运行。将维护对话上下文(包括以前的提示和推理),而无需其他参数。
如果使用的是 DBMS_CLOUD_AI_AGENT.RUN_TEAM 过程,则可以通过传递初始运行中使用的同一 conversation_id 来恢复暂停的代理团队。座席团队运行状态(包括待定操作和上下文)将得到保留,座席团队将完全从停止的位置继续。
有关使用 DBMS_CLOUD_AI_AGENT.RUN_TEAM 函数的示例,请参见。
--Interact with the agent
DECLARE
v_response VARCHAR2(4000);
BEGIN
v_response := DBMS_CLOUD_AI_AGENT.RUN_TEAM(
team_name => '<same initial team name>',
user_prompt => 'response to request',
params => '{"conversation_id": "<same initial conversation_id>"}'
);
DBMS_OUTPUT.PUT_LINE(v_response);
END;For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
获得 Oracle 支持
Oracle 客户访问和使用 Oracle 支持服务将遵循其适用服务的 Oracle 订单中规定的条款和条件。