resource_manager.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from pymilvus import connections, CollectionSchema, Collection
  2. from neo4j import AsyncGraphDatabase, AsyncDriver
  3. from applications.config import NEO4j_CONFIG
  4. from applications.utils.mysql import DatabaseManager
  5. from applications.utils.milvus.field import fields
  6. from applications.utils.elastic_search import AsyncElasticSearchClient
  7. class ResourceManager:
  8. def __init__(self, es_index, es_hosts, es_password, milvus_config):
  9. self.es_index = es_index
  10. self.es_hosts = es_hosts
  11. self.es_password = es_password
  12. self.milvus_config = milvus_config
  13. self.es_client: AsyncElasticSearchClient | None = None
  14. self.milvus_client: Collection | None = None
  15. self.mysql_client: DatabaseManager | None = None
  16. self.graph_client: AsyncDriver | None = None
  17. async def load_milvus(self):
  18. connections.connect("default", **self.milvus_config)
  19. schema = CollectionSchema(
  20. fields, description="Chunk multi-vector embeddings with metadata"
  21. )
  22. self.milvus_client = Collection(name="chunk_multi_embeddings_v2", schema=schema)
  23. # create index
  24. vector_index_params = {
  25. "index_type": "IVF_FLAT",
  26. "metric_type": "COSINE",
  27. "params": {"M": 16, "efConstruction": 200},
  28. }
  29. self.milvus_client.create_index("vector_text", vector_index_params)
  30. self.milvus_client.create_index("vector_summary", vector_index_params)
  31. self.milvus_client.create_index("vector_questions", vector_index_params)
  32. self.milvus_client.load()
  33. async def startup(self):
  34. # 初始化 Elasticsearch
  35. self.es_client = AsyncElasticSearchClient(
  36. index_name=self.es_index, hosts=self.es_hosts, password=self.es_password
  37. )
  38. if await self.es_client.es.ping():
  39. print("✅ Elasticsearch connected")
  40. else:
  41. print("❌ Elasticsearch connection failed")
  42. # 初始化 MySQL
  43. self.mysql_client = DatabaseManager()
  44. await self.mysql_client.init_pools()
  45. print("✅ MySQL connected")
  46. # 初始化 milvus
  47. await self.load_milvus()
  48. print("✅ Milvus loaded")
  49. uri: str = NEO4j_CONFIG["url"]
  50. auth: tuple = NEO4j_CONFIG["user"], NEO4j_CONFIG["password"]
  51. self.graph_client = AsyncGraphDatabase.driver(uri=uri, auth=auth)
  52. print("✅ NEO4j loaded")
  53. async def shutdown(self):
  54. # 关闭 Elasticsearch
  55. if self.es_client:
  56. await self.es_client.close()
  57. print("Elasticsearch closed")
  58. # 关闭 Milvus
  59. connections.disconnect("default")
  60. print("Milvus closed")
  61. # 关闭 MySQL
  62. if self.mysql_client:
  63. await self.mysql_client.close_pools()
  64. print("Mysql closed")
  65. await self.graph_client.close()
  66. print("Graph closed")
  67. _resource_manager: ResourceManager | None = None
  68. def init_resource_manager(es_index, es_hosts, es_password, milvus_config):
  69. global _resource_manager
  70. if _resource_manager is None:
  71. _resource_manager = ResourceManager(
  72. es_index, es_hosts, es_password, milvus_config
  73. )
  74. return _resource_manager
  75. def get_resource_manager() -> ResourceManager:
  76. return _resource_manager