8 Postgres for generative AI

 

This chapter covers

  • Exploring Postgres capabilities for generative artificial intelligence (AI)
  • Using the pgvector extension to store and query vector embeddings
  • Optimizing the similarity search with HNSW and IVFFlat indexes
  • Implementing retrieval augmented generation (RAG) with Postgres

Generative artificial intelligence (AI) uses specialized models to produce text, images, videos, and other types of data. These generative models are trained on vast amounts of data and can generate new content based on patterns learned during training. For example, a large language model (LLM) is trained on diverse text data that can produce coherent text in response to user prompts in a natural language such as English or Japanese. An LLM can answer questions, engage in conversation, and perform various tasks by trying to understand the intent behind the prompts.

In this chapter, you learn to use Postgres with generative AI applications. You begin by exploring the ways to use Postgres with LLMs and embedding models. Next, you learn to work with the pgvector extension, which allows you to store vector embeddings and perform similarity searches. Finally, you explore how to expand an LLM’s knowledge base using retrieval-augmented generation (RAG). And you’ll do all this the developer way by transforming our movie recommendation service from Chapter 6 into a generative AI application.

8.1 How to use Postgres with gen AI

8.1.1 Postgres and LLMs

8.1.2 Postgres and embedding models

8.2 Starting Postgres with pgvector

8.3 Generating embeddings

8.3.1 Generating embeddings for movies

8.3.2 Loading final dataset to Postgres

8.4 Performing vector similarity search

8.4.1 Using cosine distance for similarity search

8.4.2 Changing search phrase for better results

8.5 Indexing embeddings

8.5.1 Using IVFFlat index

8.5.2 Using HNSW index

8.6 Implementing RAG

8.6.1 Preparing environment for prototype

8.6.2 Interacting with LLM

8.6.3 Retrieving context for LLM

8.6.4 Using RAG to answer questions

8.7 Summary