A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.langchain.com/docs/integrations/vectorstores/pgvecto_rs below:

PGVecto.rs | 🦜️🔗 LangChain

PGVecto.rs

This notebook shows how to use functionality related to the Postgres vector database (pgvecto.rs).

%pip install "pgvecto_rs[sdk]" langchain-community
from typing import List

from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings.fake import FakeEmbeddings
from langchain_community.vectorstores.pgvecto_rs import PGVecto_rs
from langchain_core.documents import Document
from langchain_text_splitters import CharacterTextSplitter
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = FakeEmbeddings(size=3)

Start the database with the official demo docker image.

! docker run --name pgvecto-rs-demo -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d tensorchord/pgvecto-rs:latest

Then contruct the db URL



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")


URL = "postgresql+psycopg://{username}:{password}@{host}:{port}/{db_name}".format(
port=PORT,
host=HOST,
username=USER,
password=PASS,
db_name=DB_NAME,
)

Finally, create the VectorStore from the documents:

db1 = PGVecto_rs.from_documents(
documents=docs,
embedding=embeddings,
db_url=URL,

collection_name="state_of_the_union",
)

You can connect to the table laterly with:




db1 = PGVecto_rs.from_collection_name(
embedding=embeddings,
db_url=URL,
collection_name="state_of_the_union",
)

Make sure that the user is permitted to create a table.

Similarity search with score Similarity Search with Euclidean Distance (Default)
query = "What did the president say about Ketanji Brown Jackson"
docs: List[Document] = db1.similarity_search(query, k=4)
for doc in docs:
print(doc.page_content)
print("======================")
Similarity Search with Filter
from pgvecto_rs.sdk.filters import meta_contains

query = "What did the president say about Ketanji Brown Jackson"
docs: List[Document] = db1.similarity_search(
query, k=4, filter=meta_contains({"source": "../../how_to/state_of_the_union.txt"})
)

for doc in docs:
print(doc.page_content)
print("======================")

Or:

query = "What did the president say about Ketanji Brown Jackson"
docs: List[Document] = db1.similarity_search(
query, k=4, filter={"source": "../../how_to/state_of_the_union.txt"}
)

for doc in docs:
print(doc.page_content)
print("======================")

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4