mxbai-embed-large is an open-source embedding model that converts text into 1,024-dimensional vectors. A movie application can embed each movie’s name and description, then store the result in a pgvector column for semantic search. section 8.3.1
A practical embedding workflow:
- Fetch each movie’s name and description from Postgres. section 8.3.1
- Concatenate the fields with a space and generate an embedding. section 8.3.1
- Store the vector in a VECTOR(1024) column and compare it with query embeddings. section 8.3.1 section 8.4.1
- Use the nearest movies as context for a retrieval-augmented generation workflow. section 8.6.3
Semantic movie search
For the query “May the force be with you,” cosine-distance search returns Star Wars: Episode VI - Return of the Jedi and The Acolyte among the top results, even though the exact phrase is absent from their stored descriptions. The example also shows that embedding search can produce an apparently unrelated result, so retrieved matches require evaluation. section 8.4.1
Listing 8.4 Pseudo-code for generating movie embeddings
# Fetch all movies from the database
movies = database.execute("SELECT id, name, description FROM omdb.movies")
# Iterate over each movie and generate the embedding
for each movie in movies:
id = movie.id
name = movie.name
description = movie.description
# Combine name and description for embedding
combined_text = name + " " + description
# Generate embedding using the model
embedding = embedding_model.generate_embedding(combined_text)
# Update the database with the generated embedding
database.execute("
UPDATE omdb.movies SET movie_embedding = ? WHERE id = ?", embedding, id)
Follow the concise pseudocode from database extraction through text concatenation, model inference, and row updates. section 8.3.1
