This commit is contained in:
2025-09-07 08:04:43 +08:00
parent adb4416ca2
commit ee76c69ac8
2 changed files with 32 additions and 16 deletions

View File

@@ -1,7 +1,16 @@
import json
import logging
from VikingDBMemoryService import VikingDBMemoryService, MEMORY_COLLECTION_NAME
from Config.Config import VOLC_ACCESSKEY, VOLC_SECRETKEY
# 控制日志输出
logger = logging.getLogger('CollectionMemory')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
def drop_existing_collection(collection_name):
# 初始化记忆库服务
memory_service = VikingDBMemoryService(
@@ -13,23 +22,23 @@ def drop_existing_collection(collection_name):
try:
# 检查集合是否存在
print(f"正在检查集合 '{collection_name}'...")
logger.info(f"正在检查集合 '{collection_name}'...")
memory_service.get_collection(collection_name)
print(f"集合 '{collection_name}' 已存在,准备删除...")
logger.info(f"集合 '{collection_name}' 已存在,准备删除...")
# 删除集合
response = memory_service.drop_collection(collection_name)
print(f"删除响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
print(f"集合 '{collection_name}' 删除成功")
logger.info(f"删除响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
logger.info(f"集合 '{collection_name}' 删除成功")
return True
except Exception as e:
error_msg = str(e)
if "collection not exist" in error_msg:
print(f"集合 '{collection_name}' 不存在,无需删除")
logger.info(f"集合 '{collection_name}' 不存在,无需删除")
return False
else:
print(f"操作失败: {error_msg}")
logger.error(f"操作失败: {error_msg}")
raise
if __name__ == "__main__":

View File

@@ -1,8 +1,15 @@
import json
import logging
from Config.Config import VOLC_ACCESSKEY, VOLC_SECRETKEY
from VikingDBMemoryService import VikingDBMemoryService, MEMORY_COLLECTION_NAME
from Volcengine.chat import wait_for_collection_ready
# 控制日志输出
logger = logging.getLogger('CollectionMemory')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
def create_memory_collection(collection_name, description="情感陪伴记忆库"):
@@ -16,18 +23,18 @@ def create_memory_collection(collection_name, description="情感陪伴记忆库
try:
# 检查集合是否已存在
print(f"正在检查集合 '{collection_name}'...")
logger.info(f"正在检查集合 '{collection_name}'...")
memory_service.get_collection(collection_name)
print(f"集合 '{collection_name}' 已存在,无需重复创建")
logger.info(f"集合 '{collection_name}' 已存在,无需重复创建")
return False
except Exception as e:
if "collection not exist" not in str(e):
print(f"检查集合时发生错误: {str(e)}")
logger.info(f"检查集合时发生错误: {str(e)}")
raise
# 创建新集合
print(f"开始创建集合 '{collection_name}'...")
logger.info(f"开始创建集合 '{collection_name}'...")
try:
response = memory_service.create_collection(
collection_name=collection_name,
@@ -35,20 +42,20 @@ def create_memory_collection(collection_name, description="情感陪伴记忆库
builtin_event_types=["sys_event_v1", "sys_profile_collect_v1"],
builtin_entity_types=["sys_profile_v1"]
)
print(f"创建响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
print(f"集合 '{collection_name}' 创建成功")
logger.info(f"创建响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
logger.info(f"集合 '{collection_name}' 创建成功")
# 等待集合就绪
print("等待集合初始化完成...")
logger.info("等待集合初始化完成...")
if wait_for_collection_ready(memory_service, collection_name):
print(f"集合 '{collection_name}' 已就绪,可以开始使用")
logger.info(f"集合 '{collection_name}' 已就绪,可以开始使用")
return True
else:
print(f"集合 '{collection_name}' 初始化超时")
logger.info(f"集合 '{collection_name}' 初始化超时")
return False
except Exception as e:
print(f"创建集合失败: {str(e)}")
logger.error(f"创建集合失败: {str(e)}")
raise
if __name__ == "__main__":