-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
3442 lines (3269 loc) · 165 KB
/
main.py
File metadata and controls
3442 lines (3269 loc) · 165 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import logging
import os
import platform
import re
from typing import List, Tuple
import gradio as gr
import oracledb
import pandas as pd
from dotenv import load_dotenv, find_dotenv
from gradio.themes import GoogleFont
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from my_langchain_community.chat_models import ChatOCIGenAI
from utils.auth_util import (
do_auth,
create_oci_cred as create_oci_cred_util, create_cohere_cred, create_openai_cred,
create_azure_openai_cred, create_langfuse_cred, test_oci_cred as test_oci_cred_util
)
from utils.chat_document_util import chat_document as chat_document_util, append_citation as append_citation_util
from utils.chat_util import chat_stream
from utils.cleanup_util import (
enable_resource_warnings
)
from utils.common_util import get_region
from utils.css_gradio_util import custom_css
from utils.database_util import create_table as create_table_util
from utils.document_conversion_util import (
convert_pdf_to_markdown, convert_excel_to_text_document, convert_xml_to_text_document, convert_json_to_text_document
)
from utils.document_embed_util import embed_save_document_by_unstructured as embed_save_document_util
from utils.document_loader_util import load_document as load_document_util
from utils.document_management_util import (
search_document as search_document_util, delete_document as delete_document_util,
get_doc_list as get_doc_list_util, get_server_path as get_server_path_util
)
from utils.document_split_util import (
reset_document_chunks_result_dataframe,
split_document_by_unstructured as split_document_util,
update_document_chunks_result_detail_with_validation
)
from utils.download_util import generate_download_file
from utils.embedding_util import (
generate_embedding_response
)
from utils.evaluation_util import eval_by_human as eval_by_human_util, eval_by_ragas, reset_eval_by_human_result
from utils.generator_util import generate_unique_id
from utils.image_processing_util import (
process_single_image_streaming as process_single_image_streaming_util,
process_image_answers_streaming as process_image_answers_streaming_util
)
from utils.prompts_util import (
get_sub_query_prompt, get_rag_fusion_prompt, get_hyde_prompt, get_step_back_prompt,
get_langgpt_rag_prompt, get_llm_evaluation_system_message, get_chat_system_message,
get_query_generation_prompt, update_langgpt_rag_prompt,
get_image_qa_prompt, update_image_qa_prompt
)
from utils.query_util import insert_query_result as insert_query_result_util
from utils.text_util import (
remove_base64_images_from_text
)
# read local .env file
load_dotenv(find_dotenv())
DEFAULT_COLLECTION_NAME = os.environ["DEFAULT_COLLECTION_NAME"]
# ログ設定
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
if platform.system() == 'Linux':
oracledb.init_oracle_client(lib_dir=os.environ["ORACLE_CLIENT_LIB_DIR"])
# データベース接続プールを初期化(ブロッキングを避けるため接続数を増加)
pool = oracledb.create_pool(
dsn=os.environ["ORACLE_23AI_CONNECTION_STRING"],
min=5,
max=20,
increment=2,
timeout=30, # 接続タイムアウト30秒
getmode=oracledb.POOL_GETMODE_WAIT # 利用可能な接続を待機
)
def check_database_pool_health():
"""
データベース接続プールの健康状態をチェックするためのラッパー関数
"""
from utils.system_util import check_database_pool_health as check_db_health
return check_db_health(pool)
def get_doc_list() -> List[Tuple[str, str]]:
"""
データベースからドキュメントリストを取得するためのラッパー関数
"""
return get_doc_list_util(pool, DEFAULT_COLLECTION_NAME)
def refresh_doc_list():
doc_list = get_doc_list()
return (
gr.Radio(choices=doc_list, value=None),
gr.CheckboxGroup(choices=doc_list, value=[]),
gr.CheckboxGroup(choices=doc_list, value=[])
)
def get_server_path(doc_id: str) -> str:
"""
ドキュメントIDからサーバーパスを取得するためのラッパー関数
"""
return get_server_path_util(pool, DEFAULT_COLLECTION_NAME, doc_id)
def set_chat_llm_answer(llm_answer_checkbox):
oci_openai_gpt_5_answer_visible = False
oci_openai_o3_answer_visible = False
oci_openai_gpt_4_1_answer_visible = False
oci_xai_grok_4_answer_visible = False
oci_cohere_command_a_answer_visible = False
oci_meta_llama_4_scout_answer_visible = False
openai_gpt_4o_answer_visible = False
azure_openai_gpt_4o_answer_visible = False
if "oci_openai/gpt-5" in llm_answer_checkbox:
oci_openai_gpt_5_answer_visible = True
if "oci_openai/o3" in llm_answer_checkbox:
oci_openai_o3_answer_visible = True
if "oci_openai/gpt-4.1" in llm_answer_checkbox:
oci_openai_gpt_4_1_answer_visible = True
if "oci_xai/grok-4" in llm_answer_checkbox:
oci_xai_grok_4_answer_visible = True
if "oci_cohere/command-a" in llm_answer_checkbox:
oci_cohere_command_a_answer_visible = True
if "oci_meta/llama-4-scout-17b-16e-instruct" in llm_answer_checkbox:
oci_meta_llama_4_scout_answer_visible = True
if "openai/gpt-4o" in llm_answer_checkbox:
openai_gpt_4o_answer_visible = True
if "azure_openai/gpt-4o" in llm_answer_checkbox:
azure_openai_gpt_4o_answer_visible = True
return (
gr.Accordion(visible=oci_openai_gpt_5_answer_visible),
gr.Accordion(visible=oci_openai_o3_answer_visible),
gr.Accordion(visible=oci_openai_gpt_4_1_answer_visible),
gr.Accordion(visible=oci_xai_grok_4_answer_visible),
gr.Accordion(visible=oci_cohere_command_a_answer_visible),
gr.Accordion(visible=oci_meta_llama_4_scout_answer_visible),
gr.Accordion(visible=openai_gpt_4o_answer_visible),
gr.Accordion(visible=azure_openai_gpt_4o_answer_visible)
)
def set_chat_llm_evaluation(llm_evaluation_checkbox):
oci_openai_gpt_5_evaluation_visible = False
oci_openai_o3_evaluation_visible = False
oci_openai_gpt_4_1_evaluation_visible = False
oci_xai_grok_4_evaluation_visible = False
oci_cohere_command_a_evaluation_visible = False
oci_meta_llama_4_scout_evaluation_visible = False
openai_gpt_4o_evaluation_visible = False
azure_openai_gpt_4o_evaluation_visible = False
if llm_evaluation_checkbox:
oci_openai_gpt_5_evaluation_visible = True
oci_openai_o3_evaluation_visible = True
oci_openai_gpt_4_1_evaluation_visible = True
oci_xai_grok_4_evaluation_visible = True
oci_cohere_command_a_evaluation_visible = True
oci_meta_llama_4_scout_evaluation_visible = True
openai_gpt_4o_evaluation_visible = True
azure_openai_gpt_4o_evaluation_visible = True
return (
gr.Accordion(visible=oci_openai_gpt_5_evaluation_visible),
gr.Accordion(visible=oci_openai_o3_evaluation_visible),
gr.Accordion(visible=oci_openai_gpt_4_1_evaluation_visible),
gr.Accordion(visible=oci_xai_grok_4_evaluation_visible),
gr.Accordion(visible=oci_cohere_command_a_evaluation_visible),
gr.Accordion(visible=oci_meta_llama_4_scout_evaluation_visible),
gr.Accordion(visible=openai_gpt_4o_evaluation_visible),
gr.Accordion(visible=azure_openai_gpt_4o_evaluation_visible),
)
def set_image_answer_visibility(llm_answer_checkbox, use_image):
"""
Vision 回答の可視性を制御する関数
選択されたLLMモデルと「画像を使って回答」の状態に基づいて、
対象のモデルのVision 回答Accordionの可視性を決定する
"""
oci_openai_gpt_5_image_visible = False
oci_openai_o3_image_visible = False
oci_openai_gpt_4_1_image_visible = False
oci_meta_llama_4_scout_image_visible = False
openai_gpt_4o_image_visible = False
azure_openai_gpt_4o_image_visible = False
# 画像を使って回答がオンで、かつ対応するモデルが選択されている場合のみ表示
if use_image:
if "oci_openai/gpt-5" in llm_answer_checkbox:
oci_openai_gpt_5_image_visible = True
if "oci_openai/o3" in llm_answer_checkbox:
oci_openai_o3_image_visible = True
if "oci_openai/gpt-4.1" in llm_answer_checkbox:
oci_openai_gpt_4_1_image_visible = True
if "oci_meta/llama-4-scout-17b-16e-instruct" in llm_answer_checkbox:
oci_meta_llama_4_scout_image_visible = True
if "openai/gpt-4o" in llm_answer_checkbox:
openai_gpt_4o_image_visible = True
if "azure_openai/gpt-4o" in llm_answer_checkbox:
azure_openai_gpt_4o_image_visible = True
return (
gr.Accordion(visible=oci_openai_gpt_5_image_visible),
gr.Accordion(visible=oci_openai_o3_image_visible),
gr.Accordion(visible=oci_openai_gpt_4_1_image_visible),
gr.Accordion(visible=oci_meta_llama_4_scout_image_visible),
gr.Accordion(visible=openai_gpt_4o_image_visible),
gr.Accordion(visible=azure_openai_gpt_4o_image_visible)
)
def reset_all_llm_messages():
"""
すべてのLLMメッセージをリセットする
"""
return (
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_5_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_o3_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_4_1_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_xai_grok_4_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_cohere_command_a_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_meta_llama_4_scout_answer_text
gr.Markdown(value=""), # tab_chat_document_openai_gpt_4o_answer_text
gr.Markdown(value="") # tab_chat_document_azure_openai_gpt_4o_answer_text
)
def reset_image_answers():
"""
Vision 回答をリセットする
"""
return (
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_5_image_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_o3_image_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_4_1_image_answer_text
gr.Markdown(value=""), # tab_chat_document_oci_meta_llama_4_scout_image_answer_text
gr.Markdown(value=""), # tab_chat_document_openai_gpt_4o_image_answer_text
gr.Markdown(value=""), # tab_chat_document_azure_openai_gpt_4o_image_answer_text
)
def reset_llm_evaluations():
"""
LLM評価をリセットする
"""
return (
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_5_evaluation_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_o3_evaluation_text
gr.Markdown(value=""), # tab_chat_document_oci_openai_gpt_4_1_evaluation_text
gr.Markdown(value=""), # tab_chat_document_oci_xai_grok_4_evaluation_text
gr.Markdown(value=""), # tab_chat_document_oci_cohere_command_a_evaluation_text
gr.Markdown(value=""), # tab_chat_document_oci_meta_llama_4_scout_evaluation_text
gr.Markdown(value=""), # tab_chat_document_openai_gpt_4o_evaluation_text
gr.Markdown(value=""), # tab_chat_document_azure_openai_gpt_4o_evaluation_text
)
def create_oci_cred(user_ocid, tenancy_ocid, fingerprint, private_key_file, region):
"""
OCI認証情報を設定するためのラッパー関数
"""
return create_oci_cred_util(user_ocid, tenancy_ocid, fingerprint, private_key_file, region, pool)
def test_oci_cred(test_query_text):
"""
OCI認証情報をテストするためのラッパー関数
"""
return test_oci_cred_util(test_query_text, pool)
def create_table():
"""
Wrapper function for creating database tables using the utility function
"""
output_sql_text = create_table_util(pool, DEFAULT_COLLECTION_NAME)
gr.Info("テーブルの作成が完了しました")
return gr.Accordion(), gr.Textbox(value=output_sql_text.strip())
def load_document(file_path, server_directory, document_metadata):
"""
ドキュメントファイルを読み込み、処理してデータベースに保存する
この関数は utils.document_loader_util モジュールの関数を呼び出すラッパー関数です。
"""
return load_document_util(
file_path, server_directory, document_metadata,
pool, DEFAULT_COLLECTION_NAME, generate_unique_id
)
def reset_document_chunks_result_detail():
return (
gr.Textbox(value=""),
gr.Textbox(value="")
)
def split_document_by_unstructured(doc_id, chunks_by, chunks_max_size,
chunks_overlap_size,
chunks_split_by, chunks_split_by_custom,
chunks_language, chunks_normalize,
chunks_normalize_options):
"""
unstructured形式のドキュメントを分割し、チャンクをデータベースに保存する
この関数は utils.document_split_util モジュールの関数を呼び出すラッパー関数です。
"""
return split_document_util(
doc_id, chunks_by, chunks_max_size,
chunks_overlap_size, chunks_split_by, chunks_split_by_custom,
chunks_language, chunks_normalize, chunks_normalize_options,
pool, DEFAULT_COLLECTION_NAME, get_server_path, generate_embedding_response
)
def on_select_split_document_chunks_result(evt: gr.SelectData, df: pd.DataFrame):
"""
分割ドキュメントチャンク結果の選択イベントハンドラー
この関数は utils.document_split_util モジュールの関数を呼び出すラッパー関数です。
"""
print("on_select_split_document_chunks_result() start...")
selected_index = evt.index[0] # 選択された行のインデックスを取得
selected_row = df.iloc[selected_index] # 選択された行のすべてのデータを取得
return selected_row['CHUNK_ID'], \
selected_row['CHUNK_DATA']
def update_document_chunks_result_detail(doc_id, df: pd.DataFrame, chunk_id, chunk_data):
"""
ドキュメントチャンク結果詳細を更新する
この関数は utils.document_split_util モジュールの関数を呼び出すラッパー関数です。
"""
return update_document_chunks_result_detail_with_validation(
doc_id, df, chunk_id, chunk_data,
pool, DEFAULT_COLLECTION_NAME, generate_embedding_response
)
def embed_save_document_by_unstructured(doc_id, chunks_by, chunks_max_size,
chunks_overlap_size,
chunks_split_by, chunks_split_by_custom,
chunks_language, chunks_normalize,
chunks_normalize_options):
"""
unstructured形式のドキュメントに対して埋め込みベクトルを生成し、データベースに保存する
この関数は utils.document_embed_util モジュールの関数を呼び出すラッパー関数です。
"""
return embed_save_document_util(
doc_id, chunks_by, chunks_max_size,
chunks_overlap_size, chunks_split_by, chunks_split_by_custom,
chunks_language, chunks_normalize, chunks_normalize_options,
pool, DEFAULT_COLLECTION_NAME, get_server_path
)
def generate_query(query_text, generate_query_radio):
has_error = False
if not query_text:
has_error = True
gr.Warning("クエリを入力してください")
if has_error:
return gr.Textbox(value=""), gr.Textbox(value=""), gr.Textbox(value="")
generate_query1 = ""
generate_query2 = ""
generate_query3 = ""
if generate_query_radio == "None":
return gr.Textbox(value=generate_query1), gr.Textbox(value=generate_query2), gr.Textbox(value=generate_query3)
region = get_region()
# if region == "us-chicago-1":
# chat_llm = ChatOCIGenAI(
# model_id="xai.grok-4",
# provider="xai",
# service_endpoint=f"https://inference.generativeai.{region}.oci.oraclecloud.com",
# compartment_id=os.environ["OCI_COMPARTMENT_OCID"],
# model_kwargs={"temperature": 0.0, "top_p": 0.75, "seed": 42, "max_tokens": 2048},
# )
# else:
chat_llm = ChatOCIGenAI(
model_id="cohere.command-a-03-2025",
provider="cohere",
service_endpoint=f"https://inference.generativeai.{region}.oci.oraclecloud.com",
compartment_id=os.environ["OCI_COMPARTMENT_OCID"],
model_kwargs={"temperature": 0.0, "top_p": 0.75, "seed": 42, "max_tokens": 600},
)
# RAG-Fusion
if generate_query_radio == "Sub-Query":
# v1
# sub_query_prompt = ChatPromptTemplate.from_messages([
# ("system",
# """
# You are an advanced assistant that specializes in breaking down complex, multifaceted input queries into more manageable sub-queries.
# This approach allows for each aspect of the query to be explored in depth, facilitating a comprehensive and nuanced response.
# Your task is to dissect the given query into its constituent elements and generate targeted sub-queries that can each be researched or answered individually,
# ensuring that the final response holistically addresses all components of the original query.
# """),
# ("user",
# "Decompose the following query into targeted sub-queries that can be individually explored: {original_query} \n OUTPUT (2 queries): )")
# ])
# v2
sub_query_prompt = ChatPromptTemplate.from_messages([
("system", get_sub_query_prompt()),
("user", get_query_generation_prompt("Sub-Query", "{original_query}"))
])
generate_sub_queries_chain = (
sub_query_prompt | chat_llm | StrOutputParser() | (lambda x: x.split("\n"))
)
sub_queries = generate_sub_queries_chain.invoke({"original_query": query_text})
print(f"{sub_queries=}")
if isinstance(sub_queries, list):
try:
generate_query1 = re.sub(r'^1\. ', '', sub_queries[0])
except (IndexError, TypeError):
generate_query1 = ""
try:
generate_query2 = re.sub(r'^2\. ', '', sub_queries[1])
except (IndexError, TypeError):
generate_query2 = ""
try:
generate_query3 = re.sub(r'^3\. ', '', sub_queries[2])
except (IndexError, TypeError):
generate_query3 = ""
elif generate_query_radio == "RAG-Fusion":
# v1
# rag_fusion_prompt = ChatPromptTemplate.from_messages([
# ("system",
# "You are a helpful assistant that generates multiple similary search queries based on a single input query."),
# ("user", "Generate multiple search queries related to: {original_query} \n OUTPUT (2 queries):")
# ])
# v2
rag_fusion_prompt = ChatPromptTemplate.from_messages([
("system", get_rag_fusion_prompt()),
("user", get_query_generation_prompt("RAG-Fusion", "{original_query}"))
])
generate_rag_fusion_queries_chain = (
rag_fusion_prompt | chat_llm | StrOutputParser() | (lambda x: x.split("\n"))
)
rag_fusion_queries = generate_rag_fusion_queries_chain.invoke({"original_query": query_text})
print(f"{rag_fusion_queries=}")
if isinstance(rag_fusion_queries, list):
try:
generate_query1 = re.sub(r'^1\. ', '', rag_fusion_queries[0])
except (IndexError, TypeError):
generate_query1 = ""
try:
generate_query2 = re.sub(r'^2\. ', '', rag_fusion_queries[1])
except (IndexError, TypeError):
generate_query2 = ""
try:
generate_query3 = re.sub(r'^3\. ', '', rag_fusion_queries[2])
except (IndexError, TypeError):
generate_query3 = ""
elif generate_query_radio == "HyDE":
hyde_prompt = ChatPromptTemplate.from_messages([
("system", get_hyde_prompt()),
("user", get_query_generation_prompt("HyDE", "{original_query}"))
])
generate_hyde_answers_chain = (
hyde_prompt | chat_llm | StrOutputParser() | (lambda x: x.split("\n"))
)
hyde_answers = generate_hyde_answers_chain.invoke({"original_query": query_text})
print(f"{hyde_answers=}")
if isinstance(hyde_answers, list):
try:
generate_query1 = re.sub(r'^1\. ', '', hyde_answers[0])
except (IndexError, TypeError):
generate_query1 = ""
try:
generate_query2 = re.sub(r'^2\. ', '', hyde_answers[1])
except (IndexError, TypeError):
generate_query2 = ""
try:
generate_query3 = re.sub(r'^3\. ', '', hyde_answers[2])
except (IndexError, TypeError):
generate_query3 = ""
elif generate_query_radio == "Step-Back-Prompting":
step_back_prompt = ChatPromptTemplate.from_messages([
("system", get_step_back_prompt()),
("user", get_query_generation_prompt("Step-Back-Prompting", "{original_query}"))
])
generate_step_back_queries_chain = (
step_back_prompt | chat_llm | StrOutputParser() | (lambda x: x.split("\n"))
)
step_back_queries = generate_step_back_queries_chain.invoke({"original_query": query_text})
print(f"{step_back_queries=}")
if isinstance(step_back_queries, list):
try:
generate_query1 = re.sub(r'^1\. ', '', step_back_queries[0])
except (IndexError, TypeError):
generate_query1 = ""
try:
generate_query2 = re.sub(r'^2\. ', '', step_back_queries[1])
except (IndexError, TypeError):
generate_query2 = ""
try:
generate_query3 = re.sub(r'^3\. ', '', step_back_queries[2])
except (IndexError, TypeError):
generate_query3 = ""
elif generate_query_radio == "Customized-Multi-Step-Query":
region = get_region()
select_multi_step_query_sql = f"""
SELECT json_value(dc.cmetadata, '$.file_name') name, de.embed_id embed_id, de.embed_data embed_data, de.doc_id doc_id
FROM {DEFAULT_COLLECTION_NAME}_embedding de, {DEFAULT_COLLECTION_NAME}_collection dc
WHERE de.doc_id = dc.id
ORDER BY vector_distance(de.embed_vector , (
SELECT to_vector(et.embed_vector) embed_vector
FROM
dbms_vector_chain.utl_to_embeddings(:query_text, JSON('{{"provider": "ocigenai", "credential_name": "OCI_CRED", "url": "https://inference.generativeai.{region}.oci.oraclecloud.com/20231130/actions/embedText", "model": "cohere.embed-v4.0"}}')) t,
JSON_TABLE ( t.column_value, '$[*]'
COLUMNS (
embed_id NUMBER PATH '$.embed_id',
embed_data VARCHAR2 ( 4000 ) PATH '$.embed_data',
embed_vector CLOB PATH '$.embed_vector'
)
)
et), COSINE)
"""
select_multi_step_query_sql += "FETCH FIRST 3 ROWS ONLY"
# Prepare parameters for SQL execution
multi_step_query_params = {
"query_text": query_text
}
# For debugging: Print the final SQL command.
# Assuming complete_sql is your SQL string with placeholders like :extend_around_chunk_size, :doc_ids, etc.
query_sql_output = select_multi_step_query_sql
# Manually replace placeholders with parameter values for debugging
for key, value in multi_step_query_params.items():
placeholder = f":{key}"
# For the purpose of display, ensure the value is properly quoted if it's a string
display_value = f"'{value}'" if isinstance(value, str) else str(value)
query_sql_output = query_sql_output.replace(placeholder, display_value)
# Now query_sql_output contains the SQL command with parameter values inserted
print(f"\nQUERY_SQL_OUTPUT:\n{query_sql_output}")
with pool.acquire() as conn:
with conn.cursor() as cursor:
cursor.execute(select_multi_step_query_sql, multi_step_query_params)
multi_step_queries = []
for row in cursor:
# print(f"row: {row}")
multi_step_queries.append(row[2])
print(f"{multi_step_queries=}")
if isinstance(multi_step_queries, list):
generate_query1 = multi_step_queries[0]
generate_query2 = multi_step_queries[1]
generate_query3 = multi_step_queries[2]
return (
gr.Textbox(value=generate_query1),
gr.Textbox(value=generate_query2),
gr.Textbox(value=generate_query3)
)
def search_document(
reranker_model_radio_input,
reranker_top_k_slider_input,
reranker_threshold_slider_input,
threshold_value_slider_input,
top_k_slider_input,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
text_search_checkbox_input,
text_search_k_slider_input,
document_metadata_text_input,
query_text_input,
sub_query1_text_input,
sub_query2_text_input,
sub_query3_text_input,
partition_by_k_slider_input,
answer_by_one_checkbox_input,
extend_first_chunk_size_input,
extend_around_chunk_size_input,
use_image
):
"""
類似度検索を使用して質問に関連する分割を取得するためのラッパー関数
"""
return search_document_util(
pool,
DEFAULT_COLLECTION_NAME,
reranker_model_radio_input,
reranker_top_k_slider_input,
reranker_threshold_slider_input,
threshold_value_slider_input,
top_k_slider_input,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
text_search_checkbox_input,
text_search_k_slider_input,
document_metadata_text_input,
query_text_input,
sub_query1_text_input,
sub_query2_text_input,
sub_query3_text_input,
partition_by_k_slider_input,
answer_by_one_checkbox_input,
extend_first_chunk_size_input,
extend_around_chunk_size_input,
use_image
)
async def chat_document(
search_result,
llm_answer_checkbox,
include_citation,
include_current_time,
use_image,
query_text,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
rag_prompt_template
):
"""
検索結果を使用してLLMとチャットするためのラッパー関数
"""
async for result in chat_document_util(
search_result,
llm_answer_checkbox,
include_citation,
include_current_time,
use_image,
query_text,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
rag_prompt_template
):
yield result
async def append_citation(
search_result,
llm_answer_checkbox,
include_citation,
use_image,
query_text,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
oci_openai_gpt_5_answer_text,
oci_openai_o3_answer_text,
oci_openai_gpt_4_1_answer_text,
oci_xai_grok_4_answer_text,
oci_cohere_command_a_answer_text,
oci_meta_llama_4_scout_answer_text,
openai_gpt_4o_answer_text,
azure_openai_gpt_4o_answer_text,
):
"""
LLMの回答に引用情報を追加するためのラッパー関数
"""
async for result in append_citation_util(
search_result,
llm_answer_checkbox,
include_citation,
use_image,
query_text,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
oci_openai_gpt_5_answer_text,
oci_openai_o3_answer_text,
oci_openai_gpt_4_1_answer_text,
oci_xai_grok_4_answer_text,
oci_cohere_command_a_answer_text,
oci_meta_llama_4_scout_answer_text,
openai_gpt_4o_answer_text,
azure_openai_gpt_4o_answer_text,
):
yield result
async def process_single_image_streaming(image_url, query_text, llm_answer_checkbox_group, target_models, image_index,
doc_id, img_id, custom_image_prompt=None):
"""
単一画像を選択されたLLMモデルで処理し、ストリーミング形式で回答を返すためのラッパー関数
"""
async for result in process_single_image_streaming_util(
image_url, query_text, llm_answer_checkbox_group, target_models, image_index,
doc_id, img_id, custom_image_prompt
):
yield result
async def process_image_answers_streaming(
search_result,
use_image,
single_image_processing,
llm_answer_checkbox_group,
query_text,
oci_openai_gpt_5_image_answer_text,
oci_openai_o3_image_answer_text,
oci_openai_gpt_4_1_image_answer_text,
oci_meta_llama_4_scout_image_answer_text,
openai_gpt_4o_image_answer_text,
azure_openai_gpt_4o_image_answer_text,
image_limit_k=5,
custom_image_prompt=None,
):
"""
Vision 回答がオンの場合、検索結果から画像データを取得し、
選択されたVisionモデルで画像処理を行い、ストリーミング形式で回答を出力するためのラッパー関数
"""
async for result in process_image_answers_streaming_util(
pool,
DEFAULT_COLLECTION_NAME,
search_result,
use_image,
single_image_processing,
llm_answer_checkbox_group,
query_text,
oci_openai_gpt_5_image_answer_text,
oci_openai_o3_image_answer_text,
oci_openai_gpt_4_1_image_answer_text,
oci_meta_llama_4_scout_image_answer_text,
openai_gpt_4o_image_answer_text,
azure_openai_gpt_4o_image_answer_text,
image_limit_k,
custom_image_prompt
):
yield result
def set_query_id_state():
print("in set_query_id_state() start...")
return generate_unique_id("query_")
def eval_by_human(query_id, llm_name, human_evaluation_result, user_comment):
"""
人間評価のラッパー関数
"""
return eval_by_human_util(query_id, llm_name, human_evaluation_result, user_comment, pool)
def generate_eval_result_file():
print("in generate_eval_result_file() start...")
with pool.acquire() as conn:
with conn.cursor() as cursor:
select_sql = """
SELECT r.query_id,
r.query,
r.standard_answer,
r.sql,
f.llm_name,
f.llm_answer,
f.vlm_answer,
f.ragas_evaluation_result,
f.human_evaluation_result,
f.user_comment,
TO_CHAR(r.created_date, 'YYYY-MM-DD HH24:MI:SS') AS created_date
FROM RAG_QA_RESULT r
LEFT JOIN
RAG_QA_FEEDBACK f
ON
r.query_id = f.query_id \
"""
cursor.execute(select_sql)
# 列名を取得
columns = [col[0] for col in cursor.description]
# データを取得
data = cursor.fetchall()
print(f"{columns=}")
# データをDataFrameに変換
result_df = pd.DataFrame(data, columns=columns)
print(f"{result_df=}")
# 列名を日文に変更
result_df.rename(columns={
'QUERY_ID': 'クエリID',
'QUERY': 'クエリ',
'STANDARD_ANSWER': '標準回答',
'SQL': '使用されたSQL',
'LLM_NAME': 'LLM モデル',
'LLM_ANSWER': 'LLM メッセージ',
'VLM_ANSWER': 'Vision 回答',
'RAGAS_EVALUATION_RESULT': 'LLM 評価結果',
'HUMAN_EVALUATION_RESULT': 'Human 評価結果',
'USER_COMMENT': 'Human コメント',
'CREATED_DATE': '作成日時'
}, inplace=True)
print(f"{result_df=}")
# 必要に応じてcreated_date列をdatetime型に変換
result_df['作成日時'] = pd.to_datetime(result_df['作成日時'], format='%Y-%m-%d %H:%M:%S')
# Vision回答からbase64画像情報を削除
if 'Vision 回答' in result_df.columns:
result_df['Vision 回答'] = result_df['Vision 回答'].apply(
lambda x: remove_base64_images_from_text(x) if pd.notna(x) else x
)
# ファイルパスを定義
filepath = '/tmp/evaluation_result.xlsx'
# ExcelWriterを使用して複数のDataFrameを異なるシートに書き込み
with pd.ExcelWriter(filepath) as writer:
result_df.to_excel(writer, sheet_name='Sheet1', index=False)
print(f"Excelファイルが {filepath} に保存されました")
gr.Info("評価レポートの生成が完了しました")
return gr.DownloadButton(value=filepath, visible=True)
def insert_query_result(
search_result,
query_id,
query,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
sql,
llm_answer_checkbox_group,
llm_evaluation_checkbox,
standard_answer_text,
oci_openai_gpt_5_response,
oci_openai_o3_response,
oci_openai_gpt_4_1_response,
oci_xai_grok_4_response,
oci_cohere_command_a_response,
oci_meta_llama_4_scout_response,
openai_gpt_4o_response,
azure_openai_gpt_4o_response,
oci_openai_gpt_5_evaluation,
oci_openai_o3_evaluation,
oci_openai_gpt_4_1_evaluation,
oci_xai_grok_4_evaluation,
oci_cohere_command_a_evaluation,
oci_meta_llama_4_scout_evaluation,
openai_gpt_4o_evaluation,
azure_openai_gpt_4o_evaluation,
oci_openai_gpt_5_image_response,
oci_openai_o3_image_response,
oci_openai_gpt_4_1_image_response,
oci_meta_llama_4_scout_image_response,
openai_gpt_4o_image_response,
azure_openai_gpt_4o_image_response
):
"""
クエリ結果をデータベースに挿入するためのラッパー関数
"""
insert_query_result_util(
pool,
search_result,
query_id,
query,
doc_id_all_checkbox_input,
doc_id_checkbox_group_input,
sql,
llm_answer_checkbox_group,
llm_evaluation_checkbox,
standard_answer_text,
oci_openai_gpt_5_response,
oci_openai_o3_response,
oci_openai_gpt_4_1_response,
oci_xai_grok_4_response,
oci_cohere_command_a_response,
oci_meta_llama_4_scout_response,
openai_gpt_4o_response,
azure_openai_gpt_4o_response,
oci_openai_gpt_5_evaluation,
oci_openai_o3_evaluation,
oci_openai_gpt_4_1_evaluation,
oci_xai_grok_4_evaluation,
oci_cohere_command_a_evaluation,
oci_meta_llama_4_scout_evaluation,
openai_gpt_4o_evaluation,
azure_openai_gpt_4o_evaluation,
oci_openai_gpt_5_image_response,
oci_openai_o3_image_response,
oci_openai_gpt_4_1_image_response,
oci_meta_llama_4_scout_image_response,
openai_gpt_4o_image_response,
azure_openai_gpt_4o_image_response
)
def delete_document(server_directory, doc_ids):
"""
指定されたドキュメントを削除するためのラッパー関数
"""
return delete_document_util(pool, DEFAULT_COLLECTION_NAME, server_directory, doc_ids)
theme = gr.themes.Default(
spacing_size="sm",
font=[GoogleFont(name="Noto Sans JP"), GoogleFont(name="Noto Sans SC"), GoogleFont(name="Roboto")]
).set()
with gr.Blocks(css=custom_css, theme=theme) as app:
gr.Markdown(value="# RAG精度あげたろう", elem_classes="main_Header")
gr.Markdown(value="### LLM&RAG精度評価ツール",
elem_classes="sub_Header")
query_id_state = gr.State()
with gr.Tabs() as tabs:
with gr.TabItem(label="環境設定") as tab_setting:
with gr.TabItem(label="OCI GenAIの設定*") as tab_create_oci_cred:
with gr.Accordion(label="使用されたSQL", open=False) as tab_create_oci_cred_sql_accordion:
tab_create_oci_cred_sql_text = gr.Textbox(
label="SQL",
show_label=False,
lines=25,
max_lines=50,
autoscroll=False,
interactive=False,
show_copy_button=True
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_user_ocid_text = gr.Textbox(
label="User OCID*",
lines=1,
interactive=True
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_tenancy_ocid_text = gr.Textbox(
label="Tenancy OCID*",
lines=1,
interactive=True
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_fingerprint_text = gr.Textbox(
label="Fingerprint*",
lines=1,
interactive=True
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_private_key_file = gr.File(
label="Private Key*",
file_types=[".pem"],
type="filepath",
interactive=True
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_region_text = gr.Dropdown(
choices=["ap-osaka-1", "us-chicago-1"],
label="Region*",
interactive=True,
value="ap-osaka-1",
)
with gr.Row():
with gr.Column():
tab_create_oci_clear_button = gr.ClearButton(value="クリア")
with gr.Column():
tab_create_oci_cred_button = gr.Button(value="設定/再設定", variant="primary")
with gr.Accordion(label="OCI GenAIのテスト", open=False) as tab_create_oci_cred_test_accordion:
with gr.Row():
with gr.Column():
tab_create_oci_cred_test_query_text = gr.Textbox(
label="テキスト",
lines=1,
max_lines=1,
value="こんにちわ"
)
with gr.Row():
with gr.Column():
tab_create_oci_cred_test_vector_text = gr.Textbox(