Database / pgvector basics Interview Questions
How do you use pgvector with LangChain for building AI applications?
LangChain provides a PGVector vector store implementation that wraps pgvector, making it easy to use pgvector as the backend for LangChain-based RAG applications, agents, and chatbots.
# pip install langchain langchain-postgres langchain-openai from langchain_postgres import PGVector from langchain_openai import OpenAIEmbeddings from langchain_core.documents import Document # Connection string: CONNECTION = "postgresql+psycopg://user:pass@localhost/mydb" # Initialise the vector store (creates table and extension if needed) vectorstore = PGVector( connection=CONNECTION, collection_name="documents", embeddings=OpenAIEmbeddings(model="text-embedding-3-small"), use_jsonb=True, # store metadata in JSONB column ) # Add documents docs = [ Document(page_content="pgvector enables vector search in PostgreSQL", metadata={"source": "docs", "category": "database"}), Document(page_content="HNSW is the recommended index for most use cases", metadata={"source": "docs", "category": "indexing"}), ] vectorstore.add_documents(docs) # Similarity search: results = vectorstore.similarity_search( "What index should I use for fast search?", k=3, ) for doc in results: print(doc.page_content, doc.metadata) # Search with score: results_with_score = vectorstore.similarity_search_with_score( "fast nearest neighbour search", k=3, ) for doc, score in results_with_score: print(f"score={score:.4f}: {doc.page_content}") # Use as a LangChain retriever: retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
