## PGVecto.rs needs the connection string to the database.## We will load it from the environment variables.import osPORT = 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( port=PORT, host=HOST, username=USER, password=PASS, db_name=DB_NAME,)
Finally, create the VectorStore from the documents:
Copy
Ask AI
db1 = PGVecto_rs.from_documents( documents=docs, embedding=embeddings, db_url=URL, # The table name is f"collection_{collection_name}", so that it should be unique. collection_name="state_of_the_union",)
You can connect to the table laterly with:
Copy
Ask AI
# Create new empty vectorstore with collection_name.# Or connect to an existing vectorstore in database if exists.# Arguments should be the same as when the vectorstore was created.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 Euclidean Distance (Default)
Copy
Ask AI
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("======================")
from pgvecto_rs.sdk.filters import meta_containsquery = "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:
Copy
Ask AI
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("======================")