1919
2020
2121def setup_inference_endpoint ():
22- inference_id = "e5-small-model"
23- try :
24- es_client .inference .put (
25- inference_id = inference_id ,
26- task_type = "text_embedding" ,
27- body = {
28- "service" : "elasticsearch" ,
29- "service_settings" : {
30- "num_allocations" : 1 ,
31- "num_threads" : 1 ,
32- "model_id" : ".multilingual-e5-small" ,
33- },
34- },
35- )
36- print (f"✅ Inference endpoint '{ inference_id } ' created successfully" )
37- except Exception as e :
38- print (f"❌ Error creating inference endpoint: { str (e )} " )
22+ """Create the e5-small-model inference endpoint for text embeddings if it doesn't exist."""
3923
40-
41- def setup_inference_endpoint ():
4224 inference_id = "e5-small-model"
4325
4426 try :
@@ -66,6 +48,8 @@ def setup_inference_endpoint():
6648
6749
6850def setup_index ():
51+ """Create the Elasticsearch index with semantic_text field mappings if it doesn't exist."""
52+
6953 try :
7054 if es_client .indices .exists (index = INDEX_NAME ):
7155 print (f"✅ Index '{ INDEX_NAME } ' already exists" )
@@ -92,11 +76,15 @@ def setup_index():
9276
9377
9478def load_documents (dataset_folder , index_name ):
79+ """Generator that yields documents from .txt files in the dataset folder for bulk indexing."""
80+
9581 for filename in os .listdir (dataset_folder ):
9682 if filename .endswith (".txt" ):
9783 filepath = os .path .join (dataset_folder , filename )
9884
99- with open (filepath , "r" , encoding = "utf-8" ) as file :
85+ with open (
86+ filepath , "r" , encoding = "utf-8"
87+ ) as file : # UTF-8 encoding ensures proper handling of special characters and international text
10088 content = file .read ()
10189
10290 yield {
@@ -106,9 +94,18 @@ def load_documents(dataset_folder, index_name):
10694
10795
10896def index_documents ():
97+ """Bulk index all documents from the dataset folder into Elasticsearch and return success count and latency."""
98+
10999 try :
110100 start_time = time .time ()
111101
102+ 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
108+
112109 success , _ = helpers .bulk (es_client , load_documents (DATASET_FOLDER , INDEX_NAME ))
113110
114111 end_time = time .time ()
@@ -121,6 +118,8 @@ def index_documents():
121118
122119
123120def semantic_search (query , size = 3 ):
121+ """Perform semantic search and return top results with latency."""
122+
124123 start_time = time .time ()
125124 search_body = {
126125 "query" : {"semantic" : {"field" : "semantic_field" , "query" : query }},
@@ -134,9 +133,12 @@ def semantic_search(query, size=3):
134133
135134
136135def query_local_ai (prompt , model ):
136+ """Send a prompt to Local AI model and return the response, latency, and tokens per second."""
137+
137138 start_time = time .time ()
138139
139140 try :
141+ # Using simple completions without streaming.
140142 response = ai_client .chat .completions .create (
141143 model = model ,
142144 messages = [{"role" : "user" , "content" : prompt }],
@@ -173,6 +175,9 @@ def query_local_ai(prompt, model):
173175
174176 time .sleep (2 ) # Wait for indexing to complete
175177
178+ if success == 0 : # if the index documents failed, or index does not exist, exit
179+ exit (1 )
180+
176181 query = "Can you summarize the performance issues in the API?"
177182
178183 print (f"🔍 Search: '{ query } '" )
0 commit comments