-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_packages.sql
More file actions
261 lines (199 loc) · 7.69 KB
/
create_packages.sql
File metadata and controls
261 lines (199 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
create or replace package chathistory_pkg is
----------
----------
---------- save conversations
----------
procedure prc_add_conversation(p_username varchar2, p_started_on timestamp, p_app_session number);
----------
---------- save prompt detail as users ask questions
----------
procedure prc_add_prompt(
p_id number
, p_conv_id number
, p_prompt varchar2
, p_response varchar2
, p_asked_on timestamp
, p_chathistory varchar2
, p_references varchar2);
----------
---------- Save embedded details using a model from PCA
----------
procedure prc_cr_doc_vectors_pca;
----------
---------- Save embedded details using a model from Database
----------
procedure prc_cr_doc_vectors_db;
end;
/
----------
---------- save conversations
----------
create or replace package body chathistory_pkg is
---------
procedure prc_add_conversation(p_username varchar2, p_started_on timestamp, p_app_session number) is
pragma autonomous_transaction;
begin
insert into conversations values(conv_seq.nextval, p_username, p_started_on, p_app_session);
commit;
end;
----------
---------- save prompt detail as users ask questions
----------
procedure prc_add_prompt(
p_id number
, p_conv_id number
, p_prompt varchar2
, p_response varchar2
, p_asked_on timestamp
, p_chathistory varchar2
, p_references varchar2) is
pragma autonomous_transaction;
begin
insert into prompts values(
p_id
, p_conv_id
, p_prompt
, p_response
, p_asked_on
, p_chathistory
, p_references);
commit;
end;
----------
---------- chunk document into pieces
---------- Save embedded details using a model from ollama
----------
procedure prc_cr_doc_vectors_pca is
v_count integer;
v_embedding_endpoint varchar2(500) := 'http://{ip address}:11434';
v_api_credential varchar2(100) := 'OLLAMA_CRED';
v_provider_embedding varchar2(100) := 'Ollama';
v_text_endpoint varchar2(100) := '/api/embeddings';
v_model_embed varchar2(100) := 'mxbai-embed-large';
begin
----------
---------- Loop through documents where there are no embedded details
----------
for i in (select l.id
from documents l
where l.id not in (select lv.id from documents_vector_pca lv))
loop
----------
---------- Count current number of embedding (in case another session did the embeddings already)
----------
select count(id) into v_count from monitor_embedding_pca m where i.id = m.id;
----------
---------- If there are still no embeddings (count = 0)
----------
if v_count = 0 then
----------
---------- insert row into monitoring table (and commit) to ensure another session doesn't process this document
---------- Then chunk the document and insert into embeddings table
----------
insert into monitor_embedding_pca values (i.id,'processing');
commit;
insert into documents_vector_pca
select i.id
, json_value(c.column_value, '$.chunk_id' returning number) as chunk_id
, json_value(c.column_value, '$.chunk_offset' returning number) as chunk_pos
, json_value(c.column_value, '$.chunk_length' returning number) as chunk_size
, replace(json_value(c.column_value, '$.chunk_data'),chr(10),'') as chunk_txt
, dbms_vector_chain.utl_to_embedding(json_value(c.column_value, '$.chunk_data'), json('{
"provider": "' || v_provider_embedding || '",
"credential_name": "' || v_api_credential || '",
"url": "' || v_embedding_endpoint || v_text_endpoint || '",
"model": "' || v_model_embed || '",
"batch_size":10
}')) embed_vector
from
------- doc to text query ---------
(select id
, dbms_vector_chain.utl_to_text (l.file_content, json('{"plaintext":"true","charset":"utf8"}')) file_text
from documents l where id=l.id) t,
------- chunking ---------
dbms_vector_chain.utl_to_chunks(t.file_text,
json('{ "by":"words",
"max":"200",
"overlap":"0",
"split":"sentence",
"language":"american",
"normalize":"all" }')) c
where i.id=t.id;
commit;
end if;
end loop;
end;
----------
---------- chunk document into pieces
---------- Save embedded details using a model from Database
----------
procedure prc_cr_doc_vectors_db is
v_count integer;
v_db_provider varchar2(100) := 'database';
v_db_model_embed varchar2(100) := 'ALL_MINILM_L12_V2';
begin
----------
---------- Loop through documents where there are no embedded details
----------
for i in (select l.id
from documents l
where l.id not in (select lv.id from documents_vector_db lv))
loop
----------
---------- Count current number of embedding (in case another session did the embeddings already)
----------
select count(id) into v_count from monitor_embedding_db m where i.id = m.id;
----------
---------- If there are still no embeddings (count = 0)
----------
if v_count = 0 then
----------
---------- insert row into monitoring table (and commit) to ensure another session doesn't process this document
---------- Then chunk the document and insert into embeddings table
----------
insert into monitor_embedding_db values (i.id,'processing');
commit;
insert into documents_vector_db
select i.id
, json_value(c.column_value, '$.chunk_id' returning number) as chunk_id
, json_value(c.column_value, '$.chunk_offset' returning number) as chunk_pos
, json_value(c.column_value, '$.chunk_length' returning number) as chunk_size
, replace(json_value(c.column_value, '$.chunk_data'),chr(10),'') as chunk_txt
, dbms_vector.utl_to_embedding(json_value(c.column_value, '$.chunk_data')
, json('{
"provider":"database",
"model": "ALL_MINILM_L12_V2"
}')) embed_vector
from
------- doc to text query ---------
(select id
, dbms_vector_chain.utl_to_text (l.file_content, json('{"plaintext":"true","charset":"utf8"}')) file_text
from documents l where id=l.id) t,
------- chunking ---------
dbms_vector_chain.utl_to_chunks(t.file_text,
json('{ "by":"words",
"max":"200",
"overlap":"0",
"split":"sentence",
"language":"american",
"normalize":"all" }')) c
where i.id=t.id;
commit;
end if;
end loop;
end;
-------------------------
end;
/
CREATE OR REPLACE EDITIONABLE TRIGGER "TRG_DOCUMENTS_VECTOR"
after insert on DOCUMENTS
for each row
declare
my_job number;
begin
dbms_job.submit(job => my_job, what => 'chathistory_pkg.prc_cr_doc_vectors_db;');
dbms_job.submit(job => my_job, what => 'chathistory_pkg.prc_cr_doc_vectors_pca;');
end;
/
ALTER TRIGGER "TRG_DOCUMENTS_VECTOR" enable;
/