Skip to content

Correct usage #182

@diego-lipinski-de-castro

Description

Currently we have two options

from langchain_postgres import PGVector
from langgraph.store.postgres import PostgresStore

Are they the made for the same use case?
Which one will be kept and maintained in the future?
What are their differences?

I tested both for vector stores and they both worked the same, besides that, what does one have that the other do not?

Example using PostgresStore

from langchain.embeddings import init_embeddings
from langgraph.store.postgres import PostgresStore, AsyncPostgresStore

with PostgresStore.from_conn_string(
    conn_string=os.getenv("VECTOR_DATABASE_URL"),
    index={
        "dims": 1536,
        "embed": init_embeddings("openai:text-embedding-ada-002"),
        # "fields": ["text"]
    }
) as store:
    store.setup()

    store.put(("docs",), "doc1", {"text": "Python tutorial"})
    store.put(("docs",), "doc2", {"text": "TypeScript guide"})
    store.put(("docs",), "doc3", {"text": "Ancient Mayan architecture and urban planning"})
    store.put(("docs",), "doc4", {"text": "Deep sea bioluminescent organisms"})
    store.put(("docs",), "doc5", {"text": "Traditional Japanese pottery techniques"})
    store.put(("docs",), "doc6", {"text": "History of Renaissance art movements"})
    store.put(("docs",), "doc7", {"text": "Quantum mechanics principles"})
    store.put(("docs",), "doc8", {"text": "Sustainable agriculture methods"})
    store.put(("docs",), "doc9", {"text": "Classical music composition theory"})
    store.put(("docs",), "doc10", {"text": "Volcanic formation processes"})

    results = store.search(("docs",), query="programming tutorial", limit=2)
    
    print(results)

Example using langchain_postgres

from langchain_postgres import PGVector
from langchain_core.documents import Document

embeddings = OpenAIEmbeddings(
    # text-embedding-3-large
    model="text-embedding-ada-002"
)

vector_store = PGVector(
    embeddings=embeddings,
    connection=os.getenv("VECTOR_DATABASE_URL"),
    collection_name="langchain",
    use_jsonb=True,
    create_extension=True,
)

documents = [
    Document(page_content="Python tutorial", metadata={"source": "doc1"}),
    Document(page_content="TypeScript guide", metadata={"source": "doc2"}),
    Document(page_content="Other guide", metadata={"source": "doc3"}),
    Document(page_content="Machine learning fundamentals", metadata={"source": "doc4"}),
    Document(page_content="Web development best practices", metadata={"source": "doc5"}),
    Document(page_content="Data structures and algorithms", metadata={"source": "doc6"}),
    Document(page_content="Cloud computing architecture", metadata={"source": "doc7"}),
    Document(page_content="Software testing methodologies", metadata={"source": "doc8"}),
    Document(page_content="DevOps principles and practices", metadata={"source": "doc9"}),
    Document(page_content="Database optimization techniques", metadata={"source": "doc10"})
]

vector_store.add_documents(documents=documents)

retriever = vector_store.as_retriever()

queries = [
    "software development",
    "ancient history",
    "natural sciences",
    "cultural practices"
]

for query in queries:
    results = retriever.invoke(query)
    print(f"\nQuery: {query}")
    for doc in results:
        print(f"- {doc.page_content}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions