62
62
warnings .filterwarnings ("ignore" , category = UserWarning , module = "gradio_client.documentation" )
63
63
64
64
65
- load_dotenv ('ragtest /.env' )
65
+ load_dotenv ('indexing /.env' )
66
66
67
67
# Set default values for API-related environment variables
68
68
os .environ .setdefault ("LLM_API_BASE" , os .getenv ("LLM_API_BASE" ))
@@ -105,8 +105,8 @@ def initialize_models():
105
105
embeddings_api_base = os .getenv ("EMBEDDINGS_API_BASE" )
106
106
embeddings_api_key = os .getenv ("EMBEDDINGS_API_KEY" )
107
107
108
- llm_service_type = os .getenv ("LLM_SERVICE_TYPE" )
109
- embeddings_service_type = os .getenv ("EMBEDDINGS_SERVICE_TYPE" )
108
+ llm_service_type = os .getenv ("LLM_SERVICE_TYPE" , "openai_chat" ). lower () # Provide a default and lower it
109
+ embeddings_service_type = os .getenv ("EMBEDDINGS_SERVICE_TYPE" , "openai" ). lower () # Provide a default and lower it
110
110
111
111
llm_model = os .getenv ("LLM_MODEL" )
112
112
embeddings_model = os .getenv ("EMBEDDINGS_MODEL" )
@@ -119,7 +119,7 @@ def initialize_models():
119
119
embeddings_models = models
120
120
121
121
# Initialize LLM
122
- if llm_service_type . lower () == "openai_chat" :
122
+ if llm_service_type == "openai_chat" :
123
123
llm = ChatOpenAI (
124
124
api_key = llm_api_key ,
125
125
api_base = f"{ llm_api_base } /v1" ,
@@ -140,15 +140,15 @@ def initialize_models():
140
140
"model" : embeddings_model ,
141
141
"api_type" : "open_ai" ,
142
142
"api_base" : embeddings_api_base ,
143
- "api_key" : embeddings_api_key or None , # Change this line
144
- "provider" : embeddings_service_type . lower ()
143
+ "api_key" : embeddings_api_key or None ,
144
+ "provider" : embeddings_service_type
145
145
}
146
146
)
147
147
148
148
return llm_models , embeddings_models , llm_service_type , embeddings_service_type , llm_api_base , embeddings_api_base , text_embedder
149
149
150
150
def find_latest_output_folder ():
151
- root_dir = "./ragtest /output"
151
+ root_dir = "./indexing /output"
152
152
folders = [f for f in os .listdir (root_dir ) if os .path .isdir (os .path .join (root_dir , f ))]
153
153
154
154
if not folders :
@@ -251,7 +251,7 @@ def wait_for_api_server(port):
251
251
252
252
def load_settings ():
253
253
try :
254
- with open ("ragtest /settings.yaml" , "r" ) as f :
254
+ with open ("indexing /settings.yaml" , "r" ) as f :
255
255
return yaml .safe_load (f ) or {}
256
256
except FileNotFoundError :
257
257
return {}
@@ -264,7 +264,7 @@ def update_setting(key, value):
264
264
settings [key ] = value
265
265
266
266
try :
267
- with open ("ragtest /settings.yaml" , "w" ) as f :
267
+ with open ("indexing /settings.yaml" , "w" ) as f :
268
268
yaml .dump (settings , f , default_flow_style = False )
269
269
return f"Setting '{ key } ' updated successfully"
270
270
except Exception as e :
@@ -301,11 +301,14 @@ def get_openai_client():
301
301
llm_model = os .getenv ("LLM_MODEL" )
302
302
)
303
303
304
- def chat_with_openai (messages , model , temperature , max_tokens , api_base ):
304
+ async def chat_with_openai (messages , model , temperature , max_tokens , api_base ):
305
+ client = AsyncOpenAI (
306
+ base_url = api_base ,
307
+ api_key = os .getenv ("LLM_API_KEY" )
308
+ )
309
+
305
310
try :
306
- logging .info (f"Attempting to use model: { model } " )
307
- client = OpenAI (base_url = api_base , api_key = os .getenv ("LLM_API_KEY" ))
308
- response = client .chat .completions .create (
311
+ response = await client .chat .completions .create (
309
312
model = model ,
310
313
messages = messages ,
311
314
temperature = temperature ,
@@ -314,7 +317,7 @@ def chat_with_openai(messages, model, temperature, max_tokens, api_base):
314
317
return response .choices [0 ].message .content
315
318
except Exception as e :
316
319
logging .error (f"Error in chat_with_openai: { str (e )} " )
317
- logging . error ( f"Attempted with model : { model } , api_base: { api_base } " )
320
+ return f"An error occurred : { str ( e ) } "
318
321
return f"Error: { str (e )} "
319
322
320
323
def chat_with_llm (query , history , system_message , temperature , max_tokens , model , api_base ):
@@ -419,7 +422,7 @@ def construct_cli_args(query_type, preset, community_level, response_type, custo
419
422
if not selected_folder :
420
423
raise ValueError ("No folder selected. Please select an output folder before querying." )
421
424
422
- artifacts_folder = os .path .join ("./ragtest /output" , selected_folder , "artifacts" )
425
+ artifacts_folder = os .path .join ("./indexing /output" , selected_folder , "artifacts" )
423
426
if not os .path .exists (artifacts_folder ):
424
427
raise ValueError (f"Artifacts folder not found in { artifacts_folder } " )
425
428
@@ -464,7 +467,7 @@ def construct_cli_args(query_type, preset, community_level, response_type, custo
464
467
465
468
def upload_file (file ):
466
469
if file is not None :
467
- input_dir = os .path .join ("ragtest " , "input" )
470
+ input_dir = os .path .join ("indexing " , "input" )
468
471
os .makedirs (input_dir , exist_ok = True )
469
472
470
473
# Get the original filename from the uploaded file
@@ -487,7 +490,7 @@ def upload_file(file):
487
490
return status , gr .update (choices = updated_file_list ), update_logs ()
488
491
489
492
def list_input_files ():
490
- input_dir = os .path .join ("ragtest " , "input" )
493
+ input_dir = os .path .join ("indexing " , "input" )
491
494
files = []
492
495
if os .path .exists (input_dir ):
493
496
files = os .listdir (input_dir )
@@ -546,7 +549,7 @@ def save_file_content(file_path, content):
546
549
return status , update_logs ()
547
550
548
551
def manage_data ():
549
- db = lancedb .connect ("./ragtest /lancedb" )
552
+ db = lancedb .connect ("./indexing /lancedb" )
550
553
tables = db .table_names ()
551
554
table_info = ""
552
555
if tables :
@@ -581,7 +584,7 @@ def find_latest_graph_file(root_dir):
581
584
return latest_file
582
585
583
586
def update_visualization (folder_name , file_name , layout_type , node_size , edge_width , node_color_attribute , color_scheme , show_labels , label_size ):
584
- root_dir = "./ragtest "
587
+ root_dir = "./indexing "
585
588
if not folder_name or not file_name :
586
589
return None , "Please select a folder and a GraphML file."
587
590
file_name = file_name .split ("] " )[1 ] if "]" in file_name else file_name # Remove file type prefix
@@ -788,7 +791,7 @@ def update_llm_settings(llm_model, embeddings_model, context_window, system_mess
788
791
"provider" : embeddings_service_type
789
792
})
790
793
791
- with open ("ragtest /settings.yaml" , 'w' ) as f :
794
+ with open ("indexing /settings.yaml" , 'w' ) as f :
792
795
yaml .dump (settings , f , default_flow_style = False )
793
796
794
797
# Update .env file
@@ -813,7 +816,7 @@ def update_llm_settings(llm_model, embeddings_model, context_window, system_mess
813
816
return f"Error updating LLM and embeddings settings: { str (e )} "
814
817
815
818
def update_env_file (key , value ):
816
- env_path = 'ragtest /.env'
819
+ env_path = 'indexing /.env'
817
820
with open (env_path , 'r' ) as file :
818
821
lines = file .readlines ()
819
822
@@ -1115,19 +1118,19 @@ def list_folder_contents(folder_path):
1115
1118
return contents
1116
1119
1117
1120
def update_output_folder_list ():
1118
- root_dir = "./ragtest "
1121
+ root_dir = "./"
1119
1122
folders = list_output_folders (root_dir )
1120
1123
return gr .update (choices = folders , value = folders [0 ] if folders else None )
1121
1124
1122
1125
def update_folder_content_list (folder_name ):
1123
- root_dir = "./ragtest "
1126
+ root_dir = "./"
1124
1127
if not folder_name :
1125
1128
return gr .update (choices = [])
1126
1129
contents = list_folder_contents (os .path .join (root_dir , "output" , folder_name , "artifacts" ))
1127
1130
return gr .update (choices = contents )
1128
1131
1129
1132
def handle_content_selection (folder_name , selected_item ):
1130
- root_dir = "./ragtest "
1133
+ root_dir = "./"
1131
1134
if isinstance (selected_item , list ) and selected_item :
1132
1135
selected_item = selected_item [0 ] # Take the first item if it's a list
1133
1136
@@ -1147,7 +1150,7 @@ def handle_content_selection(folder_name, selected_item):
1147
1150
return gr .update (), "" , ""
1148
1151
1149
1152
def initialize_selected_folder (folder_name ):
1150
- root_dir = "./ragtest "
1153
+ root_dir = "./"
1151
1154
if not folder_name :
1152
1155
return "Please select a folder first." , gr .update (choices = [])
1153
1156
folder_path = os .path .join (root_dir , "output" , folder_name , "artifacts" )
@@ -1194,7 +1197,7 @@ def refresh_indexing():
1194
1197
1195
1198
1196
1199
def run_indexing (root_dir , config_file , verbose , nocache , resume , reporter , emit_formats , custom_args ):
1197
- cmd = ["python" , "-m" , "graphrag.index" , "--root" , "./ragtest " ]
1200
+ cmd = ["python" , "-m" , "graphrag.index" , "--root" , "./indexing " ]
1198
1201
1199
1202
# Add custom CLI arguments
1200
1203
if custom_args :
@@ -1271,6 +1274,7 @@ def run_indexing(root_dir, config_file, verbose, nocache, resume, reporter, emit
1271
1274
gr .update (interactive = False ),
1272
1275
gr .update (interactive = True ),
1273
1276
str (iterations_completed ))
1277
+
1274
1278
global_vector_store_wrapper = None
1275
1279
1276
1280
def create_gradio_interface ():
@@ -1308,7 +1312,7 @@ def create_gradio_interface():
1308
1312
1309
1313
1310
1314
with gr .TabItem ("Indexing" ):
1311
- root_dir = gr .Textbox (label = "Root Directory" , value = "./ragtest " )
1315
+ root_dir = gr .Textbox (label = "Root Directory" , value = "./" )
1312
1316
config_file = gr .File (label = "Config File (optional)" )
1313
1317
with gr .Row ():
1314
1318
verbose = gr .Checkbox (label = "Verbose" , value = True )
@@ -1370,7 +1374,7 @@ def create_gradio_interface():
1370
1374
)
1371
1375
1372
1376
with gr .TabItem ("Indexing Outputs/Visuals" ):
1373
- output_folder_list = gr .Dropdown (label = "Select Output Folder (Select GraphML File to Visualize)" , choices = list_output_folders ("./ragtest " ), interactive = True )
1377
+ output_folder_list = gr .Dropdown (label = "Select Output Folder (Select GraphML File to Visualize)" , choices = list_output_folders ("./indexing " ), interactive = True )
1374
1378
refresh_folder_btn = gr .Button ("Refresh Folder List" , variant = "secondary" )
1375
1379
initialize_folder_btn = gr .Button ("Initialize Selected Folder" , variant = "primary" )
1376
1380
folder_content_list = gr .Dropdown (label = "Select File or Directory" , choices = [], interactive = True )
@@ -1401,14 +1405,14 @@ def create_gradio_interface():
1401
1405
embeddings_service_type = gr .Radio (
1402
1406
label = "Embeddings Service Type" ,
1403
1407
choices = ["openai" , "ollama" ],
1404
- value = settings [ 'embeddings' ][ 'llm' ] .get ('type' , 'openai' ),
1408
+ value = settings . get ( 'embeddings' , {}). get ( 'llm' , {}) .get ('type' , 'openai' ),
1405
1409
visible = False ,
1406
1410
)
1407
1411
1408
1412
embeddings_model_dropdown = gr .Dropdown (
1409
1413
label = "Embeddings Model" ,
1410
1414
choices = [],
1411
- value = settings [ 'embeddings' ][ 'llm' ] .get ('model' ),
1415
+ value = settings . get ( 'embeddings' , {}). get ( 'llm' , {}) .get ('model' ),
1412
1416
allow_custom_value = True
1413
1417
)
1414
1418
refresh_embeddings_models_btn = gr .Button ("Refresh Embedding Models" , variant = "secondary" )
@@ -1550,7 +1554,7 @@ def create_gradio_interface():
1550
1554
)
1551
1555
selected_folder = gr .Dropdown (
1552
1556
label = "Select Index Folder to Chat With" ,
1553
- choices = list_output_folders ("./ragtest " ),
1557
+ choices = list_output_folders ("./indexing " ),
1554
1558
value = None ,
1555
1559
interactive = True
1556
1560
)
@@ -1626,7 +1630,7 @@ def update_custom_options(preset):
1626
1630
save_btn .click (fn = save_file_content , inputs = [file_list , file_content ], outputs = [operation_status , log_output ])
1627
1631
1628
1632
refresh_folder_btn .click (
1629
- fn = lambda : gr .update (choices = list_output_folders ("./ragtest " )),
1633
+ fn = lambda : gr .update (choices = list_output_folders ("./indexing " )),
1630
1634
outputs = [selected_folder ]
1631
1635
)
1632
1636
@@ -1755,10 +1759,11 @@ def update_custom_options(preset):
1755
1759
1756
1760
return demo .queue ()
1757
1761
1758
- def main ():
1762
+ async def main ():
1759
1763
api_port = 8088
1760
1764
gradio_port = 7860
1761
1765
1766
+
1762
1767
print (f"Starting API server on port { api_port } " )
1763
1768
start_api_server (api_port )
1764
1769
0 commit comments