style: 格式化代码
This commit is contained in:
@@ -77,7 +77,10 @@ class CacheManager:
|
||||
embedding_array = embedding_array.flatten()
|
||||
|
||||
# 检查维度是否符合预期
|
||||
expected_dim = getattr(CacheManager, "embedding_dimension", None) or global_config.lpmm_knowledge.embedding_dimension
|
||||
expected_dim = (
|
||||
getattr(CacheManager, "embedding_dimension", None)
|
||||
or global_config.lpmm_knowledge.embedding_dimension
|
||||
)
|
||||
if embedding_array.shape[0] != expected_dim:
|
||||
logger.warning(f"嵌入向量维度不匹配: 期望 {expected_dim}, 实际 {embedding_array.shape[0]}")
|
||||
return None
|
||||
|
||||
@@ -82,22 +82,26 @@ async def check_and_migrate_database():
|
||||
if dialect.name == "sqlite" and isinstance(default_arg, bool):
|
||||
# SQLite 将布尔值存储为 0 或 1
|
||||
default_value = "1" if default_arg else "0"
|
||||
elif hasattr(compiler, 'render_literal_value'):
|
||||
elif hasattr(compiler, "render_literal_value"):
|
||||
try:
|
||||
# 尝试使用 render_literal_value
|
||||
default_value = compiler.render_literal_value(default_arg, column.type)
|
||||
except AttributeError:
|
||||
# 如果失败,则回退到简单的字符串转换
|
||||
default_value = f"'{default_arg}'" if isinstance(default_arg, str) else str(default_arg)
|
||||
default_value = (
|
||||
f"'{default_arg}'" if isinstance(default_arg, str) else str(default_arg)
|
||||
)
|
||||
else:
|
||||
# 对于没有 render_literal_value 的旧版或特定方言
|
||||
default_value = f"'{default_arg}'" if isinstance(default_arg, str) else str(default_arg)
|
||||
|
||||
default_value = (
|
||||
f"'{default_arg}'" if isinstance(default_arg, str) else str(default_arg)
|
||||
)
|
||||
|
||||
sql += f" DEFAULT {default_value}"
|
||||
|
||||
if not column.nullable:
|
||||
sql += " NOT NULL"
|
||||
|
||||
|
||||
conn.execute(text(sql))
|
||||
logger.info(f"成功向表 '{table_name}' 添加列 '{column_name}'。")
|
||||
|
||||
@@ -131,4 +135,3 @@ async def check_and_migrate_database():
|
||||
continue
|
||||
|
||||
logger.info("数据库结构检查与自动迁移完成。")
|
||||
|
||||
|
||||
@@ -423,4 +423,4 @@ async def store_action_info(
|
||||
except Exception as e:
|
||||
logger.error(f"[SQLAlchemy] 存储动作信息时发生错误: {e}")
|
||||
traceback.print_exc()
|
||||
return None
|
||||
return None
|
||||
|
||||
@@ -781,6 +781,7 @@ async def get_db_session() -> AsyncGenerator[Optional[AsyncSession], None]:
|
||||
session = SessionLocal()
|
||||
# 对于 SQLite,在会话开始时设置 PRAGMA
|
||||
from src.config.config import global_config
|
||||
|
||||
if global_config.database.database_type == "sqlite":
|
||||
await session.execute(text("PRAGMA busy_timeout = 60000"))
|
||||
await session.execute(text("PRAGMA foreign_keys = ON"))
|
||||
|
||||
@@ -781,23 +781,23 @@ class ModuleColoredConsoleRenderer:
|
||||
thought_color = "\033[38;5;218m"
|
||||
# 分割消息内容
|
||||
prefix, thought = event_content.split("内心思考:", 1)
|
||||
|
||||
|
||||
# 前缀部分(“决定进行回复,”)使用模块颜色
|
||||
if module_color:
|
||||
prefix_colored = f"{module_color}{prefix.strip()}{RESET_COLOR}"
|
||||
else:
|
||||
prefix_colored = prefix.strip()
|
||||
|
||||
|
||||
# “内心思考”部分换行并使用专属颜色
|
||||
thought_colored = f"\n\n{thought_color}内心思考:{thought.strip()}{RESET_COLOR}\n"
|
||||
|
||||
|
||||
# 重新组合
|
||||
# parts.append(prefix_colored + thought_colored)
|
||||
# 将前缀和思考内容作为独立的part添加,避免它们之间出现多余的空格
|
||||
if prefix_colored:
|
||||
parts.append(prefix_colored)
|
||||
parts.append(thought_colored)
|
||||
|
||||
|
||||
elif module_color:
|
||||
event_content = f"{module_color}{event_content}{RESET_COLOR}"
|
||||
parts.append(event_content)
|
||||
|
||||
@@ -98,13 +98,13 @@ class ChromaDBImpl(VectorDBBase):
|
||||
"n_results": n_results,
|
||||
**kwargs,
|
||||
}
|
||||
|
||||
|
||||
# 修复ChromaDB的where条件格式
|
||||
if where:
|
||||
processed_where = self._process_where_condition(where)
|
||||
if processed_where:
|
||||
query_params["where"] = processed_where
|
||||
|
||||
|
||||
return collection.query(**query_params)
|
||||
except Exception as e:
|
||||
logger.error(f"查询集合 '{collection_name}' 失败: {e}")
|
||||
@@ -114,7 +114,7 @@ class ChromaDBImpl(VectorDBBase):
|
||||
"query_embeddings": query_embeddings,
|
||||
"n_results": n_results,
|
||||
}
|
||||
logger.warning(f"使用回退查询模式(无where条件)")
|
||||
logger.warning("使用回退查询模式(无where条件)")
|
||||
return collection.query(**fallback_params)
|
||||
except Exception as fallback_e:
|
||||
logger.error(f"回退查询也失败: {fallback_e}")
|
||||
@@ -124,19 +124,19 @@ class ChromaDBImpl(VectorDBBase):
|
||||
"""
|
||||
处理where条件,转换为ChromaDB支持的格式
|
||||
ChromaDB支持的格式:
|
||||
- 简单条件: {"field": "value"}
|
||||
- 简单条件: {"field": "value"}
|
||||
- 操作符条件: {"field": {"$op": "value"}}
|
||||
- AND条件: {"$and": [condition1, condition2]}
|
||||
- OR条件: {"$or": [condition1, condition2]}
|
||||
"""
|
||||
if not where:
|
||||
return None
|
||||
|
||||
|
||||
try:
|
||||
# 如果只有一个字段,直接返回
|
||||
if len(where) == 1:
|
||||
key, value = next(iter(where.items()))
|
||||
|
||||
|
||||
# 处理列表值(如memory_types)
|
||||
if isinstance(value, list):
|
||||
if len(value) == 1:
|
||||
@@ -146,7 +146,7 @@ class ChromaDBImpl(VectorDBBase):
|
||||
return {key: {"$in": value}}
|
||||
else:
|
||||
return {key: value}
|
||||
|
||||
|
||||
# 多个字段使用 $and 操作符
|
||||
conditions = []
|
||||
for key, value in where.items():
|
||||
@@ -157,9 +157,9 @@ class ChromaDBImpl(VectorDBBase):
|
||||
conditions.append({key: {"$in": value}})
|
||||
else:
|
||||
conditions.append({key: value})
|
||||
|
||||
|
||||
return {"$and": conditions}
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"处理where条件失败: {e}, 使用简化条件")
|
||||
# 回退到只使用第一个条件
|
||||
@@ -189,7 +189,7 @@ class ChromaDBImpl(VectorDBBase):
|
||||
processed_where = None
|
||||
if where:
|
||||
processed_where = self._process_where_condition(where)
|
||||
|
||||
|
||||
return collection.get(
|
||||
ids=ids,
|
||||
where=processed_where,
|
||||
@@ -202,7 +202,7 @@ class ChromaDBImpl(VectorDBBase):
|
||||
logger.error(f"从集合 '{collection_name}' 获取数据失败: {e}")
|
||||
# 如果获取失败,尝试不使用where条件重新获取
|
||||
try:
|
||||
logger.warning(f"使用回退获取模式(无where条件)")
|
||||
logger.warning("使用回退获取模式(无where条件)")
|
||||
return collection.get(
|
||||
ids=ids,
|
||||
limit=limit,
|
||||
|
||||
Reference in New Issue
Block a user