-
Start the docker compose file.
docker compose up -d -
Download the Ollama models. We'll use the
all-minilmmodel for embeddings and thetinyllamamodel for reasoning.docker compose exec ollama ollama pull all-minilm docker compose exec ollama ollama pull tinyllama
-
Connect to the database in your local developer environment The easiest way connect to the database is with the following command:
docker compose exec -it db psql.Alternatively, you can connect to the database with the following connection string:
postgres://postgres:postgres@localhost:5432/postgres. -
Enable pgai on your database
CREATE EXTENSION IF NOT EXISTS ai CASCADE;
-
Create a table with the data you want to embed from a huggingface dataset
We'll create a table named
wikifrom a few rows of the english-languagewikimedia/wikipediadataset.First, we'll create the table:
CREATE TABLE wiki ( id TEXT PRIMARY KEY, url TEXT, title TEXT, text TEXT );
Then, we'll load the data from the huggingface dataset:
SELECT ai.load_dataset('wikimedia/wikipedia', '20231101.en', table_name=>'wiki', batch_size=>5, max_batches=>1, if_table_exists=>'append');
Related documentation: load dataset from huggingface.
-
Create a vectorizer for
wikiTo enable semantic search on the
wikitable, we need to create vector embeddings for thetextcolumn. We use a vectorizer to automatically create these embeddings and keep them in sync with the data in thewikitable.SELECT ai.create_vectorizer( 'wiki'::regclass, destination => 'wiki_embeddings', embedding => ai.embedding_ollama('all-minilm', 384), chunking => ai.chunking_recursive_character_text_splitter('text') );
Related documentation: vectorizer usage guide and vectorizer API reference.
-
Check the progress of the vectorizer embedding creation
select * from ai.vectorizer_status;
Click to see the output
id source_table target_table view pending_items 1 public.wiki public.wiki_embeddings_store public.wiki_embeddings 10000 All the embeddings have been created when the
pending_itemscolumn is 0. This may take a few minutes as the model is running locally and not on a GPU. -
Search the embeddings
We'll search the embeddings for the concept of "properties of light" even though these words are not in the text of the articles. This is possible because vector embeddings capture the semantic meaning of the text.
Semantic search is a powerful feature in its own right, but it is also a key component of Retrieval Augmented Generation (RAG).
SELECT title, chunk FROM wiki_embeddings ORDER BY embedding <=> ai.ollama_embed('all-minilm', 'properties of light') LIMIT 1;
Click to see the output
title chunk Albedo Water reflects light very differently from typical terrestrial materials. The reflectivity of a water surface is calculated using the Fresnel equations.... This query selects from the
wiki_embeddingsview, which is created by the vectorizer and joins the embeddings with the original data in thewikitable to give us the ability to search using the embeddings but still be able to access (or filter on) all the data in the original table (e.g. thetitlecolumn).Note the
ai.ollama_embedfunction is used to call theall-minilmmodel. This is part of pgai's model calling capabilities.