langchain/templates/sql-pgvector
Leonid Ganeline 163ef35dd1
docs: templates updated titles (#25646)
Updated titles into a consistent format. 
Fixed links to the diagrams.
Fixed typos.
Note: The Templates menu in the navbar is now sorted by the file names.
I'll try sorting the navbar menus by the page titles, not the page file
names.
2024-08-23 01:19:38 -07:00
..
sql_pgvector
tests
.gitignore
LICENSE
pyproject.toml templates, cli: more security deps (#19006) 2024-03-12 20:48:56 -07:00
README.md docs: templates updated titles (#25646) 2024-08-23 01:19:38 -07:00

SQL - Postgres + pgvector

This template enables user to use pgvector for combining PostgreSQL with semantic search / RAG.

It uses PGVector extension as shown in the RAG empowered SQL cookbook

Environment Setup

If you are using ChatOpenAI as your LLM, make sure the OPENAI_API_KEY is set in your environment. You can change both the LLM and embeddings model inside chain.py

And you can configure the following environment variables for use by the template (defaults are in parentheses)

  • POSTGRES_USER (postgres)
  • POSTGRES_PASSWORD (test)
  • POSTGRES_DB (vectordb)
  • POSTGRES_HOST (localhost)
  • POSTGRES_PORT (5432)

If you don't have a postgres instance, you can run one locally in docker:

docker run \
  --name some-postgres \
  -e POSTGRES_PASSWORD=test \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_DB=vectordb \
  -p 5432:5432 \
  postgres:16

And to start again later, use the --name defined above:

docker start some-postgres

PostgreSQL Database setup

Apart from having pgvector extension enabled, you will need to do some setup before being able to run semantic search within your SQL queries.

In order to run RAG over your PostgreSQL database you will need to generate the embeddings for the specific columns you want.

This process is covered in the RAG empowered SQL cookbook, but the overall approach consist of:

  1. Querying for unique values in the column
  2. Generating embeddings for those values
  3. Store the embeddings in a separate column or in an auxiliary table.

Usage

To use this package, you should first have the LangChain CLI installed:

pip install -U langchain-cli

To create a new LangChain project and install this as the only package, you can do:

langchain app new my-app --package sql-pgvector

If you want to add this to an existing project, you can just run:

langchain app add sql-pgvector

And add the following code to your server.py file:

from sql_pgvector import chain as sql_pgvector_chain

add_routes(app, sql_pgvector_chain, path="/sql-pgvector")

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith here. If you don't have access, you can skip this section

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"

If you are inside this directory, then you can spin up a LangServe instance directly by:

langchain serve

This will start the FastAPI app with a server is running locally at http://localhost:8000

We can see all templates at http://127.0.0.1:8000/docs We can access the playground at http://127.0.0.1:8000/sql-pgvector/playground

We can access the template from code with:

from langserve.client import RemoteRunnable

runnable = RemoteRunnable("http://localhost:8000/sql-pgvector")