Partie 2: Oracle APEX avec Ollama
Dans cette seconde partie nous explorons l'utilisation du service Ollama dans Oracle APEX en développant une page qui accepte en entrée une requête en langage naturel et qui la traduit vers une requête SQL pour afficher le résultat de celle-ci dans un tableau.
NL2SQL
La conversion du langage naturel vers SQL suivi de l'affichage du résultat présente deux challenges techniques principaux:
qwen2.5-coder:7b
> ollama pull qwen2.5-coder:7b
#!/bin/bash echo "Starting Ollama..." ollama serve & echo "Waiting for Ollama to be active..." while [ "$(ollama list | grep 'NAME')" == "" ]; do sleep 1 done ollama pull qwen2.5-coder:7b
Ne pas oublier de mettre à jour App Builder > Workspace Utilities > Generative AI Services > AI Model pour pointer sur le nouveau modèle: qwen2.5-coder:7b
Le prompt
declare
l_system_prompt clob;
l_owner varchar2(100) := 'DEFAULT_SCHEMA';
l_ddl clob;
l_relationships_comment clob;
begin
:P1_QUERY_OK_YN := 'N';
l_system_prompt :=
'
Your task is to convert a question into an SQL SELECT query.
This query will run on the following database tables:
{{sql_ddl_statement}}
DO NOT GENERATE REASONING.
DO NOT FORMAT CODE.
DO NOT COMMENT THE CODE.
';
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'STORAGE', false);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'TABLESPACE', false);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'CONSTRAINTS', false);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'REF_CONSTRAINTS', true);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'SEGMENT_ATTRIBUTES', false);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'EMIT_SCHEMA', false);
dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'SQLTERMINATOR', true);
for t in (
select
ata.table_name,
dbms_metadata.get_ddl('TABLE', ata.table_name, l_owner) as create_statement
from
all_tables ata
left join user_tab_comments utc on utc.table_name = ata.table_name
where
ata.owner = l_owner
)
loop
l_ddl := l_ddl || t.create_statement;
end loop;
:P1_SYSTEM_PROMPT := replace(l_system_prompt, '{{sql_ddl_statement}}', l_ddl);
end;
Invocation du modèle
declare
l_context apex_exec.t_context;
l_columns apex_exec.t_columns;
l_column apex_exec.t_column;
begin
if :P1_GENERATED_SQL is null then
return;
end if;
APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY (
p_collection_name => 'SEARCH_RESULTS_COLL',
p_query => :P1_GENERATED_SQL,
p_truncate_if_exists => 'YES'
);
l_context := apex_exec.open_query_context(p_location => apex_exec.c_location_local_db, p_sql_query => :P1_GENERATED_SQL);
l_columns := apex_exec.get_columns(p_context => l_context);
:P1_QUERY_COLUMNS_NAMES := '';
:P1_QUERY_COLUMNS_COUNT := apex_exec.get_column_count(p_context => l_context);
for i in 1..apex_exec.get_column_count(p_context => l_context)
loop
l_column := apex_exec.get_column (p_context => l_context, p_column_idx => i);
:P1_QUERY_COLUMNS_NAMES := :P1_QUERY_COLUMNS_NAMES || l_column.name || ',';
end loop;
:P1_QUERY_COLUMNS_NAMES := substr(:P1_QUERY_COLUMNS_NAMES, 1, length(:P1_QUERY_COLUMNS_NAMES) - 1);
:P1_QUERY_OK_YN := 'Y';
exception when others then
--raise_application_error(-20001, sqlerrm);
:P1_QUERY_OK_YN := 'N';
end;
const columns = $v('P1_QUERY_COLUMNS_NAMES').split(','); const htmlTableId = $('#IR_RESULTS .a-IRR-table').attr('id'); $('a.a-IRR-headerLink').each( function(i, v) { $(this).text(columns[i]); });
Conclusion
Libellés : I.A, intelligence artificielle, ollama, oracle apex




