imbajin commented on code in PR #304:
URL: 
https://github.com/apache/incubator-hugegraph-ai/pull/304#discussion_r2465619996


##########
hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py:
##########
@@ -106,6 +106,83 @@ def test_api_connection(
     return resp.status_code
 
 
+def apply_vector_engine(engine: str):
+    # Persist the vector engine selection
+    setattr(index_settings, "cur_vector_index", engine)
+    try:
+        index_settings.update_env()
+    except Exception:  # pylint: disable=W0718
+        pass
+    gr.Info("Configured!")
+
+
+def apply_vector_engine_backend(  # pylint: disable=too-many-branches
+    engine: str,
+    host: Optional[str] = None,
+    port: Optional[str] = None,
+    user: Optional[str] = None,
+    password: Optional[str] = None,
+    api_key: Optional[str] = None,
+    origin_call=None,
+) -> int:
+    """Test connection and persist per-engine connection settings"""
+    status_code = -1
+
+    # Test connection first
+    try:
+        if engine == "Milvus":
+            from pymilvus import connections, utility
+
+            connections.connect(
+                host=host, port=int(port or 19530), user=user or "", 
password=password or ""
+            )
+            # Test if we can list collections
+            _ = utility.list_collections()
+            connections.disconnect("default")
+            status_code = 200
+        elif engine == "Qdrant":
+            from qdrant_client import QdrantClient
+
+            client = QdrantClient(host=host, port=int(port or 6333), 
api_key=api_key)
+            # Test if we can get collections
+            _ = client.get_collections()
+            status_code = 200
+    except ImportError as e:
+        msg = f"Missing dependency: {e}. Please install with: uv sync --extra 
vectordb"
+        if origin_call is None:
+            raise gr.Error(msg) from e
+        return -1
+    except Exception as e:  # pylint: disable=broad-exception-caught
+        msg = f"Connection failed: {e}"
+        log.error(msg)
+        if origin_call is None:
+            raise gr.Error(msg) from e
+        return -1
+
+    # Persist settings after successful test
+    if engine == "Milvus":
+        if host is not None:
+            index_settings.milvus_host = host
+        if port is not None and str(port).strip():
+            index_settings.milvus_port = int(port)  # type: ignore[arg-type]
+        index_settings.milvus_user = user or ""
+        index_settings.milvus_password = password or ""
+    elif engine == "Qdrant":
+        if host is not None:
+            index_settings.qdrant_host = host
+        if port is not None and str(port).strip():
+            index_settings.qdrant_port = int(port)  # type: ignore[arg-type]
+        # Empty string treated as None for api key
+        index_settings.qdrant_api_key = api_key or None
+

Review Comment:
   ⚠️ **代码重复**: 这里的错误处理逻辑与 `apply_vector_engine` 函数完全相同,建议提取为公共函数。
   
   ```python
   def persist_settings_safely(settings):\n    try:\n        
settings.update_env()\n    except Exception as e:\n        log.warning("Failed 
to persist settings: %s", e)\n\nindex_settings.cur_vector_index = 
engine\npersist_settings_safely(index_settings)\n```



##########
hugegraph-llm/src/hugegraph_llm/demo/rag_demo/configs_block.py:
##########
@@ -106,6 +106,83 @@ def test_api_connection(
     return resp.status_code
 
 
+def apply_vector_engine(engine: str):
+    # Persist the vector engine selection
+    setattr(index_settings, "cur_vector_index", engine)
+    try:
+        index_settings.update_env()
+    except Exception:  # pylint: disable=W0718
+        pass
+    gr.Info("Configured!")
+
+
+def apply_vector_engine_backend(  # pylint: disable=too-many-branches
+    engine: str,
+    host: Optional[str] = None,
+    port: Optional[str] = None,
+    user: Optional[str] = None,
+    password: Optional[str] = None,
+    api_key: Optional[str] = None,
+    origin_call=None,
+) -> int:
+    """Test connection and persist per-engine connection settings"""
+    status_code = -1
+
+    # Test connection first
+    try:
+        if engine == "Milvus":
+            from pymilvus import connections, utility
+
+            connections.connect(
+                host=host, port=int(port or 19530), user=user or "", 
password=password or ""
+            )
+            # Test if we can list collections
+            _ = utility.list_collections()
+            connections.disconnect("default")
+            status_code = 200
+        elif engine == "Qdrant":
+            from qdrant_client import QdrantClient
+

Review Comment:
   ⚠️ **资源管理问题**: Qdrant 客户端连接后没有显式关闭,虽然 Python 有 GC,但最好显式管理资源。
   
   建议:
   ```python
   elif engine == "Qdrant":
       from qdrant_client import QdrantClient
       
       client = QdrantClient(host=host, port=int(port or 6333), api_key=api_key)
       try:
           _ = client.get_collections()
           status_code = 200
       finally:
           client.close()  # 如果 API 支持的话
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to