Use this file to discover all available pages before exploring further.
Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability and offers database engines such as PostgreSQL.This guide provides a quick overview of how to use Cloud SQL for PostgreSQL to store vector embeddings with the PostgresVectorStore class.
To use the PostgresVectorStore library, you’ll need to install the @langchain/google-cloud-sql-pg package and then follow the steps below.First, you’ll need to log in to your Google Cloud account and set the following environment variables based on your Google Cloud project; these will be defined based on how you want to configure (fromInstance, fromEngine, fromEngineArgs) your PostgresEngine instance :
To instantiate a PostgresVectorStore, you’ll first need to create a database connection through the PostgresEngine, then initialize the vector store table and finally call the .initialize() method to instantiate the vector store.
import { Column, PostgresEngine, PostgresEngineArgs, PostgresVectorStore, PostgresVectorStoreArgs, VectorStoreTableArgs,} from "@langchain/google-cloud-sql-pg";import { SyntheticEmbeddings } from "@langchain/core/utils/testing"; // This is used as an Embedding serviceimport * as dotenv from "dotenv";dotenv.config();const peArgs: PostgresEngineArgs = { user: process.env.DB_USER ?? "", password: process.env.PASSWORD ?? "",};// PostgresEngine instantiationconst engine: PostgresEngine = await PostgresEngine.fromInstance( process.env.PROJECT_ID ?? "", process.env.REGION ?? "", process.env.INSTANCE_NAME ?? "", process.env.DB_NAME ?? "", peArgs);const vectorStoreArgs: VectorStoreTableArgs = { metadataColumns: [new Column("page", "TEXT"), new Column("source", "TEXT")],};// Vector store table initializationawait engine.initVectorstoreTable("my_vector_store_table", 768, vectorStoreArgs);const embeddingService = new SyntheticEmbeddings({ vectorSize: 768 });const pvectorArgs: PostgresVectorStoreArgs = { metadataColumns: ["page", "source"],};// PostgresVectorStore instantiationconst vectorStore = await PostgresVectorStore.initialize( engine, embeddingService, "my_vector_store_table", pvectorArgs);
You can delete one or more Documents from the vector store by passing the arrays of ids to be deleted:
// deleting a documentconst id1 = ids[0];await vectorStore.delete({ ids: [id1] });// deleting more than one documentawait vectorStore.delete({ ids: ids });
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.