1515
1616
1717es_client = Elasticsearch (ES_URL , api_key = ES_API_KEY )
18- ai_client = OpenAI (base_url = LOCAL_AI_URL , api_key = "sk-x" )
18+ ai_client = OpenAI (
19+ base_url = LOCAL_AI_URL , api_key = "sk-x"
20+ ) # You don't need a real OpenAI key for Local AI but we need to pass something, if you leave it blank it throws an error
1921
2022
2123def setup_inference_endpoint ():
@@ -53,6 +55,7 @@ def setup_index():
5355 try :
5456 if es_client .indices .exists (index = INDEX_NAME ):
5557 print (f"✅ Index '{ INDEX_NAME } ' already exists" )
58+ return False
5659
5760 print (f"📦 Creating index '{ INDEX_NAME } '..." )
5861 es_client .indices .create (
@@ -71,8 +74,10 @@ def setup_index():
7174 },
7275 )
7376 print (f"✅ Index '{ INDEX_NAME } ' created successfully" )
77+ return True
7478 except Exception as e :
7579 print (f"❌ Error creating index: { str (e )} " )
80+ exit (1 )
7681
7782
7883def load_documents (dataset_folder , index_name ):
@@ -97,24 +102,16 @@ def index_documents():
97102 """Bulk index all documents from the dataset folder into Elasticsearch and return success count and latency."""
98103
99104 try :
100- start_time = time .time ()
101-
102105 if es_client .indices .exists (index = INDEX_NAME ) is False :
103- print (
104- f"❌ Error: Index '{ INDEX_NAME } ' does not exist. Please set up the index first."
105- )
106-
107- return 0 , 0
106+ print (f"❌ Error: Index '{ INDEX_NAME } ' does not exist. " )
107+ exit (1 )
108108
109109 success , _ = helpers .bulk (es_client , load_documents (DATASET_FOLDER , INDEX_NAME ))
110110
111- end_time = time .time ()
112- bulk_latency = (end_time - start_time ) * 1000 # ms
113-
114- return success , bulk_latency
111+ return success
115112 except Exception as e :
116- print (f"❌ Error: { str (e )} " )
117- return 0 , 0
113+ print (f"❌ Error indexing documents : { str (e )} " )
114+ exit ( 1 )
118115
119116
120117def semantic_search (query , size = 3 ):
@@ -168,15 +165,17 @@ def query_local_ai(prompt, model):
168165
169166 # Setup inference endpoint and index
170167 setup_inference_endpoint ()
171- setup_index ()
168+ is_created = setup_index ()
172169
173- print ("\n 📥 Indexing documents..." )
174- success , bulk_latency = index_documents ()
170+ if is_created : # Index was just created, need to index documents
171+ print ("\n 📥 Indexing documents..." )
172+ success = index_documents ()
175173
176- time .sleep (2 ) # Wait for indexing to complete
174+ if success == 0 : # if indexing failed, exit
175+ print ("❌ Documents indexing failed. Exiting." )
176+ exit (1 )
177177
178- if success == 0 : # if the index documents failed, or index does not exist, exit
179- exit (1 )
178+ time .sleep (1 ) # Wait for indexing to complete
180179
181180 query = "Can you summarize the performance issues in the API?"
182181
@@ -212,6 +211,5 @@ def query_local_ai(prompt, model):
212211 for citation in citations :
213212 print (f" { citation } " )
214213
215- print (f"✅ Indexed { success } documents in { bulk_latency :.0f} ms" )
216- print (f"🔍 Search Latency: { search_latency :.0f} ms" )
214+ print (f"\n 🔍 Search Latency: { search_latency :.0f} ms" )
217215 print (f"🤖 AI Latency: { ai_latency :.0f} ms | { tokens_per_second :.1f} tokens/s" )
0 commit comments