fix(vector_store): 修复 ids 列表检查逻辑,避免 numpy 数组歧义
fix(memory_tools): 优化邻居节点数据检查逻辑,确保嵌入数据有效 fix(manager): 更新配置项名称为 search_max_expand_depth
This commit is contained in:
@@ -137,7 +137,7 @@ class MemoryManager:
|
|||||||
graph_store=self.graph_store,
|
graph_store=self.graph_store,
|
||||||
persistence_manager=self.persistence,
|
persistence_manager=self.persistence,
|
||||||
embedding_generator=self.embedding_generator,
|
embedding_generator=self.embedding_generator,
|
||||||
max_expand_depth=getattr(self.config, 'max_expand_depth', 1), # 从配置读取默认深度
|
max_expand_depth=getattr(self.config, 'search_max_expand_depth', 1), # 从配置读取默认深度
|
||||||
)
|
)
|
||||||
|
|
||||||
self._initialized = True
|
self._initialized = True
|
||||||
|
|||||||
@@ -210,15 +210,19 @@ class VectorStore:
|
|||||||
import orjson
|
import orjson
|
||||||
similar_nodes = []
|
similar_nodes = []
|
||||||
# 修复:检查 ids 列表长度而不是直接判断真值(避免 numpy 数组歧义)
|
# 修复:检查 ids 列表长度而不是直接判断真值(避免 numpy 数组歧义)
|
||||||
if results.get("ids") is not None and len(results["ids"]) > 0 and len(results["ids"][0]) > 0:
|
ids = results.get("ids")
|
||||||
for i, node_id in enumerate(results["ids"][0]):
|
if ids is not None and len(ids) > 0 and len(ids[0]) > 0:
|
||||||
|
distances = results.get("distances")
|
||||||
|
metadatas = results.get("metadatas")
|
||||||
|
|
||||||
|
for i, node_id in enumerate(ids[0]):
|
||||||
# ChromaDB 返回的是距离,需要转换为相似度
|
# ChromaDB 返回的是距离,需要转换为相似度
|
||||||
# 余弦距离: distance = 1 - similarity
|
# 余弦距离: distance = 1 - similarity
|
||||||
distance = results["distances"][0][i] if results["distances"] else 0.0 # type: ignore
|
distance = distances[0][i] if distances is not None and len(distances) > 0 else 0.0 # type: ignore
|
||||||
similarity = 1.0 - distance
|
similarity = 1.0 - distance
|
||||||
|
|
||||||
if similarity >= min_similarity:
|
if similarity >= min_similarity:
|
||||||
metadata = results["metadatas"][0][i] if results["metadatas"] else {} # type: ignore
|
metadata = metadatas[0][i] if metadatas is not None and len(metadatas) > 0 else {} # type: ignore
|
||||||
|
|
||||||
# 解析 JSON 字符串回列表/字典
|
# 解析 JSON 字符串回列表/字典
|
||||||
for key, value in list(metadata.items()):
|
for key, value in list(metadata.items()):
|
||||||
@@ -368,13 +372,18 @@ class VectorStore:
|
|||||||
try:
|
try:
|
||||||
result = self.collection.get(ids=[node_id], include=["metadatas", "embeddings"])
|
result = self.collection.get(ids=[node_id], include=["metadatas", "embeddings"])
|
||||||
|
|
||||||
# 修复:检查 ids 列表长度而不是直接判断真值(避免 numpy 数组歧义)
|
# 修复:直接检查 ids 列表是否非空(避免 numpy 数组的布尔值歧义)
|
||||||
if result and result.get("ids") is not None and len(result["ids"]) > 0:
|
if result is not None:
|
||||||
return {
|
ids = result.get("ids")
|
||||||
"id": result["ids"][0],
|
if ids is not None and len(ids) > 0:
|
||||||
"metadata": result["metadatas"][0] if result["metadatas"] else {},
|
metadatas = result.get("metadatas")
|
||||||
"embedding": np.array(result["embeddings"][0]) if result["embeddings"] else None,
|
embeddings = result.get("embeddings")
|
||||||
}
|
|
||||||
|
return {
|
||||||
|
"id": ids[0],
|
||||||
|
"metadata": metadatas[0] if metadatas is not None and len(metadatas) > 0 else {},
|
||||||
|
"embedding": np.array(embeddings[0]) if embeddings is not None and len(embeddings) > 0 and embeddings[0] is not None else None,
|
||||||
|
}
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -910,10 +910,12 @@ class MemoryTools:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
neighbor_vector_data = await self.vector_store.get_node_by_id(neighbor_id)
|
neighbor_vector_data = await self.vector_store.get_node_by_id(neighbor_id)
|
||||||
if not neighbor_vector_data or neighbor_vector_data.get("embedding") is None:
|
if neighbor_vector_data is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
neighbor_embedding = neighbor_vector_data["embedding"]
|
neighbor_embedding = neighbor_vector_data.get("embedding")
|
||||||
|
if neighbor_embedding is None:
|
||||||
|
continue
|
||||||
|
|
||||||
# 计算语义相似度
|
# 计算语义相似度
|
||||||
semantic_sim = self._cosine_similarity(
|
semantic_sim = self._cosine_similarity(
|
||||||
|
|||||||
Reference in New Issue
Block a user