文章目錄
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 faiss56 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 1914 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 @classmethod872 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 faiss56 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 faissImportError: 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 installxyz, where xyz is the package you are trying toinstall.If you wish to install a Python library that isn't in Homebrew,use a virtual environment:python3 -m venv path/to/venvsource path/to/venv/bin/activatepython3 -m pip install xyzIf you wish to install a Python application that isn't in Homebrew,it may be easiest to use 'pipx install xyz', which will manage avirtual environment for you. You can install pipx withbrew install pipxYou may restore the old behavior of pip by passingthe '--break-system-packages' flag to pip, or by adding'break-system-packages = true' to your pip.conf file. The latterwill permanently disable this error.If you disable this error, we STRONGLY recommend that you additionallypass the '--user' flag to pip, or set 'user = true' in your pip.conffile. 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,沒用幾分鐘成功安裝