The LangChain package for CloudSQL for Postgres provides a way to connect to Cloud SQL instances from the LangChain ecosystem.
Main features:
In order to use this package, you first need to go through the following steps:
$ pnpm install @langchain/google-cloud-sql-pg
Before you use the PostgresVectorStore you will need to create a postgres connection through the PostgresEngine interface.
import { Column, PostgresEngine, PostgresEngineArgs, PostgresVectorStore, VectorStoreTableArgs } from "@langchain/google-cloud-sql-pg";
import { SyntheticEmbeddings } from "@langchain/core/utils/testing";
const pgArgs: PostgresEngineArgs = {
user: "db-user",
password: "password"
}
const engine: PostgresEngine = await PostgresEngine.fromInstance(
"project-id",
"region",
"instance-name",
"database-name",
pgArgs
);
const vectorStoreTableArgs: VectorStoreTableArgs = {
metadataColumns: [new Column("page", "TEXT"), new Column("source", "TEXT")],
};
await engine.initVectorstoreTable("my-table", 768, vectorStoreTableArgs);
const embeddingService = new SyntheticEmbeddings({ vectorSize: 768 });
Use a PostgresVectorStore to store embedded data and perform vector similarity search for Postgres.
const pvectorArgs: PostgresVectorStoreArgs = {
idColumn: "ID_COLUMN",
contentColumn: "CONTENT_COLUMN",
embeddingColumn: "EMBEDDING_COLUMN",
metadataColumns: ["page", "source"]
}
const vectorStoreInstance = await PostgresVectorStore.initialize(engine, embeddingService, "my-table", pvectorArgs)
PostgresVectorStore interface methods available:
See the full Vector Store tutorial.
Use PostgresChatMessageHistory to store messages and provide conversation history in Postgres.
First, initialize the Chat History Table and then create the ChatMessageHistory instance.
// ChatHistory table initialization
await engine.initChatHistoryTable("chat_message_table");
const historyInstance = await PostgresChatMessageHistory.initialize(engine, "test", "chat_message_table");
The create method of the PostgresChatMessageHistory receives the engine, the session Id and the table name.
PostgresChatMessageHistory interface methods available:
See the full Chat Message History tutorial.
Use a document loader to load data as LangChain Documents.
import { PostgresEngine, PostgresLoader } from "@langchain/google-cloud-sql-pg";
const documentLoaderArgs: PostgresLoaderOptions = {
tableName: "test_table_custom",
contentColumns: [ "fruit_name", "variety"],
metadataColumns: ["fruit_id", "quantity_in_stock", "price_per_unit", "organic"],
format: "text"
};
const documentLoaderInstance = await PostgresLoader.initialize(PEInstance, documentLoaderArgs);
const documents = await documentLoaderInstance.load();
See the full Loader tutorial.
Enumerator of the Distance strategies.
Convert index attributes to string.
Convert index attributes to string.
Cloud SQL shared connection pool
Google Cloud SQL for PostgreSQL vector store integration.
Google Cloud SQL for PostgreSQL vector store integration.
Convert index attributes to string.