Maximizing Cost Efficiency with Huggingface Embeddings for Vector Databases

Andrew Lim YH
2 min readJul 16, 2023
Image by 11485062 from Pixabay


In recent years, vector databases have gained popularity for their ability to efficiently store and retrieve high-dimensional data, such as word embeddings.

Vector embedding is a crucial step in many data-driven applications, but it can often be computationally expensive and resource-intensive. However, by leveraging Huggingface embeddings, we can significantly reduce the cost associated with embedding vectors while maintaining performance and accuracy.

In this article, we will explore how using Huggingface embeddings can save costs compared to traditional embedding approaches

Understanding Huggingface Embeddings

Huggingface is a leading library in natural language processing (NLP) that offers a wide range of pre-trained models and embeddings. These embeddings are derived from state-of-the-art models such as BERT, GPT, or RoBERTa and capture rich semantic information from text. Unlike traditional embedding methods that require training from scratch, Huggingface embeddings provide precomputed representations that can be readily used for various NLP tasks.

How to do it?

from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from transformers import GPT2TokenizerFast

#Inititalise the embedding
hf_embeddings = HuggingFaceEmbeddings()

#Load documents
loader = PyPDFLoader('1Q23_media_briefing_transcript.pdf')
pages = loader.load()

#Split the token
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
text_split = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(tokenizer, chunk_size=800, chunk_overlap=20)
text = text_split.split_documents(pages)

#Create the vectorstore
store = Chroma.from_documents(text,hf_embeddings,persist_directory='saved_vdb')
#Load the vectorstore
vectordb = Chroma(persist_directory='saved_vdb', embedding_function=hf_embeddings)

#Get the semantic paragraph
prompt = 'Your query'
search = vectordb.similarity_search_with_score(prompt)


Exploring the world of Huggingface embeddings and cost-saving techniques has been a delightful journey. Finding efficient ways to embed vectors while minimizing expenses has not only been intellectually stimulating but also a source of satisfaction and happiness.

By leveraging the power of pretrained embeddings, implementing batch processing and embracing transfer learning, we can unlock significant cost savings in the vector embedding process.

Saving costs while embedding vectors is not just about financial gains; it is about fostering creativity, innovation, and a sense of accomplishment. It allows us to allocate resources where they matter the most and fuels our enthusiasm to tackle new challenges.

So, let’s continue to have fun while finding innovative ways to save costs in vector embedding processes. Let’s celebrate the joy of optimizing efficiency, embracing cutting-edge technologies, and making the most of the incredible tools and resources available to us.

May your journey in cost-saving vector embeddings be filled with excitement, productivity, and a happy sense of accomplishment. Here’s to fun and fulfilling cost savings in all your future endeavors!