AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug]faiss.IndexFlatL2 对 embedding dimension 类型过于严格,建议在核心层强制 int 转换

Open AmeiMiao opened this issue 10 months ago • 2 comments

What happened / 发生了什么

问题描述

在 AstrBot 使用 FaissVecDB 作为向量存储后端时,可能在初始化阶段报错并失败,错误信息如下: TypeError: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2' 报错位置在 AstrBot 核心代码中:astrbot/core/db/vec_db/faiss_impl/embedding_storage.py 原因分析 faiss.IndexFlatL2 构造函数要求传入的 dimension 参数必须是 Python 原生的 int 类型。 但在实际使用中,dimension 往往来自 WebUI / 插件配置 / 序列化结果,可能会是: 字符串(如 "1536") 浮点数 numpy.int64 Faiss 的 Python SWIG 绑定对参数类型非常严格,这些情况都会导致构造函数重载匹配失败,从而抛出上述异常。

建议修复方案 diff --git a/astrbot/core/db/vec_db/faiss_impl/embedding_storage.py b/astrbot/core/db/vec_db/faiss_impl/embedding_storage.py @@ class EmbeddingStorage:

  •    self.dimension = dimension
    
  •    dimension = int(dimension)
    
  •    self.dimension = dimension
       self.path = path
       self.index = None
       if path and os.path.exists(path):
           self.index = faiss.read_index(path)
       else:
           base_index = faiss.IndexFlatL2(dimension)
    

Reproduce / 如何复现?

复现方式 使用任意依赖 FaissVecDB 的功能或插件 embedding 维度从配置 / WebUI 读取(非严格 int) 初始化 FaissVecDB

AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

AstrBot:v4.12.1 Python:3.11 部署方式:Docker

OS

Windows

Logs / 报错日志

[08:35:52] [Plug] [ERRO] [v4.12.1] [core.plugin_initializer:318]: 完整初始化流程失败: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2'. Possible C/C++ prototypes are: faiss::IndexFlatL2::IndexFlatL2(faiss::idx_t) faiss::IndexFlatL2::IndexFlatL2() Traceback (most recent call last): File "/AstrBot/data/plugins/astrbot_plugin_livingmemory/core/plugin_initializer.py", line 222, in _complete_initialization self.db = FaissVecDB(db_path, index_path, self.embedding_provider) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/AstrBot/astrbot/core/db/vec_db/faiss_impl/vec_db.py", line 28, in init self.embedding_storage = EmbeddingStorage( ^^^^^^^^^^^^^^^^^ File "/AstrBot/astrbot/core/db/vec_db/faiss_impl/embedding_storage.py", line 20, in init base_index = faiss.IndexFlatL2(dimension) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/faiss/swigfaiss_avx512.py", line 2425, in init _swigfaiss_avx512.IndexFlatL2_swiginit(self, _swigfaiss_avx512.new_IndexFlatL2(*args)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2'. Possible C/C++ prototypes are: faiss::IndexFlatL2::IndexFlatL2(faiss::idx_t) faiss::IndexFlatL2::IndexFlatL2() [08:35:53] [Plug] [ERRO] [v4.12.1] [core.plugin_initializer:87]: LivingMemory 插件初始化失败: 初始化失败: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2'. Possible C/C++ prototypes are: faiss::IndexFlatL2::IndexFlatL2(faiss::idx_t) faiss::IndexFlatL2::IndexFlatL2() Traceback (most recent call last): File "/AstrBot/data/plugins/astrbot_plugin_livingmemory/core/plugin_initializer.py", line 222, in _complete_initialization self.db = FaissVecDB(db_path, index_path, self.embedding_provider) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/AstrBot/astrbot/core/db/vec_db/faiss_impl/vec_db.py", line 28, in init self.embedding_storage = EmbeddingStorage( ^^^^^^^^^^^^^^^^^ File "/AstrBot/astrbot/core/db/vec_db/faiss_impl/embedding_storage.py", line 20, in init base_index = faiss.IndexFlatL2(dimension) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/faiss/swigfaiss_avx512.py", line 2425, in init _swigfaiss_avx512.IndexFlatL2_swiginit(self, _swigfaiss_avx512.new_IndexFlatL2(*args)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2'. Possible C/C++ prototypes are: faiss::IndexFlatL2::IndexFlatL2(faiss::idx_t) faiss::IndexFlatL2::IndexFlatL2() The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/AstrBot/data/plugins/astrbot_plugin_livingmemory/core/plugin_initializer.py", line 83, in initialize await self._complete_initialization() File "/AstrBot/data/plugins/astrbot_plugin_livingmemory/core/plugin_initializer.py", line 321, in _complete_initialization raise InitializationError(f"初始化失败: {e}") from e data.plugins.astrbot_plugin_livingmemory.core.base.exceptions.InitializationError: 初始化失败: Wrong number or type of arguments for overloaded function 'new_IndexFlatL2'. Possible C/C++ prototypes are: faiss::IndexFlatL2::IndexFlatL2(faiss::idx_t) faiss::IndexFlatL2::IndexFlatL2()

Are you willing to submit a PR? / 你愿意提交 PR 吗?

  • [ ] Yes!

Code of Conduct

AmeiMiao avatar Jan 19 '26 01:01 AmeiMiao

Hi @AmeiMiao! I'm Dosu and I’m helping the AstrBot team.

你的分析和修复建议完全正确。faiss.IndexFlatL2 的 Python SWIG 绑定确实要求 dimension 必须是 Python 原生 int,否则会抛出类型错误。目前 AstrBot 的 EmbeddingStorage 实现中,dimension 参数直接传递,没有做类型转换,这会导致你遇到的初始化报错代码参考。

直接在 EmbeddingStorage 构造函数里加上 dimension = int(dimension) 是最直接有效的修复方式,唯一的副作用是如果传入的值无法转换为 int,会抛出 ValueError,但这正好能暴露配置错误,便于排查。

目前主仓库 embedding provider 部分(如 Gemini、OpenAI)已通过 PR #4547 做了类似的 int 转换,但核心层 EmbeddingStorage 仍建议加一层兜底,保证所有入口都安全。

建议:

  1. 在 EmbeddingStorage 构造函数最前面加 dimension = int(dimension)。
  2. 可在代码注释中说明 faiss.IndexFlatL2 对类型的严格要求,方便后续维护。

没有发现官方文档或代码注释有相关提醒,这个修复对提升健壮性很有帮助。

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about AstrBot Share Dosu with your team

dosubot[bot] avatar Jan 19 '26 01:01 dosubot[bot]

已修复。

Soulter avatar Jan 26 '26 13:01 Soulter