Skip to content

Commit b1a2600

Browse files
authored
Merge pull request #16 from martygubar/main
Fixed bug in Explain. And added audit for profile name
2 parents 66f9edf + 1776118 commit b1a2600

File tree

1 file changed

+88
-13
lines changed

1 file changed

+88
-13
lines changed

apex/select-ai-chat/f101.sql

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ prompt APPLICATION 101 - ADB Chat
3333
-- Application Export:
3434
-- Application: 101
3535
-- Name: ADB Chat
36-
-- Date and Time: 23:34 Thursday February 13, 2025
36+
-- Date and Time: 19:45 Thursday February 20, 2025
3737
-- Exported By: ADBCHAT
3838
-- Flashback: 0
3939
-- Export Type: Application Export
@@ -242524,6 +242524,7 @@ wwv_flow_imp_page.create_page_process(
242524242524
' l_has_summary number;',
242525242525
' l_prompt varchar2(4000);',
242526242526
' v_profile_name varchar2(100);',
242527+
' v_summary_profile_name varchar2(100);',
242527242528
' l_profile_names user_cloud_ai_profiles.profile_name%type := lower(APEX_UTIL.GET_PREFERENCE( ',
242528242529
' p_preference => ''CLOUD_AI_PROFILE'',',
242529242530
' p_user => :APP_USER));',
@@ -242532,9 +242533,18 @@ wwv_flow_imp_page.create_page_process(
242532242533
' -- Is this a chat or ask database?',
242533242534
' b_is_chat := case when upper(:P1_ASK_ADB) = ''Y'' then false else true end;',
242534242535
'',
242536+
' -- Get the AI profile for this prompt. It will choose from the list of profiles that were selected by the user',
242537+
' v_profile_name:= ADB_CHAT.get_profile(',
242538+
' profile_names => JSON(l_profile_names),',
242539+
' prompt => :P1_PROMPT,',
242540+
' is_chat => b_is_chat',
242541+
' );',
242542+
'',
242543+
' v_profile_name := UPPER(v_profile_name);',
242544+
'',
242535242545
' -- Generate response using prompt and profiles',
242536242546
' j_response:= ADB_CHAT.generate(',
242537-
' profile_names => JSON(l_profile_names),',
242547+
' profile_name => v_profile_name,',
242538242548
' prompt => :P1_PROMPT,',
242539242549
' is_chat => b_is_chat',
242540242550
' );',
@@ -242553,8 +242563,8 @@ wwv_flow_imp_page.create_page_process(
242553242563
'',
242554242564
' if l_has_summary = 0 then',
242555242565
' -- Yes! Generate the summary b/c one does not exist.',
242556-
' -- get a profile to generate the response:',
242557-
' v_profile_name:= ADB_CHAT.get_profile(',
242566+
' -- get a profile to generate the response. ',
242567+
' v_summary_profile_name:= ADB_CHAT.get_profile(',
242558242568
' profile_names => JSON(l_profile_names),',
242559242569
' prompt => :P1_PROMPT,',
242560242570
' is_chat => true',
@@ -242563,7 +242573,7 @@ wwv_flow_imp_page.create_page_process(
242563242573
' l_prompt := dbms_cloud_ai.generate(',
242564242574
' prompt => ''Rewrite as a short title : ''||:P1_PROMPT,',
242565242575
' action => ''chat'',',
242566-
' profile_name => v_profile_name',
242576+
' profile_name => v_summary_profile_name',
242567242577
' );',
242568242578
'',
242569242579
' ',
@@ -242572,8 +242582,8 @@ wwv_flow_imp_page.create_page_process(
242572242582
' ',
242573242583
' end if;',
242574242584
'',
242575-
' insert into ADB_CHAT_PROMPTS (conv_id, prompt, response, asked_on, showsql) ',
242576-
' values (:p1_conv_id, :p1_prompt, c_response, systimestamp, c_sql);',
242585+
' insert into ADB_CHAT_PROMPTS (conv_id, profile_name, prompt, response, asked_on, showsql) ',
242586+
' values (:p1_conv_id, v_profile_name, :p1_prompt, c_response, systimestamp, c_sql);',
242577242587
'',
242578242588
'end;'))
242579242589
,p_process_clob_language=>'PLSQL'
@@ -243663,6 +243673,9 @@ wwv_flow_imp_page.create_page(
243663243673
' word-wrap: break-word;',
243664243674
' font-family: inherit;',
243665243675
' font-size: inherit;',
243676+
'}',
243677+
'.CodeMirror{',
243678+
'height : 45vh!important',
243666243679
'}'))
243667243680
,p_page_template_options=>'#DEFAULT#'
243668243681
,p_protection_level=>'C'
@@ -243716,8 +243729,9 @@ wwv_flow_imp_page.create_page_plug(
243716243729
'begin',
243717243730
'',
243718243731
'',
243719-
' SELECT SHOWSQL into l_showsql FROM ADB_CHAT_PROMPTS WHERE ID = :P3_CURRENT_ID;',
243720-
' SELECT PROMPT into l_prompt FROM ADB_CHAT_PROMPTS WHERE ID = :P3_CURRENT_ID;',
243732+
' SELECT SHOWSQL INTO l_showsql FROM ADB_CHAT_PROMPTS WHERE ID = :P3_CURRENT_ID;',
243733+
' SELECT PROMPT INTO l_prompt FROM ADB_CHAT_PROMPTS WHERE ID = :P3_CURRENT_ID;',
243734+
' SELECT PROFILE_NAME INTO l_profile_name FROM ADB_CHAT_PROMPTS WHERE ID = :P3_CURRENT_ID;',
243721243735
'',
243722243736
' -- You can''t get an explanation if the current model was changed to Vector Search',
243723243737
' SELECT count(*) ',
@@ -244460,6 +244474,10 @@ wwv_flow_imp_shared.create_install_script(
244460244474
,p_sequence=>10
244461244475
,p_script_type=>'INSTALL'
244462244476
,p_script_clob=>wwv_flow_string.join(wwv_flow_t_varchar2(
244477+
'drop table if EXISTS ADB_CHAT_PROMPTS;',
244478+
'drop table if EXISTS ADB_CHAT_CONVERSATIONS;',
244479+
'',
244480+
'',
244463244481
' CREATE TABLE "ADB_CHAT_CONVERSATIONS" ',
244464244482
' ( "ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE NOT NULL ENABLE, ',
244465244483
' "SUMMARY" VARCHAR2(4000 CHAR), ',
@@ -244472,6 +244490,7 @@ wwv_flow_imp_shared.create_install_script(
244472244490
' CREATE TABLE "ADB_CHAT_PROMPTS" ',
244473244491
' ( "ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE NOT NULL ENABLE, ',
244474244492
' "CONV_ID" NUMBER NOT NULL ENABLE, ',
244493+
' "PROFILE_NAME" VARCHAR2(400), ',
244475244494
' "PROMPT" VARCHAR2(4000 CHAR), ',
244476244495
' "RESPONSE" CLOB, ',
244477244496
' "ASKED_ON" TIMESTAMP (6), ',
@@ -244485,6 +244504,7 @@ wwv_flow_imp_shared.create_install_script(
244485244504
'',
244486244505
' CREATE INDEX "ADB_CHAT_PROMPTS_I1" ON "ADB_CHAT_PROMPTS" ("CONV_ID") ',
244487244506
' ;',
244507+
'',
244488244508
'create or replace PACKAGE adb_chat AS',
244489244509
' /**',
244490244510
' * Generate SQL or return a response from the given prompt. Pass multiple profile names to if you want to search across multiple domains.',
@@ -244499,6 +244519,18 @@ wwv_flow_imp_shared.create_install_script(
244499244519
' FUNCTION generate(profile_names JSON, prompt clob, is_chat boolean default false) RETURN JSON;',
244500244520
'',
244501244521
' /**',
244522+
' * Generate SQL or return a response from the given prompt. Pass a single profile name.',
244523+
' * If you do pass multiple profile names, you will need to provide a description when define the profile using dbms_cloud_ai.create_profile(description => ''xx'')',
244524+
' * @param profile_name : A single profile name that will be used to generate the response',
244525+
' * @param prompt : The question you would like to get an answer to',
244526+
' * @return : A JSON object with the fillowing attributes: ',
244527+
' sql : the generated SQL for a natural language query',
244528+
' response : the narrated response to a question. ',
244529+
' error : true | false. There was an error processing the request.',
244530+
' */',
244531+
' FUNCTION generate(profile_name VARCHAR2, prompt clob, is_chat boolean default false) RETURN JSON;',
244532+
'',
244533+
' /**',
244502244534
' * Returns the profile name that will be used to process the prompt. See above for details',
244503244535
' * This can be called independently. It is called by the generate function.',
244504244536
' * @param profile_names : List of profile names in a JSON array ["profile1", "profile2"]',
@@ -244520,8 +244552,11 @@ wwv_flow_imp_shared.create_install_script(
244520244552
'/',
244521244553
'',
244522244554
'',
244555+
'',
244556+
'',
244557+
'',
244523244558
'create or replace PACKAGE BODY adb_chat AS',
244524-
' /**',
244559+
' /**',
244525244560
' * Generate SQL or return a response from the given prompt. Pass multiple profile names to if you want to search across multiple domains.',
244526244561
' * If you do pass multiple profile names, you will need to provide a description when define the profile using dbms_cloud_ai.create_profile(description => ''xx'')',
244527244562
' * @param profile_names : List of profile names in a JSON array ["profile1", "profile2"]',
@@ -244541,8 +244576,45 @@ wwv_flow_imp_shared.create_install_script(
244541244576
' -- Get the profile',
244542244577
' v_profile_name := get_profile(profile_names, prompt, is_chat);',
244543244578
'',
244579+
' -- Get the response',
244580+
' j_response := generate(v_profile_name, prompt, is_chat);',
244581+
'',
244582+
' -- Return the JSON doc',
244583+
' RETURN j_response; ',
244584+
' END;',
244585+
'',
244586+
'',
244587+
' /**',
244588+
' * Generate SQL or return a response from the given prompt. Pass a single profile name.',
244589+
' * If you do pass multiple profile names, you will need to provide a description when define the profile using dbms_cloud_ai.create_profile(description => ''xx'')',
244590+
' * @param profile_name : A single profile name that will be used to generate the response',
244591+
' * @param prompt : The question you would like to get an answer to',
244592+
' * @return : A JSON object with the fillowing attributes: ',
244593+
' sql : the generated SQL for a natural language query',
244594+
' response : the narrated response to a question. ',
244595+
' error : true | false. There was an error processing the request.',
244596+
' */',
244597+
' FUNCTION generate(profile_name VARCHAR2, prompt clob, is_chat boolean default false) RETURN JSON IS',
244598+
' v_profile_name varchar2(100);',
244599+
' v_action VARCHAR2(100);',
244600+
' j_response JSON;',
244601+
' c_response CLOB;',
244602+
' i_uses_vector_index NUMBER;',
244603+
' i_num_profiles NUMBER;',
244604+
' c_sql clob;',
244605+
' i_cursor NUMBER := dbms_sql.open_cursor;',
244606+
'',
244607+
' BEGIN',
244608+
' -- Ensure the profile is valid',
244609+
' v_profile_name := upper(profile_name);',
244610+
'',
244611+
' SELECT COUNT(profile_name)',
244612+
' INTO i_num_profiles',
244613+
' FROM user_cloud_ai_profiles',
244614+
' WHERE profile_name = v_profile_name;',
244615+
'',
244544244616
' -- If CHAT, then simply call the model to get an answer',
244545-
' IF is_chat and v_profile_name is not null THEN',
244617+
' IF is_chat and v_profile_name is not null AND i_num_profiles !=0 THEN',
244546244618
' c_response := dbms_cloud_ai.generate(',
244547244619
' prompt => prompt,',
244548244620
' action => ''chat'',',
@@ -244555,7 +244627,7 @@ wwv_flow_imp_shared.create_install_script(
244555244627
' ''error'' VALUE false',
244556244628
' RETURNING JSON);',
244557244629
'',
244558-
' ELSIF v_profile_name IS NULL THEN',
244630+
' ELSIF v_profile_name IS NULL OR i_num_profiles = 0 THEN',
244559244631
' -- There''s no AI profile, so error out.',
244560244632
' j_response := JSON_OBJECT(',
244561244633
' ''sql'' VALUE null,',
@@ -244590,7 +244662,7 @@ wwv_flow_imp_shared.create_install_script(
244590244662
' ELSE',
244591244663
' -- NL2SQL: generate the sql and then execute it',
244592244664
' c_sql := dbms_cloud_ai.generate(',
244593-
' prompt => prompt || '' format column headings using camel case with spaces.'',',
244665+
' prompt => prompt,',
244594244666
' action => ''showsql'',',
244595244667
' profile_name => v_profile_name',
244596244668
' );',
@@ -244766,6 +244838,9 @@ wwv_flow_imp_shared.create_install_script(
244766244838
'END adb_chat;',
244767244839
'/',
244768244840
'',
244841+
'',
244842+
'',
244843+
'',
244769244844
' '))
244770244845
);
244771244846
wwv_flow_imp_shared.create_install_object(

0 commit comments

Comments
 (0)