In this blog post, we will show you how to build a RAG pipeline with pgvecto.rs and LlamaIndex.
LlamaIndex is a simple, flexible data framework for connecting custom data sources to large language models (LLMs).
pgvecto.rs provides a LlamaIndex integration that allows you to retrieve the most similar vectors. In this blog post, we will show you how to build a RAG pipeline with pgvecto.rs and LlamaIndex.
Install dependencies
Some dependencies are required to use the LlamaIndex integration:
pip install llama-index "pgvecto_rs[sdk]"
You could start the Postgres instance with pgvecto.rs extension in a docker container:
First, create the text loader and the text splitter to split the text into chunks. We use the markdown file pgvecto-rs/getting-started/overview.md as an example.
importloggingimportosimportsyslogging.basicConfig(stream=sys.stdout,level=logging.INFO)logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))frompgvecto_rs.sdkimportPGVectoRsURL="postgresql+psycopg://{username}:{password}@{host}:{port}/{db_name}".format(port=os.getenv("DB_PORT","5432"),host=os.getenv("DB_HOST","localhost"),username=os.getenv("DB_USER","postgres"),password=os.getenv("DB_PASS","mysecretpassword"),db_name=os.getenv("DB_NAME","postgres"),)client=PGVectoRs(db_url=URL,collection_name="example",dimension=1536,# Using OpenAI’s text-embedding-ada-002)
Then set up the OpenAI API key and load the documents into the database.
importosos.environ["OPENAI_API_KEY"]="sk-..."fromllama_indeximportSimpleDirectoryReader,VectorStoreIndexfromllama_index.vector_storesimportPGVectoRsStore# load documentsdocuments=SimpleDirectoryReader("./src/getting-started/overview.md").load_data()# initialize without metadata filterfromllama_index.storage.storage_contextimportStorageContextvector_store=PGVectoRsStore(client=client)storage_context=StorageContext.from_defaults(vector_store=vector_store)index=VectorStoreIndex.from_documents(documents,storage_context=storage_context)
Query index
Finally, we can retrieve the most similar chunks.
# set Logging to DEBUG for more detailed outputsquery_engine=index.as_query_engine()response=query_engine.query("What did the author do growing up?")