Building LLM Applications in LangChain using

Building LLM Applications in LangChain using


4 min read

LangChain is a framework for developing applications powered by language models. It enables applications that:

  • Are context-aware: connect a language model to sources of context (prompt instructions, few shot examples, content to ground its response in, etc.)

  • Reason: rely on a language model to reason (about how to answer based on provided context, what actions to take, etc.) is a vector search extension in Postgres. In this blog post, we'll show you how to build LLM applications with LangChain and

Install dependencies

Some dependencies are required to use the LangChain integration:

pip install langchain langchain-openai pgvecto-rs

You could start the postgres instance with extension in a docker container:

docker run \
  --name pgvecto-rs-demo \
  -e POSTGRES_PASSWORD=mysecretpassword \
  -p 5432:5432 \
  -d tensorchord/pgvecto-rs:pg16-v0.2.0

Then you can connect to the database using the psql command line tool. The default username is postgres, and the default password is mysecretpassword.

psql postgresql://postgres:mysecretpassword@localhost:5432/postgres

Run the following SQL to ensure the extension is enabled.


Create the database and load documents

We will show how to use in LangChain to retrieve the most similar vectors.

First, you need to create the text loader and the text splitter to split the text into chunks. We use the markdown file as an example.

## Loading Environment Variables
from dotenv import load_dotenv


from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores.pgvecto_rs import PGVecto_rs
from langchain_openai import OpenAIEmbeddings

loader = TextLoader("./src/getting-started/")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()

Then, we will create the PGVecto_rs instance and load the documents into the database.

## needs the connection string to the database.
## We will load it from the environment variables.
import os

PORT = os.getenv("DB_PORT", 5432)
HOST = os.getenv("DB_HOST", "localhost")
USER = os.getenv("DB_USER", "postgres")
PASS = os.getenv("DB_PASS", "mysecretpassword")
DB_NAME = os.getenv("DB_NAME", "postgres")

# Run tests with shell:
URL = "postgresql+psycopg://{username}:{password}@{host}:{port}/{db_name}".format(

# The pgvectors Module will try to create a table with the name of the collection.
# So, make sure that the collection name is unique and the user has the permission to create a table.

COLLECTION_NAME = "state_of_the_union_test"

db = PGVecto_rs.from_documents(

Query index

Finally, we can retrieve the most similar chunks in LangChain.

query = "What is"
docs_with_score = db.similarity_search_with_score(query)

for doc, score in docs_with_score:
    print("-" * 80)
    print("Score: ", score)
    print("-" * 80)

The output is:

Created a chunk of size 1181, which is longer than the specified 1000
Score:  0.25059962
# Overview

An introduction to the

## What is is a Postgres extension that provides vector similarity search functions. It is written in Rust and based on [pgrx]( It is currently in the beta status, we invite you to try it out in production and provide us with feedback. Read more at [📝our launch blog](

## Why use
Score:  0.29536954
- 💃 **Easy to use**: is a Postgres extension, which means that you can use it directly within your existing database. This makes it easy to integrate into your existing workflows and applications.
- 🔗 **Async indexing**:'s index is asynchronously constructed by the background threads and does not block insertions and always ready for new queries.
- 🥅 **Filtering**: supports filtering. You can set conditions when searching or retrieving points. This is the missing feature of other postgres extensions.
- 🧮 **Quantization**: supports scalar quantization and product qutization up to 64x.
- 🦀 **Rewrite in Rust**: Rust's strict compile-time checks ensure memory safety, reducing the risk of bugs and security issues commonly associated with C extensions.

## Comparison with pgvector
Score:  0.35845917
More details at [📝`` vs. pgvector](/faqs/

## Quick start

For new users, we recommend using the [Docker image]( to get started quickly.

Initialize from an existing database

Above, we created a vector store from scratch. However, often times we want to work with an existing vector store. In order to do that, we can initialize it directly.

db = PGVecto_rs(
    # OpenAI embedding has 1536 dimensions.

Then, we can add vectors to the store and query:

docs_with_score = db.similarity_search_with_score("foo")

The output is:

(Document(page_content='foo'), 0.0)