@@ -33,7 +33,7 @@ prompt APPLICATION 101 - ADB Chat
33
33
-- Application Export:
34
34
-- Application: 101
35
35
-- Name: ADB Chat
36
- -- Date and Time: 23:34 Thursday February 13 , 2025
36
+ -- Date and Time: 19:45 Thursday February 20 , 2025
37
37
-- Exported By: ADBCHAT
38
38
-- Flashback: 0
39
39
-- Export Type: Application Export
@@ -242524,6 +242524,7 @@ wwv_flow_imp_page.create_page_process(
242524
242524
' l_has_summary number;',
242525
242525
' l_prompt varchar2(4000);',
242526
242526
' v_profile_name varchar2(100);',
242527
+ ' v_summary_profile_name varchar2(100);',
242527
242528
' l_profile_names user_cloud_ai_profiles.profile_name%type := lower(APEX_UTIL.GET_PREFERENCE( ',
242528
242529
' p_preference => ''CLOUD_AI_PROFILE'',',
242529
242530
' p_user => :APP_USER));',
@@ -242532,9 +242533,18 @@ wwv_flow_imp_page.create_page_process(
242532
242533
' -- Is this a chat or ask database?',
242533
242534
' b_is_chat := case when upper(:P1_ASK_ADB) = ''Y'' then false else true end;',
242534
242535
'',
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
+ '',
242535
242545
' -- Generate response using prompt and profiles',
242536
242546
' j_response:= ADB_CHAT.generate(',
242537
- ' profile_names => JSON(l_profile_names) ,',
242547
+ ' profile_name => v_profile_name ,',
242538
242548
' prompt => :P1_PROMPT,',
242539
242549
' is_chat => b_is_chat',
242540
242550
' );',
@@ -242553,8 +242563,8 @@ wwv_flow_imp_page.create_page_process(
242553
242563
'',
242554
242564
' if l_has_summary = 0 then',
242555
242565
' -- 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(',
242558
242568
' profile_names => JSON(l_profile_names),',
242559
242569
' prompt => :P1_PROMPT,',
242560
242570
' is_chat => true',
@@ -242563,7 +242573,7 @@ wwv_flow_imp_page.create_page_process(
242563
242573
' l_prompt := dbms_cloud_ai.generate(',
242564
242574
' prompt => ''Rewrite as a short title : ''||:P1_PROMPT,',
242565
242575
' action => ''chat'',',
242566
- ' profile_name => v_profile_name ',
242576
+ ' profile_name => v_summary_profile_name ',
242567
242577
' );',
242568
242578
'',
242569
242579
' ',
@@ -242572,8 +242582,8 @@ wwv_flow_imp_page.create_page_process(
242572
242582
' ',
242573
242583
' end if;',
242574
242584
'',
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);',
242577
242587
'',
242578
242588
'end;'))
242579
242589
,p_process_clob_language=>'PLSQL'
@@ -243663,6 +243673,9 @@ wwv_flow_imp_page.create_page(
243663
243673
' word-wrap: break-word;',
243664
243674
' font-family: inherit;',
243665
243675
' font-size: inherit;',
243676
+ '}',
243677
+ '.CodeMirror{',
243678
+ 'height : 45vh!important',
243666
243679
'}'))
243667
243680
,p_page_template_options=>'#DEFAULT#'
243668
243681
,p_protection_level=>'C'
@@ -243716,8 +243729,9 @@ wwv_flow_imp_page.create_page_plug(
243716
243729
'begin',
243717
243730
'',
243718
243731
'',
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;',
243721
243735
'',
243722
243736
' -- You can''t get an explanation if the current model was changed to Vector Search',
243723
243737
' SELECT count(*) ',
@@ -244460,6 +244474,10 @@ wwv_flow_imp_shared.create_install_script(
244460
244474
,p_sequence=>10
244461
244475
,p_script_type=>'INSTALL'
244462
244476
,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
+ '',
244463
244481
' CREATE TABLE "ADB_CHAT_CONVERSATIONS" ',
244464
244482
' ( "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, ',
244465
244483
' "SUMMARY" VARCHAR2(4000 CHAR), ',
@@ -244472,6 +244490,7 @@ wwv_flow_imp_shared.create_install_script(
244472
244490
' CREATE TABLE "ADB_CHAT_PROMPTS" ',
244473
244491
' ( "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, ',
244474
244492
' "CONV_ID" NUMBER NOT NULL ENABLE, ',
244493
+ ' "PROFILE_NAME" VARCHAR2(400), ',
244475
244494
' "PROMPT" VARCHAR2(4000 CHAR), ',
244476
244495
' "RESPONSE" CLOB, ',
244477
244496
' "ASKED_ON" TIMESTAMP (6), ',
@@ -244485,6 +244504,7 @@ wwv_flow_imp_shared.create_install_script(
244485
244504
'',
244486
244505
' CREATE INDEX "ADB_CHAT_PROMPTS_I1" ON "ADB_CHAT_PROMPTS" ("CONV_ID") ',
244487
244506
' ;',
244507
+ '',
244488
244508
'create or replace PACKAGE adb_chat AS',
244489
244509
' /**',
244490
244510
' * 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(
244499
244519
' FUNCTION generate(profile_names JSON, prompt clob, is_chat boolean default false) RETURN JSON;',
244500
244520
'',
244501
244521
' /**',
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
+ ' /**',
244502
244534
' * Returns the profile name that will be used to process the prompt. See above for details',
244503
244535
' * This can be called independently. It is called by the generate function.',
244504
244536
' * @param profile_names : List of profile names in a JSON array ["profile1", "profile2"]',
@@ -244520,8 +244552,11 @@ wwv_flow_imp_shared.create_install_script(
244520
244552
'/',
244521
244553
'',
244522
244554
'',
244555
+ '',
244556
+ '',
244557
+ '',
244523
244558
'create or replace PACKAGE BODY adb_chat AS',
244524
- ' /**',
244559
+ ' /**',
244525
244560
' * Generate SQL or return a response from the given prompt. Pass multiple profile names to if you want to search across multiple domains.',
244526
244561
' * 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'')',
244527
244562
' * @param profile_names : List of profile names in a JSON array ["profile1", "profile2"]',
@@ -244541,8 +244576,45 @@ wwv_flow_imp_shared.create_install_script(
244541
244576
' -- Get the profile',
244542
244577
' v_profile_name := get_profile(profile_names, prompt, is_chat);',
244543
244578
'',
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
+ '',
244544
244616
' -- 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',
244546
244618
' c_response := dbms_cloud_ai.generate(',
244547
244619
' prompt => prompt,',
244548
244620
' action => ''chat'',',
@@ -244555,7 +244627,7 @@ wwv_flow_imp_shared.create_install_script(
244555
244627
' ''error'' VALUE false',
244556
244628
' RETURNING JSON);',
244557
244629
'',
244558
- ' ELSIF v_profile_name IS NULL THEN',
244630
+ ' ELSIF v_profile_name IS NULL OR i_num_profiles = 0 THEN',
244559
244631
' -- There''s no AI profile, so error out.',
244560
244632
' j_response := JSON_OBJECT(',
244561
244633
' ''sql'' VALUE null,',
@@ -244590,7 +244662,7 @@ wwv_flow_imp_shared.create_install_script(
244590
244662
' ELSE',
244591
244663
' -- NL2SQL: generate the sql and then execute it',
244592
244664
' c_sql := dbms_cloud_ai.generate(',
244593
- ' prompt => prompt || '' format column headings using camel case with spaces.'' ,',
244665
+ ' prompt => prompt,',
244594
244666
' action => ''showsql'',',
244595
244667
' profile_name => v_profile_name',
244596
244668
' );',
@@ -244766,6 +244838,9 @@ wwv_flow_imp_shared.create_install_script(
244766
244838
'END adb_chat;',
244767
244839
'/',
244768
244840
'',
244841
+ '',
244842
+ '',
244843
+ '',
244769
244844
' '))
244770
244845
);
244771
244846
wwv_flow_imp_shared.create_install_object(
0 commit comments