Local Embeddings with LangChain.

Or: New Knowledge for Large Language Models.

Local Embeddings with LangChain.

Update: Thursday 25th January 2024.

TL;DR.

The post discusses generating local embeddings with LangChain. Embeddings address (some of the) memory limitations in Large Language Models (LLMs). I demonstrate an embedding implementation using various AI tools. Using embeddings for long-term memory management has potential applications in many fields, including business, science, and engineering.

Attributions:

An Introduction.

In the fascinating world of AI, Large Language Models (LLMs) hold a special place due to their impressive ability to understand and generate human-like responses. However, their static memory, limited to what they were trained on, poses certain challenges. To tackle these limitations, I turn to using LangChain to create local embeddings. In this post, I delve deep into this innovative solution, demonstrating how to implement embeddings using tools like Ollama, Llama2, bs4, GPT4All, Chroma, and LangChain itself.

The purpose of this post is to present a way for LLMs to use locally generated embeddings.

The Big Picture.

LLMs (large language models) have static knowledge, limited to what they learned up until the day they were trained and compiled. They cannot retain new information beyond their base knowledge, meaning they only know what they've been taught and nothing more.

Embeddings (partly) resolve one of the known LLM memory issues.

Prerequisites.

  • A Linux-based distro (I use Ubuntu),

  • Python 3.9 or later, and

  • Anaconda.

Updating the System.

  • I update the system:
sudo apt clean && \
sudo apt update && \
sudo apt dist-upgrade -y && \
sudo apt --fix-broken install && \
sudo apt autoclean && \
sudo apt autoremove -y

Creating an Anaconda Environment.

  • I use conda to display a list of Anaconda environments:
conda env list
  • I use conda to create, and activate, a new environment named (-n) (langchain):
conda create -n langchain python=3.11 -y && conda activate langchain

NOTE: This command creates the (langchain) environment, then activates the (langchain) environment.

Changing the LangChain Home Directory.

NOTE: I will define the home directory with settings in the environment directory.

  • I create the LangChain home directory:
mkdir ~/LangChain
  • I make new directories within the (langchain) environment:
mkdir -p $HOME/anaconda3/envs/langchain/etc/conda/activate.d
  • I use Nano to create the set_working_directory.sh shell script:
sudo nano $HOME/anaconda3/envs/langchain/etc/conda/activate.d/set_working_directory.sh
  • I add the following to the script, save the changes (CTRL + S), and exit (CTRL + X) the Nano text editor:
cd ~/LangChain
  • I activate the (base) environment:
conda activate
  • I activate the (langchain) environment:
conda activate langchain

NOTE: I should now, by default, be in the ~/LangChain home directory.

Installing Ollama.

  • I install Ollama:
curl https://ollama.ai/install.sh | sh

Installing Llama2.

  • I install Llama2:
ollama pull llama2

Installing LangChain.

  • I install LangChain:
pip install langchain

Creating a Python Module.

  • I create a Python module called langchain-test.py:
sudo nano ./langchain-test.py
  • I add the following to the langchain-test.py module, save the file, and exit Nano:
from langchain.llms import Ollama
ollama = Ollama(base_url='http://localhost:11434', model='llama2')
print(ollama('Why is the sky blue?'))
  • I run the langchain-test.py module:
python3 langchain-test.py

Installing More Requirements.

  • I install bs4:
pip install bs4
  • I install GPT4All:
pip install gpt4all
  • I install Chroma:
pip install chromadb

Creating Another Python Module.

  • I create another Python module called langchain-ollama.py:
sudo nano langchain-ollama.py
  • I add the following to the langchain-ollama.py module:
# Import Ollama
from langchain.llms import Ollama
# Import the document loader
from langchain.document_loaders import WebBaseLoader
# Import the text splitter
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Import the GPT4All embeddings tool
from langchain.embeddings import GPT4AllEmbeddings
# Import the vector store
from langchain.vectorstores import Chroma
# Import chains
from langchain.chains import RetrievalQA

ollama = Ollama(base_url='http://localhost:11434', model='llama2')

# Load the document from a URL
loader = WebBaseLoader('https://solodev.app/12s-my-2024-technology-stack')
# Load the document contents as 'data'
data = loader.load()
# Define the 'text_splitter' params
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
# Split the 'data'
all_splits = text_splitter.split_documents(data)
# Instantiate the vector store
vectorstore = Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings())
# Use chains to link tasks
qachain = RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever())

question = "What technologies will be used in 2024?"
print(qachain({"query": question}))
  • I run the langchain-ollama.py module:
python3 langchain-ollama.py

The Results.

Local embeddings with LangChain offer one solution to the memory limitations of Large Language Models (LLMs). By using tools like Ollama, Llama2, bs4, GPT4All, Chroma, and of course, LangChain itself, I can enhance the static knowledge of an LLM, making it more adaptable and versatile. This process, although technical, is achievable with a Linux-based operating system and Python 3.9 or later. The potential applications of this technology are vast, promising specialist knowledge adaptations in many fields including business, science, and engineering.

In Conclusion.

In the fascinating world of AI, Large Language Models (LLMs) have some memory limitations. They can only retain information up to the day they were trained and compiled. But guess what? There is a workaround.

Local embeddings with LangChain!

The embedding approach helps LLMs overcome their memory limitations, making them more flexible and useful. Imagine LLMs not being restricted by their initial knowledge. With modern AI tools, I can increase an LLM's knowledge base. Embeddings offer specialized knowledge adaptations in many specialist fields.

I found the concept of embeddings easy to understand and so I created a comprehensive guide for myself. With a Linux-based operating system and Python 3.9 or later (and a decent GPU), I can extend the knowledge base of my favourite LLMs.

What's your take on this? What do you think the impact of embeddings will be? I'm looking forward to your thoughts and insights!

Until next time: Be safe, be kind, be awesome.