---
title: "Building a RAG application with Llama 3.1 and pgvector"
description: "A small RAG pipeline built on Neon Postgres with pgvector as the vector store, OctoAI's gte-large embedding model producing 1024-dimensional vectors, and Llama 3.1 405B generating answers."
url: "https://swe.quest/articles/building-a-rag-application-with-llama-3-1-and-pgvector-aa7660e0eb99da02"
canonical: "https://neon.com/blog/building-a-rag-application-with-llama-3-1-and-pgvector"
source: "Neon"
published: "2024-07-30"
difficulty: "medium"
kind: "How-to"
reading_minutes: 12
tags: ["rag","vector-search","postgres","llms","python"]
---

# Building a RAG application with Llama 3.1 and pgvector

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-07-30, 12 min read, How-to. Original: [https://neon.com/blog/building-a-rag-application-with-llama-3-1-and-pgvector](https://neon.com/blog/building-a-rag-application-with-llama-3-1-and-pgvector)

## Summary

A small RAG pipeline built on Neon Postgres with pgvector as the vector store, OctoAI's gte-large embedding model producing 1024-dimensional vectors, and Llama 3.1 405B generating answers. The walkthrough enables the vector extension, creates a table with a VECTOR(1024) column, and embeds and inserts a CSV of fake quotes from Python via psycopg2. At query time it embeds the user input, retrieves the two nearest quotes with a cosine-distance ORDER BY ... LIMIT 2 query, and injects them into the system prompt. Embedding dimension directly drives storage cost, e.g. 1024 for gte-large versus up to 3072 for OpenAI's model.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [RAG & retrieval](https://swe.quest/tags/rag.md) (AI & ML)
- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Python basics ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- LLM APIs ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

pgvector, Llama 3.1, Neon, OctoAI, gte-large, psycopg2

## Related posts

- [Storing OpenAI embeddings in Postgres with pgvector](https://swe.quest/articles/storing-openai-embeddings-in-postgres-with-pgvector-19a5d03614f86fc6.md): Supabase · 2023-02-06 · Medium · 11 min read · Vector search, PostgreSQL, RAG & retrieval, LLMs
  pgvector adds a vector data type and distance operators (Euclidean \<-\>, negative inner product \<#\>, cosine \<=\>) to Postgres, so OpenAI text-embedding-ada-002 embeddings (1536 dimensions) can be stored and queried for similarity directly in the database.
- [Building Data-Aware AI Applications with Heroku AI and LlamaIndex](https://swe.quest/articles/building-data-aware-ai-applications-with-heroku-ai-and-llamaindex-8f85403b0aabecc2.md): Heroku Engineering · 2025-08-19 · Medium · 4 min read · RAG & retrieval, Vector search, PostgreSQL, Python, LLMs
  A RAG stack on Heroku pairs LlamaIndex for ingestion, indexing and retrieval with Heroku Managed Inference (Claude 4 Sonnet via an OpenAI-compatible API), Cohere Embed Multilingual for embeddings, and Heroku Postgres with pgvector as the vector store.
- [Build an end-to-end RAG pipeline entirely in psql using pgrag and DeepSeek](https://swe.quest/articles/build-an-end-to-end-rag-pipeline-entirely-in-psql-using-pgrag-and-deepseek-250dc9ab3281f7d0.md): Neon · 2025-01-30 · Medium · 9 min read · RAG & retrieval, PostgreSQL, Vector search, LLMs
  pgrag, Neon's open-source Postgres extension, lets you build a complete RAG pipeline inside psql.
- [ChatGPT plugins now support Postgres & Supabase](https://swe.quest/articles/chatgpt-plugins-now-support-postgres-supabase-db9d10a95a8d96b9.md): Supabase · 2023-05-25 · Medium · 7 min read · RAG & retrieval, Vector search, PostgreSQL, LLMs
  Supabase contributed Postgres and Supabase datastore providers to OpenAI's ChatGPT Retrieval Plugin, storing text-embedding-ada-002 embeddings in pgvector.
- [Easy Embeddings Indexing Pipelines with Redpanda and Neon](https://swe.quest/articles/easy-embeddings-indexing-pipelines-with-redpanda-and-neon-5e15b71c2d49e429.md): Neon · 2024-09-06 · Medium · 3 min read · Vector search, Data pipelines, PostgreSQL, Queues & messaging, RAG & retrieval
  An embeddings indexing pipeline can be built from configuration alone with Redpanda Connect: it consumes e-commerce invoice messages from a Redpanda Serverless topic over SASL/TLS, builds a text description of each order with Bloblang, computes embeddings locally via Ollama's nomic-embed-text model, and flattens items into separate messages before writing them to Neon Postgres with pgvector for natural-language search.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
