Postgres 嵌入
Postgres Embedding is an open-source vector similarity search for
Postgresthat usesHierarchical Navigable Small Worlds (HNSW)for approximate nearest neighbor search.
It supports:
- exact and approximate nearest neighbor search using HNSW
- L2 distance
此笔记本展示了如何使用Postgres向量数据库(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
将OpenAI API密钥添加到环境变量中以使用 OpenAIEmbeddings。
import getpass
import os
if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
OpenAI API Key:········
## Loading Environment Variables
from typing import List, Tuple
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import PGEmbedding
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
if "DATABASE_URL" not in 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, score in docs_with_score:
print("-" * 80)
print("Score: ", score)
print(doc.page_content)
print("-" * 80)
在Postgres中使用向量存储
在PG中上传向量存储
db = PGEmbedding.from_documents(
embedding=embeddings,
documents=docs,
collection_name=collection_name,
connection_string=connection_string,
pre_delete_collection=False,
)
创建 HNSW 索引
默认情况下,扩展程序执行顺序扫描搜索,召回率为100%。您可以考虑为近似最近邻(ANN)搜索创建HNSW索引以加快similarity_search_with_score的执行时间。要在向量列上创建HNSW索引,请使用create_hnsw_index函数:
PGEmbedding.create_hnsw_index(
max_elements=10000, dims=1536, m=8, ef_construction=16, ef_search=16
)
上面的函数等同于运行以下 SQL 查询:
CREATE INDEX ON vectors USING hnsw(vec) WITH (maxelements=10000, dims=1536, m=3, efconstruction=16, efsearch=16);
上述语句中使用的 HNSW 索引选项包括:
-
maxelements:定义索引的最大元素数量。这是一个必需参数。上面示例中的值为3。在实际应用中,该值会大得多,例如1000000。一个“元素”指的是数据集中的一个数据点(向量),它在HNSW图中表示为一个节点。通常,您应将此选项设置为能够容纳数据集中行数的值。
-
dims:定义向量数据的维度数。这是一个必需参数。上面的例子中使用了一个较小的值。如果你存储的数据是使用 OpenAI 的 text-embedding-ada-002 模型生成的,该模型支持 1536 维度,则应定义一个值为 1536,例如。
-
m:定义在图构建过程中为每个节点创建的最大双向链接数(也称为“边”)。 以下附加索引选项受支持:
-
efConstruction:定义在索引构建过程中考虑的最近邻数量。默认值为32。
-
efsearch:定义在索引搜索期间考虑的最近邻的数量。默认值为 32。 有关如何配置这些选项以影响 HNSW 算法的信息,请参阅 调整 HNSW 算法。
在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, score in docs_with_score:
print("-" * 80)
print("Score: ", score)
print(doc.page_content)
print("-" * 80)