macos Darwin安装faiss-cpu

文章目录

macos 使用brew instll fass, 后python3.12执行引用faiss包功能出现的问题

安装时遇到问题如下

复制代码
ModuleNotFoundError                       Traceback (most recent call last)
File ~/Src/ai/framework/langchain/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py:55, in dependable_faiss_import(no_avx2)
     54     else:
---> 55         import faiss
     56 except ImportError:

ModuleNotFoundError: No module named 'faiss'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[1], line 19
     14 texts = text_splitter.create_documents(
     15     [page.page_content for page in pages[:4]]
     16 )
     18 embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
---> 19 db = FAISS.from_documents(texts, embeddings)
     21 retriever = db.as_retriever(search_kwargs={"k": 5})
     23 docs = retriever.invoke(query="llama2有多少参数?")

File ~/Src/ai/framework/langchain/.venv/lib/python3.12/site-packages/langchain_core/vectorstores.py:635, in VectorStore.from_documents(cls, documents, embedding, **kwargs)
    633 texts = [d.page_content for d in documents]
    634 metadatas = [d.metadata for d in documents]
--> 635 return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)

File ~/Src/ai/framework/langchain/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py:931, in FAISS.from_texts(cls, texts, embedding, metadatas, ids, **kwargs)
    912 """Construct FAISS wrapper from raw documents.
    913 
    914 This is a user friendly interface that:
   (...)
    928         faiss = FAISS.from_texts(texts, embeddings)
    929 """
    930 embeddings = embedding.embed_documents(texts)
--> 931 return cls.__from(
    932     texts,
    933     embeddings,
    934     embedding,
    935     metadatas=metadatas,
    936     ids=ids,
    937     **kwargs,
    938 )

File ~/Src/ai/framework/langchain/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py:883, in FAISS.__from(cls, texts, embeddings, embedding, metadatas, ids, normalize_L2, distance_strategy, **kwargs)
    871 @classmethod
    872 def __from(
    873     cls,
   (...)
    881     **kwargs: Any,
    882 ) -> FAISS:
--> 883     faiss = dependable_faiss_import()
    884     if distance_strategy == DistanceStrategy.MAX_INNER_PRODUCT:
    885         index = faiss.IndexFlatIP(len(embeddings[0]))

File ~/Src/ai/framework/langchain/.venv/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py:57, in dependable_faiss_import(no_avx2)
     55         import faiss
     56 except ImportError:
---> 57     raise ImportError(
     58         "Could not import faiss python package. "
     59         "Please install it with `pip install faiss-gpu` (for CUDA supported GPU) "
     60         "or `pip install faiss-cpu` (depending on Python version)."
     61     )
     62 return faiss

ImportError: Could not import faiss python package. Please install it with `pip install faiss-gpu` (for CUDA supported GPU) or `pip install faiss-cpu` (depending on Python version).

直接使用本地 pip安装fass-cpu会遇到如下问题

复制代码
venwei@svenweis-MacBook-Pro langchain$ pip3 install faiss-cpu 
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:
    
    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz
    
    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with
    
    brew install pipx
    
    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.
    
    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.
    
    Read more about this behavior here: <https://peps.python.org/pep-0668/>
    ```
    大概意思是让尝试使用brew来安装, 安装完,再使用python来执行文件就出现文章头的错误. 提示内容也说如果非要使用pip安装就使用python虚拟环境. 目前使用python版本为python3.12, 于是就创建了虚拟环境,并且再次使用pip install faiss-cpu, 又遇到如下列表问题,
* 缺少其它安装包
* swig 执行时出现不同的错误
* ...
中间考虑过是否为python版本问题,运行看pypi上看下说明,只是大于Requires: Python >=3.8, 这句直接导致我浪费大量时间,中间也切换过python3.11遇到是swig执行时遇到问题.
于是考虑是不是swig版本与faiss的版本有相关的,又尝试不同的swig版本,结果仍出问题.
最后想编译faiss,在打算着手做时,才发现python的版本要求是3.8~3.10..WTF
下载并使用python3.10安装faiss-cpu,没用几分钟成功安装
相关推荐
黑科技iOS上架12 小时前
iOS framework/dylib动态库定向加固实战
macos·ios·objective-c·cocoa·审核·ios混淆·执行程序差异化
神奇的小猴程序员1 天前
苹果Mac电脑玩我的世界没有版本?用PCL-R即可!
macos
灵韵aura1 天前
macOS 的指令缩写
macos
yqj2343 天前
ESP32-S3中小智项目优化内容
ide·macos·xcode
Mininglamp_27183 天前
Cider量化配置与性能调优:在M系列Mac上压榨Mano-P的每一点推理性能
macos
xuefuhe4 天前
MacOS EAGAIN无法打开terminal
macos
传奇开心果编程4 天前
【SwiftUI入门练中学】第7课 动画与手势:让界面更灵动
学习·macos·ui·ios·swiftui·swift
the3clipse4 天前
MacOS、Linux 与 Windows 视频编解码对比:AV1、HEVC、ProRes 与硬件加速优劣势
linux·windows·macos·hevc·av1·视频解码·硬件解码
传奇开心果编程5 天前
【SwiftUI娓娓道来】第3课:让界面活起来——交互与动画指南
学习·macos·ui·ios·swiftui·swift
朋友圈自动点赞工具5 天前
Gmail 委托邮箱在手机上关不掉
macos·智能手机·cocoa·硬件评测