How to install Langchain on an M2 Macbook

and develop a simple Q/A app.

Roman Orac

--

Photo by Patrick Tomasso on Unsplash

Recently I started playing with Langchain, which is a popular framework for large language models. Langchain enables you to quickly create a chatbot (or an agent) by using OpenAI’s ChatGPT.

While installing Langchain on an M2 Macbook is quite straightforward with:

pip install langchain

The problem arose later on when I tried to develop a simple app, which answers questions from a text document:

import os
import glob
from langchain.document_loaders import TextLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.chat_models import ChatOpenAI

os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY

loader_list = []
for filepath in glob.glob('*.txt'):
print(filepath)
loader = TextLoader(filepath)
loader_list.append(loader)

index = VectorstoreIndexCreator().from_loaders(loader_list)

def answer_promth(prompt):
return index.query(prompt, llm=ChatOpenAI())

The chroma DB is the AI-native open-source embedding database. The app uses it for memory.

While running the code above on an M2 Macbook you might encounter an error:

ValueError: Could not import chromadb python package. Please install it with `pip install chromadb`.

If you’re a Python developer, you know that the solution is to run to install the missing dependency:

pip install chromadb

But you might encounter another error while installing it:

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for hnswlib
Failed to build hnswlib
ERROR: Could not build wheels for hnswlib, which is required to install pyproject.toml-based projects

The solution that worked for me was to install conda with miniforge:

brew install miniforge
conda init zsh
conda activate base
conda install chromadb

Then the installation works out of the box.

Let me know if this solution also worked for you.

Let’s connect

Talk: Book a call
Socials: YouTube 🎥 | LinkedIn | Twitter
Code: GitHub

--

--