{"id":3327,"date":"2024-03-25T10:20:00","date_gmt":"2024-03-25T10:20:00","guid":{"rendered":"https:\/\/www.bohort.nl\/?p=3327"},"modified":"2024-05-23T11:50:39","modified_gmt":"2024-05-23T11:50:39","slug":"using-oracle-select-ai-in-an-apex-application","status":"publish","type":"post","link":"https:\/\/www.bohort.nl\/en\/2024\/03\/25\/using-oracle-select-ai-in-an-apex-application\/","title":{"rendered":"Using Oracle Select AI in an (APEX) Application"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This is a follow-up post on Oracle Select AI. In <a href=\"https:\/\/www.bohort.nl\/en\/2024\/03\/23\/introducing-oracle-select-ai\/\">my previous post <\/a>I explained how to setup Oracle Select AI, and how that enables you to chat with your database. In this post I will take it one step further and show you how Select AI can be used in an application. I can be an application built in any technology. Read along and benefit from the below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Secret is Simple<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As you cannot use Select AI itself from within an application, Oracle added the &#8220;GENERATE&#8221; function to the DBMS_CLOUD_AI package. This function provides AI translation in a stateless manner. With your existing AI profile, you can use this function to perform the supported actions such as showsql, narrate, or chat. The default action is showsql.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewbox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"DBMS_CLOUD_AI.GENERATE(                    prompt =&gt; 'what is the total number of customers\u2019,                     profile_name =&gt; 'openai_gpt35\u2019,                     action =&gt; 'showsql')\" style=\"color:#D4D4D4;display:none\" aria-label=\"Kopieer\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #4FC1FF\">DBMS_CLOUD_AI<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">GENERATE<\/span><span style=\"color: #D4D4D4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #9CDCFE\">prompt<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">=&gt;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;what is the total number of customers\u2019,<\/span><span style=\"color: #F44747\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #9CDCFE\">profile_name<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">=&gt;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;openai_gpt35\u2019,<\/span><span style=\"color: #F44747\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #9CDCFE\">action<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">=&gt;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;showsql&#39;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8220;GENERATE&#8221; function does <strong>NOT<\/strong> support the <code>runsql<\/code>\u00a0action. If you supply the\u00a0<code>runsql<\/code>\u00a0action, it returns the following error: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ORA-20000: runsql action is not supported by generate function ORA-06512: at \"C##CLOUD$SERVICE.DBMS_CLOUD\", line xxxx <br>ORA-06512: at \"C##CLOUD$SERVICE.DBMS_CLOUD_AI\", line 2696 ORA-06512: at line x<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The reason for this is that allowing arbitrary SQL execution within an AI generation context could pose security risks. With the GENERATE function you will get the SQL Statement that you need, however there is some work to be done in order to actually run the statement in your application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">So what are your Options?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In order to run the SQL that was returned by the &#8220;GENERATE&#8221; function there are several options. In this post I will provide (in my opinion) the easiest. This is to use a Classic APEX Report, which is based on a Function that returns a SQL Statement. It works almost instant, after you provide the PROMPT that can be entered by the user. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"649\" src=\"https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55-1024x649.png\" alt=\"\" class=\"wp-image-3328\" srcset=\"https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55-1024x649.png 1024w, https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55-300x190.png 300w, https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55-768x487.png 768w, https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55-18x12.png 18w, https:\/\/www.bohort.nl\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-23-at-13.37.55.png 1410w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And that is really it. Nothing complex. What is debatable is the format which you show the query result to the user. Maybe you don&#8217;t want a table, maybe a diagram or a form makes more sense, but that is something for another post.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a follow-up post on Oracle Select AI. In my previous post I explained how to setup Oracle Select AI, and how that enables you to chat with your&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[91,93,73,1],"tags":[],"class_list":["post-3327","post","type-post","status-publish","format-standard","hentry","category-ai","category-apex","category-apex-world","category-niet-gecategoriseerd"],"_links":{"self":[{"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/posts\/3327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/comments?post=3327"}],"version-history":[{"count":2,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/posts\/3327\/revisions"}],"predecessor-version":[{"id":3330,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/posts\/3327\/revisions\/3330"}],"wp:attachment":[{"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/media?parent=3327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/categories?post=3327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bohort.nl\/en\/wp-json\/wp\/v2\/tags?post=3327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}