Skip to content

Agent Zero AI framework

faiss monkey patch

frdel8e5b108f5827
diff --git a/python/helpers/faiss_monkey_patch.py b/python/helpers/faiss_monkey_patch.py
new file mode 100644
index 000000000..65c997a06
--- /dev/null
+++ b/python/helpers/faiss_monkey_patch.py
@@ -0,0 +1,28 @@
+import sys
+from types import ModuleType, SimpleNamespace
+
+import numpy # real numpy
+
+# for python 3.12 on arm, faiss needs a fake cpuinfo module
+
+
+""" This disgusting hack was brought to you by:
+https://github.com/facebookresearch/faiss/issues/3936
+"""
+
+def hack_faiss_for_python_3_12() -> None:
+ # Create a fake 'cpuinfo' module
+ fake_cpuinfo_module = ModuleType("numpy.distutils.cpuinfo")
+ fake_cpu_module = SimpleNamespace()
+ fake_cpu_module.info = [{"Features": "asimd fp"}] # No 'sve'
+ setattr(fake_cpuinfo_module, "cpu", fake_cpu_module)
+
+ # Create a fake 'distutils' module and assign the cpuinfo module
+ fake_distutils_module = ModuleType("numpy.distutils")
+ setattr(fake_distutils_module, "cpuinfo", fake_cpuinfo_module)
+
+ # Inject the fake modules into sys.modules
+ sys.modules["numpy.distutils"] = fake_distutils_module
+ sys.modules["numpy.distutils.cpuinfo"] = fake_cpuinfo_module
+
+hack_faiss_for_python_3_12()
\ No newline at end of file
diff --git a/python/helpers/memory.py b/python/helpers/memory.py
index 7c43042d5..60e60e2d3 100644
--- a/python/helpers/memory.py
+++ b/python/helpers/memory.py
@@ -5,7 +5,12 @@ from langchain.embeddings import CacheBackedEmbeddings
# from langchain_chroma import Chroma
from langchain_community.vectorstores import FAISS
+
+# faiss needs to be patched for python 3.12 on arm #TODO remove once not needed
+from python.helpers import faiss_monkey_patch
import faiss
+
+
from langchain_community.docstore.in_memory import InMemoryDocstore
from langchain_community.vectorstores.utils import (
DistanceStrategy,
diff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.py
index c1bc1095b..21bd55ce6 100644
--- a/python/helpers/vector_db.py
+++ b/python/helpers/vector_db.py
@@ -1,7 +1,12 @@
from typing import Any, List, Sequence
import uuid
from langchain_community.vectorstores import FAISS
+
+# faiss needs to be patched for python 3.12 on arm #TODO remove once not needed
+from python.helpers import faiss_monkey_patch
import faiss
+
+
from langchain_core.documents import Document
from langchain.storage import InMemoryByteStore
from langchain_community.docstore.in_memory import InMemoryDocstore
diff --git a/requirements.txt b/requirements.txt
index ebf7e9d04..1b1de90ea 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,7 +4,7 @@ beautifulsoup4==4.13.4
browser-use==0.2.5
docker==7.1.0
duckduckgo-search==6.1.12
-faiss-cpu==1.9.0
+faiss-cpu==1.11.0
fastmcp==2.3.4
flask[async]==3.0.3
flask-basicauth==0.2.0
@@ -35,4 +35,4 @@ unstructured==0.15.13
unstructured-client==0.25.9
webcolors==24.6.0
nest-asyncio==1.6.0
-crontab==1.0.1
+crontab==1.0.1
\ No newline at end of file