Skip to content

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

@AmeiMiao

Description

@AmeiMiao

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:coreThe bug / feature is about astrbot's core, backendbugSomething isn't workingpriority: p0will fix asap

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions