liveBook · Manning

Retrieval-augmented generation

As explained in Just Use Postgres!

Retrieval-augmented generation is a technique in which a generative model incorporates information retrieved from an external knowledge source when generating an answer.

Retrieval-augmented generation (RAG) lets a generative model use relevant information from a private knowledge base when producing an answer. An application retrieves context, supplies it to the language model, and asks the model to generate a response or perform a task. section 8.0 section 8.6

A Postgres-backed RAG request follows this sequence:

  • Context retrieval—embed the user’s question and use vector similarity search to find relevant private data. section 8.6 section 8.6.3
  • LLM augmentation—provide the retrieved records as context so the model can use knowledge beyond its original training data. section 8.6 section 8.1.1
  • Response generation—let the language model answer the user or perform the requested task using the supplied context. section 8.6 section 8.6.4

Movie recommendations from retrieved context

For the question “I’d like to watch the best movies about pirates,” the application retrieves three pirate-movie records from Postgres, including The Curse of the Black Pearl and Dead Man’s Chest. It passes those records to TinyLlama, which generates conversational recommendations based on them. section 8.6.4

Figure 8.10 Implementing RAG with an LLM, an embedding model, and Postgres
A diagram of a process

AI-generated content may be incorrect.

Use the architecture figure to follow the path from chatbot question through the backend, embedding model, Postgres retrieval, and final LLM response. section 8.6

How Just Use Postgres! covers it

How deep it goes
Traces retrieval, augmentation, and generation through a Python prototype, stopping short of comprehensive production guidance.
Examples
Demonstrates conversational movie recommendations using pirate-film questions, retrieved records, and TinyLlama-generated answers.
What it assumes
Assumes Python, Postgres with pgvector, an embedding model, and an LLM; the prototype supplies setup instructions through a notebook.
What you can do after
Apply it via Python, Ollama, LangChain, Postgres, and pgvector to retrieve context and generate answers.
How it connects
Connects embedding models, vector similarity search, Postgres, pgvector, LangChain, Ollama, and large language models.

Also explained in

Compare how 32 books explain retrieval-augmented generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.