🚶🏻‍♂️ Getting Started

Here you will learn how to use the fastembed package to embed your data into a vector space. The package is designed to be easy to use and fast. It is built on top of the ONNX standard, which allows for fast inference on a variety of hardware (called Runtimes in ONNX).

Quick Start

The fastembed package is designed to be easy to use. We’ll be using TextEmbedding class. It takes a list of strings as input and returns a generator of vectors.

> 💡 You can learn more about generators from Python Wiki

  1. !pip install -Uqq fastembed
  1. import numpy as np
  2. from fastembed import TextEmbedding
  3. # Example list of documents
  4. documents: list[str] = [
  5. "This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.",
  6. "fastembed is supported by and maintained by Qdrant.",
  7. ]
  8. # This will trigger the model download and initialization
  9. embedding_model = TextEmbedding()
  10. print("The model BAAI/bge-small-en-v1.5 is ready to use.")
  11. embeddings_generator = embedding_model.embed(documents)
  12. embeddings_list = list(embeddings_generator)
  13. len(embeddings_list[0]) # Vector of 384 dimensions
  1. Fetching 9 files: 0%| | 0/9 [00:00<?, ?it/s]
  1. The model BAAI/bge-small-en-v1.5 is ready to use.
  1. 384

> 💡 Why do we use generators? > > We use them to save memory mostly. Instead of loading all the vectors into memory, we can load them one by one. This is useful when you have a large dataset and you don’t want to load all the vectors at once.

  1. embeddings_generator = embedding_model.embed(documents)
  2. for doc, vector in zip(documents, embeddings_generator):
  3. print("Document:", doc)
  4. print(f"Vector of type: {type(vector)} with shape: {vector.shape}")
  1. Document: This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc.
  2. Vector of type: <class 'numpy.ndarray'> with shape: (384,)
  3. Document: fastembed is supported by and maintained by Qdrant.
  4. Vector of type: <class 'numpy.ndarray'> with shape: (384,)
  1. embeddings_list = np.array(list(embedding_model.embed(documents)))
  2. embeddings_list.shape
  1. (2, 384)

We’re using BAAI/bge-small-en-v1.5 a state of the art Flag Embedding model. The model does better than OpenAI text-embedding-ada-002. We’ve made it even faster by converting it to ONNX format and quantizing the model for you.

Format of the Document List

  1. List of Strings: Your documents must be in a list, and each document must be a string
  2. For Retrieval Tasks with our default: If you’re working with queries and passages, you can add special labels to them:
  3. Queries: Add “query:” at the beginning of each query string
  4. Passages: Add “passage:” at the beginning of each passage string

Beyond the default model

The default model is built for speed and efficiency. If you need a more accurate model, you can use the TextEmbedding class to load any model from our list of available models. You can find the list of available models using TextEmbedding.list_supported_models().

  1. multilingual_large_model = TextEmbedding("intfloat/multilingual-e5-large")
  1. Fetching 8 files: 0%| | 0/8 [00:00<?, ?it/s]
  1. np.array(
  2. list(multilingual_large_model.embed(["Hello, world!", "你好世界", "¡Hola Mundo!", "नमस्ते!"]))
  3. ).shape # Vector of 1024 dimensions
  1. (4, 1024)

Next: Checkout how to use FastEmbed with Qdrant for similarity search: FastEmbed with Qdrant