Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Postgres Embedding

Postgres Embedding is an open-source vector similarity search forPostgres that usesHierarchical Navigable Small Worlds (HNSW) for approximate nearest neighbor search.

It supports:

  • exact and approximate nearest neighbor search using HNSW
  • L2 distance

This notebook shows how to use the Postgres vector database (PGEmbedding).

The PGEmbedding integration creates the pg_embedding extension for you, but you run the following Postgres query to add it:

CREATE EXTENSION embedding;
# Pip install necessary package
%pip install--upgrade--quiet langchain-openai langchain-community
%pip install--upgrade--quiet psycopg2-binary
%pip install--upgrade--quiet tiktoken

Add the OpenAI API Key to the environment variables to useOpenAIEmbeddings.

import getpass
import os

if"OPENAI_API_KEY"notin os.environ:
os.environ["OPENAI_API_KEY"]= getpass.getpass("OpenAI API Key:")
OpenAI API Key:········
## Loading Environment Variables
from typingimport List, Tuple
from langchain_community.document_loadersimport TextLoader
from langchain_community.vectorstoresimport PGEmbedding
from langchain_core.documentsimport Document
from langchain_openaiimport OpenAIEmbeddings
from langchain_text_splittersimport CharacterTextSplitter
if"DATABASE_URL"notin os.environ:
os.environ["DATABASE_URL"]= getpass.getpass("Database Url:")
Database Url:········
loader= TextLoader("state_of_the_union.txt")
documents= loader.load()
text_splitter= CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs= text_splitter.split_documents(documents)

embeddings= OpenAIEmbeddings()
connection_string= os.environ.get("DATABASE_URL")
collection_name="state_of_the_union"
db= PGEmbedding.from_documents(
embedding=embeddings,
documents=docs,
collection_name=collection_name,
connection_string=connection_string,
)

query="What did the president say about Ketanji Brown Jackson"
docs_with_score: List[Tuple[Document,float]]= db.similarity_search_with_score(query)
for doc, scorein docs_with_score:
print("-"*80)
print("Score: ", score)
print(doc.page_content)
print("-"*80)

Working with vectorstore in Postgres

Uploading a vectorstore in PG

db= PGEmbedding.from_documents(
embedding=embeddings,
documents=docs,
collection_name=collection_name,
connection_string=connection_string,
pre_delete_collection=False,
)

Create HNSW Index

By default, the extension performs a sequential scan search, with 100% recall. You might consider creating an HNSW index for approximate nearest neighbor (ANN) search to speed upsimilarity_search_with_score execution time. To create the HNSW index on your vector column, use acreate_hnsw_index function:

PGEmbedding.create_hnsw_index(
max_elements=10000, dims=1536, m=8, ef_construction=16, ef_search=16
)

The function above is equivalent to running the below SQL query:

CREATEINDEXON vectorsUSING hnsw(vec)WITH(maxelements=10000, dims=1536, m=3, efconstruction=16, efsearch=16);

The HNSW index options used in the statement above include:

  • maxelements: Defines the maximum number of elements indexed. This is a required parameter. The example shown above has a value of 3. A real-world example would have a much large value, such as 1000000. An "element" refers to a data point (a vector) in the dataset, which is represented as a node in the HNSW graph. Typically, you would set this option to a value able to accommodate the number of rows in your in your dataset.

  • dims: Defines the number of dimensions in your vector data. This is a required parameter. A small value is used in the example above. If you are storing data generated using OpenAI's text-embedding-ada-002 model, which supports 1536 dimensions, you would define a value of 1536, for example.

  • m: Defines the maximum number of bi-directional links (also referred to as "edges") created for each node during graph construction.The following additional index options are supported:

  • efConstruction: Defines the number of nearest neighbors considered during index construction. The default value is 32.

  • efsearch: Defines the number of nearest neighbors considered during index search. The default value is 32.For information about how you can configure these options to influence the HNSW algorithm, refer toTuning the HNSW algorithm.

Retrieving a vectorstore in PG

store= PGEmbedding(
connection_string=connection_string,
embedding_function=embeddings,
collection_name=collection_name,
)

retriever= store.as_retriever()
retriever
VectorStoreRetriever(vectorstore=<langchain_community.vectorstores.pghnsw.HNSWVectoreStore object at 0x121d3c8b0>, search_type='similarity', search_kwargs={})
db1= PGEmbedding.from_existing_index(
embedding=embeddings,
collection_name=collection_name,
pre_delete_collection=False,
connection_string=connection_string,
)

query="What did the president say about Ketanji Brown Jackson"
docs_with_score: List[Tuple[Document,float]]= db1.similarity_search_with_score(query)
for doc, scorein docs_with_score:
print("-"*80)
print("Score: ", score)
print(doc.page_content)
print("-"*80)

Related


[8]ページ先頭

©2009-2025 Movatter.jp