Build with AI

What is a vector database, and do you need one?

· By Robert Scales · 4 min read

Short answer

A vector database stores embeddings, the lists of numbers that represent the meaning of text or images, and quickly finds the stored items whose numbers are closest to a query. You need one when you have a large or growing collection to search by meaning; for a few hundred documents, a plain file and a short loop are often enough.

If you have tried to build a "chat with your documents" app this spring, you have met the phrase vector database within minutes. Tutorials pair ChatGPT with names like Pinecone, Chroma and pgvector as if they were required. They are useful, but they are not magic, and many small projects can skip them entirely.

The idea starts with embeddings. An embedding model turns a piece of text into a long list of numbers, a vector, so that texts with similar meanings end up with similar numbers. A vector database is software built to store those vectors and answer one question fast: which stored vectors are nearest to this new one?

How similarity search works

Ordinary databases are excellent at exact matches: find the order with number 4471, or every customer in Quebec. A keyword search finds documents containing the words you typed. Searching by meaning is different. A customer who asks "I can't get into my account" should find the help article titled "Resetting your password," even though they share no words.

Here is the process, step by step.

  1. Split your documents into chunks, such as paragraphs or help articles.
  2. Embed each chunk with an embedding model and store the vector, the original text and any labels (called metadata) together.
  3. Embed the question with the same model.
  4. Compare the question's vector with the stored ones and return the closest few.
  5. Pass those chunks to a language model as context, so it answers from your material. This pattern is often called retrieval-augmented generation, or RAG.

Closeness is measured with a formula. The common choices are Euclidean distance (straight-line distance), cosine similarity (whether two vectors point the same way) and the dot product.

A worked example with tiny vectors

Real embeddings have hundreds or thousands of numbers. To see the idea, imagine an embedding with just three. The vectors below are invented for illustration.

Help articleVectorCosine similarity to the question
A. How do I reset my password?[0.8, 0.2, 0.1]0.99
B. Store opening hours[0.1, 0.9, 0.1]0.24
C. Changing your login email[0.7, 0.1, 0.4]0.96
D. Our refund policy[0.1, 0.2, 0.9]0.34

The question "I can't get into my account" becomes [0.9, 0.1, 0.2]. Cosine similarity runs from -1 to 1, and higher means closer in meaning. Articles A and C come out on top, which is what a human would pick. B and D trail far behind.

With four articles, comparing the question to every one is instant. With ten million, checking each vector takes too long, so vector databases build an index for approximate nearest neighbor search (ANN). An ANN index finds very close matches without comparing every item, accepting a small chance of missing the exact best one in return for much more speed.

Do you actually need one?

Ask three questions.

  • How many chunks? For a few hundred or a few thousand, you can keep the vectors in a file, load them into memory and compare against all of them with a short loop or a library such as NumPy. That is exact, free and easy to inspect.
  • Does the collection change often? If users add documents all day, a database that handles adding, updating and deleting vectors saves you rebuilding a file.
  • Do you need filters? Questions like "only search the French manuals from 2022" need metadata filtering alongside similarity, which vector databases provide.

If all three answers are small, start with a file. You can move to a database later without changing your embeddings, since the vectors are just numbers.

Try it: Take ten short paragraphs from your own notes and write a one-line question each should answer. Embed them with any embedding API, store the vectors in a JSON file, and check whether the top match for each question is the paragraph you expected. Note where it misses before you add any database.

How do Pinecone, Chroma and pgvector differ?

They solve the same problem from three directions.

PineconeChromapgvector
What it isA managed vector database serviceAn open-source embedding databaseAn open-source extension for Postgres
Where it runsPinecone's cloud, used through an APIInside your Python or JavaScript app, or as a serverInside a Postgres database you already have
How you queryAPI callsFour core functions: client, collection, add, queryOrdinary SQL, ordered by distance
EmbeddingsYou supply themCan create them for you or accept your ownYou supply them
Best fitLarge production systems without a database teamPrototypes and apps built with LangChain or LlamaIndexTeams already using Postgres

Pinecone is a hosted service: you send vectors and queries over an API, and Pinecone runs the indexing, splitting across machines and copies for reliability. You manage no servers, but your data lives with a third party, and beyond its free sign-up you pay for capacity.

Chroma is free under the Apache 2.0 license and installs with one pip install chromadb. It can run entirely in memory for quick experiments. By default it embeds text for you with a Sentence Transformers model, or you can plug in OpenAI or Cohere embeddings.

pgvector adds a vector column type to Postgres. You store embeddings next to your other data and find neighbors with a normal query sorted by distance. It does exact search by default; adding an ivfflat index switches to faster, approximate search. If your app already runs on Postgres, this is often the smallest step: no new system to learn or pay for.

Sources

  1. What is a Vector Database? — Pinecone, March 27, 2023
  2. Chroma - the open-source embedding database (README) — Chroma (chroma-core) on GitHub, April 7, 2023
  3. pgvector: Open-source vector similarity search for Postgres (README) — pgvector project on GitHub, May 5, 2023

Get the free Guide to Artificial Intelligence

Sign up for the weekly newsletter and receive the free guide: what changed in AI this week, one lesson, one prompt to try. Unsubscribe in one click.

How we handle your address

Back to top

Display

Your choices are saved in this browser only. No data is sent.

Text size
100%
Colours
Reading
More settings
Accessibility statement