修复因Milvus数据库初始化失败而导致整个服务无法启动的问题

This commit is contained in:
BBIT-Kai
2026-04-27 17:04:47 +08:00
parent ca6cdb8499
commit 1c107473ae
2 changed files with 47 additions and 34 deletions
+41 -28
View File
@@ -5,31 +5,44 @@ from utils.GlobalVariable import LOCAL_IP
URI = "http://" + LOCAL_IP + ":19530" URI = "http://" + LOCAL_IP + ":19530"
knVectorstore = Milvus( _knVectorstore = None
embedding_function=llmEmbeddings, _memVectorstore = None
connection_args={"uri": URI, "token": "root:Milvus", "db_name": "bbit_ai_lab"},
collection_name="knowledge",
index_params={"index_type": "FLAT", "metric_type": "L2"}, def get_kn_vectorstore():
consistency_level="Strong", global _knVectorstore
auto_id=True, if _knVectorstore is None:
primary_field="id", try:
text_field="text", _knVectorstore = Milvus(
vector_field="vector", embedding_function=llmEmbeddings,
partition_key_field="kn_id", connection_args={
enable_dynamic_field=True, "uri": URI,
drop_old=False, # set to True if seeking to drop the collection with that name if it exists "token": "root:Milvus",
) "db_name": "bbit_ai_lab",
memVectorstore = Milvus( },
embedding_function=llmEmbeddings, collection_name="knowledge",
connection_args={"uri": URI, "token": "root:Milvus", "db_name": "bbit_ai_lab"}, index_params={"index_type": "FLAT", "metric_type": "L2"},
collection_name="memory", consistency_level="Strong",
index_params={"index_type": "FLAT", "metric_type": "L2"}, auto_id=True,
consistency_level="Strong", primary_field="id",
auto_id=True, text_field="text",
primary_field="id", vector_field="vector",
text_field="text", partition_key_field="kn_id",
vector_field="vector", enable_dynamic_field=True,
partition_key_field="ai_id", drop_old=False,
enable_dynamic_field=True, )
drop_old=False, # set to True if seeking to drop the collection with that name if it exists except Exception as e:
) print(f"[Milvus Init Failed] knowledge: {e}")
_knVectorstore = None
return _knVectorstore
def get_mem_vectorstore():
global _memVectorstore
if _memVectorstore is None:
try:
_memVectorstore = Milvus(...)
except Exception as e:
print(f"[Milvus Init Failed] memory: {e}")
_memVectorstore = None
return _memVectorstore
+6 -6
View File
@@ -3,7 +3,7 @@ from typing import List
from langchain_core.documents import Document from langchain_core.documents import Document
from config.milvus import knVectorstore, memVectorstore from config.milvus import get_kn_vectorstore, get_mem_vectorstore
def get_knowledge_by_key_words(key_words: str, kn_ids: List[str]) -> str: def get_knowledge_by_key_words(key_words: str, kn_ids: List[str]) -> str:
@@ -17,7 +17,7 @@ def get_knowledge_by_key_words(key_words: str, kn_ids: List[str]) -> str:
else: else:
return "未找到相关的知识。" return "未找到相关的知识。"
result = knVectorstore.similarity_search( result = get_kn_vectorstore().similarity_search(
query=key_words, k=3, expr=expr # 可调节返回条数 query=key_words, k=3, expr=expr # 可调节返回条数
) )
@@ -46,7 +46,7 @@ def get_memory_by_key_words(key_words: str, ai_ids: List[str]) -> str:
else: else:
expr = "" # 不限制 kn_id todo 实际上应该不反悔任何内容 expr = "" # 不限制 kn_id todo 实际上应该不反悔任何内容
result = memVectorstore.similarity_search( result = get_mem_vectorstore().similarity_search(
query=key_words, k=5, expr=expr # 可调节返回条数 query=key_words, k=5, expr=expr # 可调节返回条数
) )
@@ -65,7 +65,7 @@ def get_memory_by_key_words(key_words: str, ai_ids: List[str]) -> str:
def get_knowledge_by_base_id(base_id: str): def get_knowledge_by_base_id(base_id: str):
expr = f'kn_id == "{base_id}"' # base_id 会被替换 expr = f'kn_id == "{base_id}"' # base_id 会被替换
result = knVectorstore.similarity_search( result = get_kn_vectorstore().similarity_search(
query="", k=100, expr=expr # 如果只想用过滤条件,可以传空字符串 query="", k=100, expr=expr # 如果只想用过滤条件,可以传空字符串
) )
return [ return [
@@ -90,7 +90,7 @@ def add_knowledge(text: str, is_active: bool, base_id: str, user_id: str):
}, },
) )
] ]
return knVectorstore.add_documents(docs) return get_kn_vectorstore().add_documents(docs)
def add_memory(ai_id: str, mem: str, user_id: str, is_active: bool): def add_memory(ai_id: str, mem: str, user_id: str, is_active: bool):
@@ -105,4 +105,4 @@ def add_memory(ai_id: str, mem: str, user_id: str, is_active: bool):
}, },
) )
] ]
return memVectorstore.add_documents(docs) return get_mem_vectorstore().add_documents(docs)