OpenRAG 0.4.1.dev7__py3-none-any.whl → 0.4.1.dev9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
agent.py CHANGED
@@ -158,7 +158,7 @@ async def async_response_stream(
158
158
  else:
159
159
  delta_text = str(chunk.delta)
160
160
  full_response += delta_text
161
-
161
+
162
162
  # Enhanced logging for tool call detection (Granite 3.3 8b investigation)
163
163
  chunk_attrs = dir(chunk) if hasattr(chunk, '__dict__') else []
164
164
  tool_related_attrs = [attr for attr in chunk_attrs if 'tool' in attr.lower() or 'call' in attr.lower() or 'retrieval' in attr.lower()]
@@ -180,7 +180,7 @@ async def async_response_stream(
180
180
  chunk_data = chunk.__dict__
181
181
  else:
182
182
  chunk_data = str(chunk)
183
-
183
+
184
184
  # Log detailed chunk structure for investigation (especially for Granite 3.3 8b)
185
185
  if isinstance(chunk_data, dict):
186
186
  # Check for any fields that might indicate tool usage
@@ -218,7 +218,7 @@ async def async_response_stream(
218
218
  'retrieved_documents' in chunk_data,
219
219
  'retrieval_results' in chunk_data,
220
220
  ])
221
-
221
+
222
222
  if has_results:
223
223
  logger.info(
224
224
  "Detected implicit tool call in backend, injecting synthetic event",
@@ -242,7 +242,7 @@ async def async_response_stream(
242
242
  # Send the synthetic event first
243
243
  yield (json.dumps(synthetic_event, default=str) + "\n").encode("utf-8")
244
244
  detected_tool_call = True # Mark that we've injected a tool call
245
-
245
+
246
246
  yield (json.dumps(chunk_data, default=str) + "\n").encode("utf-8")
247
247
  except Exception as e:
248
248
  # Fallback to string representation
@@ -626,20 +626,68 @@ async def async_langflow_chat(
626
626
 
627
627
  # Extract sources from retrieval tool calls in the response
628
628
  sources = []
629
+
630
+ # Layer 1: Structured output items (OpenAI Responses API format).
631
+ # Relaxed: check for any output item with a non-empty `results` field,
632
+ # regardless of `type` string (Langflow may use different type names).
629
633
  if hasattr(response_obj, "output") and response_obj.output:
630
634
  for output_item in response_obj.output:
631
- item_type = getattr(output_item, "type", None)
632
- if item_type in ("tool_call", "retrieval_call"):
633
- for result in getattr(output_item, "results", None) or []:
634
- rd = result.model_dump() if hasattr(result, "model_dump") else (result if isinstance(result, dict) else {})
635
- if "text" in rd:
636
- sources.append({
637
- "filename": rd.get("filename", ""),
638
- "text": rd.get("text", ""),
639
- "score": rd.get("score", 0),
640
- "page": rd.get("page"),
641
- "mimetype": rd.get("mimetype"),
642
- })
635
+ for result in getattr(output_item, "results", None) or []:
636
+ rd = (
637
+ result.model_dump()
638
+ if hasattr(result, "model_dump")
639
+ else (result if isinstance(result, dict) else {})
640
+ )
641
+ if "text" in rd:
642
+ sources.append({
643
+ "filename": rd.get("filename", ""),
644
+ "text": rd.get("text", ""),
645
+ "score": rd.get("score", 0),
646
+ "page": rd.get("page"),
647
+ "mimetype": rd.get("mimetype"),
648
+ })
649
+
650
+ # Layer 2: Top-level dict inspection (mirrors streaming middleware in async_response_stream).
651
+ # Langflow may embed retrieval results directly in the response dict rather than
652
+ # inside typed output items.
653
+ if not sources:
654
+ resp_dict = (
655
+ response_obj.model_dump()
656
+ if hasattr(response_obj, "model_dump")
657
+ else getattr(response_obj, "__dict__", {})
658
+ )
659
+ implicit_results = (
660
+ resp_dict.get("results")
661
+ or resp_dict.get("outputs")
662
+ or resp_dict.get("retrieved_documents")
663
+ or resp_dict.get("retrieval_results")
664
+ or []
665
+ )
666
+ if isinstance(implicit_results, list):
667
+ for result in implicit_results:
668
+ if isinstance(result, dict) and "text" in result:
669
+ sources.append({
670
+ "filename": result.get("filename", ""),
671
+ "text": result.get("text", ""),
672
+ "score": result.get("score", 0),
673
+ "page": result.get("page"),
674
+ "mimetype": result.get("mimetype"),
675
+ })
676
+
677
+ # Layer 3: Citation-text fallback.
678
+ # Parse "(Source: filename)" patterns emitted by the LLM when it cites documents.
679
+ # This is the last-resort fallback when Langflow's response object carries no
680
+ # structured retrieval data.
681
+ if not sources:
682
+ import re
683
+ for match in re.finditer(r"\(Source:\s*([^\)]+)\)", response_text):
684
+ sources.append({
685
+ "filename": match.group(1).strip(),
686
+ "text": "",
687
+ "score": 0,
688
+ "page": None,
689
+ "mimetype": None,
690
+ })
643
691
 
644
692
  if not store_conversation:
645
693
  return response_text, response_id, sources
@@ -739,7 +787,7 @@ async def async_langflow_chat_stream(
739
787
  response_id = chunk_data["id"]
740
788
  elif "response_id" in chunk_data:
741
789
  response_id = chunk_data["response_id"]
742
-
790
+
743
791
  # Check for error status
744
792
  if chunk_data.get("finish_reason") == "error" or chunk_data.get("status") == "failed":
745
793
  error_occurred = True
@@ -788,7 +836,7 @@ async def async_langflow_chat_stream(
788
836
  # Log the error
789
837
  logger.error(f"Error in langflow chat stream: {e}", exc_info=True)
790
838
  error_occurred = True
791
-
839
+
792
840
  # Store error message in conversation history so it persists
793
841
  error_message = {
794
842
  "role": "assistant",
@@ -797,19 +845,19 @@ async def async_langflow_chat_stream(
797
845
  "error": True,
798
846
  }
799
847
  conversation_state["messages"].append(error_message)
800
-
848
+
801
849
  # Try to store the conversation with error message
802
850
  # Use a temporary response_id if we don't have one
803
851
  if not response_id:
804
852
  response_id = f"error_{user_id}_{int(datetime.now().timestamp())}"
805
-
853
+
806
854
  try:
807
855
  conversation_state["last_activity"] = datetime.now()
808
856
  await store_conversation_thread(user_id, response_id, conversation_state)
809
857
  logger.debug(f"Stored conversation with error for user {user_id}")
810
858
  except Exception as store_error:
811
859
  logger.error(f"Failed to store error conversation: {store_error}")
812
-
860
+
813
861
  # Re-raise the exception so it propagates to the API layer
814
862
  raise
815
863
 
api/settings.py CHANGED
@@ -1664,8 +1664,6 @@ async def rollback_onboarding(
1664
1664
  {"error": "No onboarding configuration to rollback"}, status_code=400
1665
1665
  )
1666
1666
 
1667
- jwt_token = user.jwt_token
1668
-
1669
1667
  logger.info("Rolling back onboarding configuration due to file failures")
1670
1668
 
1671
1669
  # Get all tasks for the user
@@ -1728,7 +1726,7 @@ async def rollback_onboarding(
1728
1726
  if filename:
1729
1727
  try:
1730
1728
  opensearch_client = session_manager.get_user_opensearch_client(
1731
- user.user_id, jwt_token
1729
+ user.user_id, user.jwt_token
1732
1730
  )
1733
1731
  from utils.opensearch_queries import build_filename_delete_body
1734
1732
  from config.settings import get_index_name
main.py CHANGED
@@ -248,10 +248,23 @@ async def init_index():
248
248
  )
249
249
  else:
250
250
  logger.info(
251
- "Index already exists, skipping creation",
251
+ "Index already exists, skipping creation and changing number of replicas",
252
252
  index_name=index_name,
253
253
  embedding_model=embedding_model,
254
254
  )
255
+ # Set number of replicas to 0 to not create unused nodes in OpenSearch, in case it was created with more replicas
256
+ current = await clients.opensearch.indices.get_settings(index=index_name)
257
+ current_replicas = int(
258
+ current[index_name]["settings"]["index"].get("number_of_replicas", 1)
259
+ )
260
+ if current_replicas != 0:
261
+ await clients.opensearch.indices.put_settings(
262
+ index=index_name,
263
+ body={"index": {"number_of_replicas": 0}},
264
+ )
265
+ logger.info(
266
+ "Updated documents index settings",
267
+ )
255
268
  await TelemetryClient.send_event(
256
269
  Category.OPENSEARCH_INDEX, MessageId.ORB_OS_INDEX_EXISTS
257
270
  )
@@ -259,6 +272,9 @@ async def init_index():
259
272
  # Create knowledge filters index
260
273
  knowledge_filter_index_name = "knowledge_filters"
261
274
  knowledge_filter_index_body = {
275
+ "settings": {
276
+ "index": {"number_of_replicas": 0, "number_of_shards": 1},
277
+ },
262
278
  "mappings": {
263
279
  "properties": {
264
280
  "id": {"type": "keyword"},
@@ -272,7 +288,7 @@ async def init_index():
272
288
  "created_at": {"type": "date"},
273
289
  "updated_at": {"type": "date"},
274
290
  }
275
- }
291
+ },
276
292
  }
277
293
 
278
294
  if not await clients.opensearch.indices.exists(
@@ -294,6 +310,19 @@ async def init_index():
294
310
  index_name=knowledge_filter_index_name,
295
311
  )
296
312
 
313
+ current = await clients.opensearch.indices.get_settings(index=knowledge_filter_index_name)
314
+ current_replicas = int(
315
+ current[knowledge_filter_index_name]["settings"]["index"].get("number_of_replicas", 1)
316
+ )
317
+ if current_replicas != 0:
318
+ await clients.opensearch.indices.put_settings(
319
+ index=knowledge_filter_index_name,
320
+ body={"index": {"number_of_replicas": 0}},
321
+ )
322
+ logger.info(
323
+ "Updated knowledge filters index settings",
324
+ )
325
+
297
326
  # Create API keys index for public API authentication
298
327
  if not await clients.opensearch.indices.exists(index=API_KEYS_INDEX_NAME):
299
328
  await clients.opensearch.indices.create(
@@ -1861,6 +1890,16 @@ async def create_app():
1861
1890
  await TelemetryClient.send_event(
1862
1891
  Category.APPLICATION_SHUTDOWN, MessageId.ORB_APP_SHUTDOWN
1863
1892
  )
1893
+ logger.info("Application shutdown initiated")
1894
+
1895
+ # Gracefully shutdown OpenSearch connection first
1896
+ try:
1897
+ from utils.opensearch_utils import graceful_opensearch_shutdown
1898
+ await graceful_opensearch_shutdown(clients.opensearch)
1899
+ clients.opensearch = None # prevent double-close in clients.cleanup()
1900
+ except Exception as e:
1901
+ logger.error("Error during graceful OpenSearch shutdown", error=str(e))
1902
+
1864
1903
  await cleanup_subscriptions_proper(services)
1865
1904
  # Cleanup task service (cancels background tasks and process pool)
1866
1905
  await services["task_service"].shutdown()
@@ -1870,6 +1909,7 @@ async def create_app():
1870
1909
  from utils.telemetry.client import cleanup_telemetry_client
1871
1910
 
1872
1911
  await cleanup_telemetry_client()
1912
+ logger.info("Application shutdown completed")
1873
1913
 
1874
1914
  return app
1875
1915
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: openrag
3
- Version: 0.4.1.dev7
3
+ Version: 0.4.1.dev9
4
4
  Summary: OpenRAG is a comprehensive Retrieval-Augmented Generation platform that enables intelligent document search and AI-powered conversations.
5
5
  Classifier: Development Status :: 4 - Beta
6
6
  Classifier: Environment :: Console
@@ -1,7 +1,7 @@
1
- agent.py,sha256=KOO8RRsXgAC1U_z293Q5fn5eig-o64oywfWDqqMfgkg,37073
1
+ agent.py,sha256=-qMzG7qKnJKGQgpndvyouNrO-PHLKE-DM8inX4K9UTA,38828
2
2
  auth_context.py,sha256=jonB-u3o-yj_s8TeV_FGFRminOZkwd6OcQm1G6WOHAk,2246
3
3
  dependencies.py,sha256=AjysWtYBXfqf6H_JZqvLKtZZlBuoTnMWBUfCzA9Rbr8,7107
4
- main.py,sha256=z_Tmnfdh0LWmSpAtr8xTIYrhF1HrmE7JjPrJZOcTI2k,69286
4
+ main.py,sha256=JOqzQ5o7eerqBfNvbN8oyiVrRfsTjg7WQDKk6e5OXZ0,71249
5
5
  session_manager.py,sha256=DOVwx7J2ZqlGLA4Jm1bdM42sDYi8lvzeW1kpRWUgQl4,10485
6
6
  api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  api/auth.py,sha256=nuwzZPW0b270ZS0UDS1Lfbqr7H3VCCgy0d9LpCAGg7g,3625
@@ -21,7 +21,7 @@ api/provider_health.py,sha256=u4gNGhEAqsQY1ml6xWzWWAOfLHDrSVYPkw1NAmBCMsI,9690
21
21
  api/provider_validation.py,sha256=54w-2qQQS9MHrgsLghpsDkCs5EQxFoUZrp_31GPK7JM,31421
22
22
  api/router.py,sha256=nOusNdc-eOMRZo6a5eqNb91oOoleYNomHTGcKuPsMmU,6229
23
23
  api/search.py,sha256=PFL2-ReVCCzJmT31IDZlFhQm3eXSPgb9LS4mnnsc_58,2010
24
- api/settings.py,sha256=E6xIWQTi5jc7m212c3-JA5sy8mjPcw8pRw9Pu7VbtnA,83691
24
+ api/settings.py,sha256=EFfjxO-aofcOFVzG7WlYvR7O4cTCNEm7gBDGe4KidXo,83656
25
25
  api/tasks.py,sha256=j3tAfpE_X4UxhiGPrgea5jkxILF6HZK9Hoy3QTFGG0g,1524
26
26
  api/upload.py,sha256=gl47Ue_d6q-YgI019lspnmPQ22PMmFch7Z9E0tsgb6U,6649
27
27
  api/v1/__init__.py,sha256=A7dLY63RcJv6ii-Xh7WgREcwXixjzOXz2w5C0LGWTCc,190
@@ -66,7 +66,7 @@ models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
66
66
  models/processors.py,sha256=a-f1joalfx21YhWmJS4vIRmKoLVBj1a78r5Q08nJb9k,32591
67
67
  models/tasks.py,sha256=9iINXQZxA9-hcYzG8xC_baBfLa2w6gcRPJdQHsx_Kms,1643
68
68
  models/url.py,sha256=92rsrLLyIqUErYipYz5QwUNTdHwJ2jBRV7PIVpncTyo,5764
69
- openrag-0.4.1.dev7.dist-info/licenses/LICENSE,sha256=jWcnPXkUxEinX60Yn-_zjsY9i84vz2QSb0fi53djOkg,11334
69
+ openrag-0.4.1.dev9.dist-info/licenses/LICENSE,sha256=jWcnPXkUxEinX60Yn-_zjsY9i84vz2QSb0fi53djOkg,11334
70
70
  services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
71
71
  services/api_key_service.py,sha256=B3yNdBBtvLjx3W3r1_CVrLktihZ4vdR_Xx8T2CbaALM,11541
72
72
  services/auth_service.py,sha256=7DTB92R_H8x4BL0x6ngvZHt5-WF4PXd2PWsWaRShqMw,22346
@@ -82,13 +82,13 @@ services/models_service.py,sha256=7eOE0yJ45ejUpCIyAnHQVZ975uwTswG-BOHQNQrCC6g,17
82
82
  services/monitor_service.py,sha256=CfZqs6SVhVEnqSOGT2sGhfBUT_21GyV2nLrG_i34sVQ,12493
83
83
  services/search_service.py,sha256=x7w8YmSBEzoPVo94xynujxj5TWQQmSuBiuoBvzHI0R0,25392
84
84
  services/session_ownership_service.py,sha256=ZO_zQ0wUldKKs58nrxXAYRyWIkxPuly4BVDM1ZZjmmk,4496
85
- services/task_service.py,sha256=rNJkHBPiV9laU06MS_jV0MDyj2Ye7paCl8t0Ugm6_1g,32657
85
+ services/task_service.py,sha256=jkzsEIfQKBed4SbmmEd4ZEhOKWbmjYK5QSGja0nK8Ok,33308
86
86
  tui/__init__.py,sha256=RICZ8Cht-_36b6KVVCkwdNFQIOG40GnDe2NZJlkphd4,138
87
87
  tui/cli.py,sha256=utCk0OtchNNHG1CYTTAOeuIp17sHuB1NIn9-GuInZdc,17982
88
88
  tui/config_fields.py,sha256=eQCVDfRQbOnTI5c6PzOZLFNYkDvb6jOL3OXGPGyPi3U,12411
89
89
  tui/main.py,sha256=CTAMzHvU_vQ5W9Qai3fUrUO82uVHInAWqAE1azOC5hc,28237
90
90
  tui/_assets/docker-compose.gpu.yml,sha256=tV8pkI3hTEk2yvpIS6m5ONavEQdmwhuqkGW8RIhQvhk,290
91
- tui/_assets/docker-compose.yml,sha256=-KFsYBNglIkJz1nI5j4HhABawO35yhiowRUHwTRnT8g,8007
91
+ tui/_assets/docker-compose.yml,sha256=V1IPmy8XobB4Sm4MEuQioGrwQ-gw3GwserTDbspd1do,7809
92
92
  tui/_assets/flows/ingestion_flow.json,sha256=mklpOLzC4TgDmK9K8W47BG8YQyazzNa6Mvkf91UC31s,495465
93
93
  tui/_assets/flows/openrag_agent.json,sha256=J9B46HAS21pRNbatgDpDtvZjhbm-OJ4FNcc5GI_7ex4,424762
94
94
  tui/_assets/flows/openrag_nudges.json,sha256=8kDz-UAfVNo0iqKCTH64Oq0v8h0bwKZsXI6Btx1nDuE,374114
@@ -135,7 +135,7 @@ utils/container_utils.py,sha256=PgISyGVEdLBZLig4lTbfG077kSkVBfu42Bg9vRhikoI,1133
135
135
  utils/docling_client.py,sha256=HesCaEdGLWzYHN61K_BPNOH--oW1jFGfpFEKqbgmzy8,2727
136
136
  utils/document_processing.py,sha256=oAVji0GlY3igDPR2wosmkLQ-rXnWI7rQLUo6d_bpuP4,4366
137
137
  utils/embedding_fields.py,sha256=injO4R8sklSnNQaHejdptFQ73yABvmtwNzJTYxSUyfg,6357
138
- utils/embeddings.py,sha256=Y6tBEQG7hJ35fhrh_ew_QLaE7gBEOJiGNzIvVEvZaYY,7479
138
+ utils/embeddings.py,sha256=ew1aMVVDaMVjNNcNadNJluXATfpQ0Y96kWYGmPsGavE,7478
139
139
  utils/env_utils.py,sha256=slkoqbTStWn0xJQJ9Ws0qtv6bUH_bC-rBJbWsgDw5pk,890
140
140
  utils/file_utils.py,sha256=n0Tp8OiBQ7QygRljl6-b8dNgqtWg1P_SB0GwOnra-y8,4345
141
141
  utils/gpu_detection.py,sha256=yU8PtO9Fp0rfHhY6JW0212cS_SVMA12BtWUXHXpQyp0,1697
@@ -144,15 +144,15 @@ utils/langflow_headers.py,sha256=GpmOiK5AS01QCDd1Pz7fxmjdTCzWtg2js_OTUQbGlak,329
144
144
  utils/langflow_utils.py,sha256=-qxpK514H0IVMLZOkyFkxm20ZQW6y-Ds_73naCoQBFQ,2942
145
145
  utils/logging_config.py,sha256=c4ejF_kA9OkUYXd2I2u7Y1I02cDqFdKylkmco8kHGmA,5270
146
146
  utils/opensearch_queries.py,sha256=rfEZL-fZiPE22oi0cON1XBIkrtSUvVV70OYuml4ewt4,1408
147
- utils/opensearch_utils.py,sha256=stllpbE0Jt7UN1sVk0CrTeKDYRyNBnEiPCke7IOrqKI,4045
147
+ utils/opensearch_utils.py,sha256=5RX01XoZvUm9HSW7H3FDMaDUHoreC86HWxncZ-2kdtM,5378
148
148
  utils/paths.py,sha256=SdNmEy15oeq2v5sgGChPLZ_MQpqlyAtpJNrrI_N8D3w,2263
149
149
  utils/version_utils.py,sha256=6Webhqo1TQV8B2vzWUlMb_nI28i1Hd5FqE-MGijYx_I,1488
150
150
  utils/telemetry/__init__.py,sha256=xoStY8hoYWnVe0B_jNthSdzRb7AxCT0rhM8ba9Q0Obg,203
151
151
  utils/telemetry/category.py,sha256=wei3V5WyUaiadYjEE5s0ys9PY0TjnebIvTykQU0P_2k,1114
152
152
  utils/telemetry/client.py,sha256=RhjW1a27w7W6gHt9X7AxdRPdZGmCEQDPskkxj5nsuiQ,12500
153
153
  utils/telemetry/message_id.py,sha256=IAu0wBVNOGPcSlQ0bAzvqIYr1Ac_n8wSTSxXzq_tiV0,9634
154
- openrag-0.4.1.dev7.dist-info/METADATA,sha256=zwtit61N_K6sXuErGpUTkbcoD5CsgxlLBPkiEMjtiB0,7514
155
- openrag-0.4.1.dev7.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
156
- openrag-0.4.1.dev7.dist-info/entry_points.txt,sha256=ArKNHB63I3K8vynNkogvZOLmmQFpIsHDL7MvreCmju4,45
157
- openrag-0.4.1.dev7.dist-info/top_level.txt,sha256=uOXDYvPnDHWjAKsqgz_7wX5PGLxkDN6k3g8dNZ7Nuv0,101
158
- openrag-0.4.1.dev7.dist-info/RECORD,,
154
+ openrag-0.4.1.dev9.dist-info/METADATA,sha256=COOqKVqhmyK8bJjvWzmZkyDgV2n6n4WfDH6CAgze0J0,7514
155
+ openrag-0.4.1.dev9.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
156
+ openrag-0.4.1.dev9.dist-info/entry_points.txt,sha256=ArKNHB63I3K8vynNkogvZOLmmQFpIsHDL7MvreCmju4,45
157
+ openrag-0.4.1.dev9.dist-info/top_level.txt,sha256=uOXDYvPnDHWjAKsqgz_7wX5PGLxkDN6k3g8dNZ7Nuv0,101
158
+ openrag-0.4.1.dev9.dist-info/RECORD,,
services/task_service.py CHANGED
@@ -411,6 +411,16 @@ class TaskService:
411
411
 
412
412
  # Mark task as completed if all files (including appended ones) are done
413
413
  if upload_task.processed_files >= upload_task.total_files:
414
+ # Force an index refresh BEFORE marking the task COMPLETED so that
415
+ # callers polling for COMPLETED status can immediately query or delete
416
+ # the newly indexed chunks without hitting the near-real-time refresh window.
417
+ if upload_task.successful_files > 0:
418
+ try:
419
+ from config.settings import clients, get_index_name
420
+ await clients.opensearch.indices.refresh(index=get_index_name())
421
+ except Exception as e:
422
+ logger.debug("Index refresh after ingest failed (non-fatal)", error=str(e))
423
+
414
424
  upload_task.status = TaskStatus.COMPLETED
415
425
  upload_task.updated_at = time.time()
416
426
 
@@ -10,26 +10,20 @@ services:
10
10
  environment:
11
11
  - discovery.type=single-node
12
12
  - OPENSEARCH_INITIAL_ADMIN_PASSWORD=${OPENSEARCH_PASSWORD}
13
- # Run security setup in background after OpenSearch starts
14
- command: >
15
- bash -c "
16
- # Ensure data directory has correct permissions
17
- sudo chown -R opensearch:opensearch /usr/share/opensearch/data || true
18
-
19
- # Start OpenSearch in background
20
- /usr/share/opensearch/opensearch-docker-entrypoint.sh opensearch &
21
-
22
- # Wait a bit for OpenSearch to start, then apply security config
23
- sleep 10 && /usr/share/opensearch/setup-security.sh &
24
-
25
- # Wait for background processes
26
- wait
27
- "
13
+ - OPENSEARCH_JAVA_OPTS=-Xms1g -Xmx1g
28
14
  ports:
29
15
  - "9200:9200"
30
16
  - "9600:9600"
31
17
  volumes:
32
- - ${OPENSEARCH_DATA_PATH:-./opensearch-data}:/usr/share/opensearch/data:U,z
18
+ - opensearch-data:/usr/share/opensearch/data
19
+ stop_grace_period: 2m
20
+ healthcheck:
21
+ test: ["CMD-SHELL", "curl -ku admin:$$OPENSEARCH_PASSWORD 'https://localhost:9200/_cluster/health' -s | grep -qE '\"status\":\"(green|yellow)\"'"]
22
+ interval: 15s
23
+ timeout: 10s
24
+ retries: 20
25
+ start_period: 60s
26
+ restart: unless-stopped
33
27
 
34
28
  dashboards:
35
29
  image: opensearchproject/opensearch-dashboards:3.0.0
@@ -186,3 +180,6 @@ services:
186
180
  - LANGFLOW_ENABLE_SUPERUSER_CLI=${LANGFLOW_ENABLE_SUPERUSER_CLI}
187
181
  # - DEFAULT_FOLDER_NAME=OpenRAG
188
182
  - HIDE_GETTING_STARTED_PROGRESS=true
183
+
184
+ volumes:
185
+ opensearch-data:
utils/embeddings.py CHANGED
@@ -158,7 +158,7 @@ async def create_dynamic_index_body(
158
158
  "settings": {
159
159
  "index": {"knn": True},
160
160
  "number_of_shards": 1,
161
- "number_of_replicas": 1,
161
+ "number_of_replicas": 0
162
162
  },
163
163
  "mappings": {
164
164
  "properties": {
utils/opensearch_utils.py CHANGED
@@ -121,3 +121,40 @@ async def wait_for_opensearch(
121
121
  message: str = "Failed to verify whether OpenSearch is ready."
122
122
  logger.error(message)
123
123
  raise OpenSearchNotReadyError(message)
124
+
125
+
126
+ async def graceful_opensearch_shutdown(opensearch_client: AsyncOpenSearch) -> None:
127
+ """Gracefully shutdown OpenSearch client connection.
128
+
129
+ This ensures that all pending operations are completed and connections
130
+ are properly closed before the application exits.
131
+
132
+ Args:
133
+ opensearch_client: The OpenSearch client to shutdown.
134
+ """
135
+ if opensearch_client is None:
136
+ logger.debug("OpenSearch client is None, skipping graceful shutdown")
137
+ return
138
+
139
+ try:
140
+ logger.info("Initiating graceful OpenSearch shutdown...")
141
+
142
+ # Flush any pending write operations before closing
143
+ try:
144
+ await asyncio.wait_for(
145
+ opensearch_client.indices.flush(index="_all", wait_if_ongoing=True),
146
+ timeout=10.0
147
+ )
148
+ logger.debug("Index flush completed")
149
+ except asyncio.TimeoutError:
150
+ logger.warning("Timeout during index flush")
151
+ except Exception as e:
152
+ logger.warning("Error during index flush", error=str(e))
153
+
154
+ # Close the client connection
155
+ await opensearch_client.close()
156
+ logger.info("OpenSearch client connection closed gracefully")
157
+
158
+ except Exception as e:
159
+ logger.error("Error during graceful OpenSearch shutdown", error=str(e))
160
+