M1 Air上Python3.12导入FAISS失败求助:官方安装方案无效
解决M1 MacBook Air + Python3.12下FAISS导入失败问题
针对你在M1芯片MacBook Air、Python3.12环境中安装faiss-gpu/faiss-cpu后仍无法导入的问题,以下是适配ARM架构的解决方案:
方案1:使用Conda安装适配ARM的FAISS
M1属于ARM架构,官方PyPI的faiss-cpu包可能未适配Python3.12的ARM版本,建议通过conda-forge安装:
- 确保你安装了支持ARM的conda(比如Miniforge3)
- 执行命令:
conda install -c conda-forge faiss-cpu
如果conda速度慢,可替换为mamba:
mamba install -c conda-forge faiss-cpu
方案2:从源码编译FAISS
如果不想用conda,可手动编译适配ARM的FAISS:
- 安装编译依赖:
brew install cmake llvm
- 克隆FAISS仓库:
git clone https://github.com/facebookresearch/faiss.git cd faiss
- 编译CPU版本:
mkdir build && cd build cmake -DFAISS_ENABLE_GPU=OFF -DFAISS_ENABLE_PYTHON=ON -DCMAKE_CXX_COMPILER=$(brew --prefix llvm)/bin/clang++ .. make -j$(sysctl -n hw.ncpu)
- 安装Python绑定:
cd ../python python setup.py install
方案3:替换为无需FAISS的向量存储
如果上述方案仍有问题,可改用LangChain支持的其他向量存储(如Chroma),无需编译适配:
- 安装Chroma:
pip install chromadb
- 修改代码中的向量存储部分:
将原代码中from langchain_community.vectorstores import FAISS替换为:
from langchain_community.vectorstores import Chroma
将get_vector_store函数修改为:
def get_vector_store(text_chunks): embeddings = GooglePalmEmbeddings() vector_store = Chroma.from_texts(text_chunks, embedding=embeddings) return vector_store
内容的提问来源于stack exchange,提问作者Andy
相关产品推荐
相关产品推荐

