[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[],[],null,["# Build LLM-powered applications using LangChain\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/langchain \"View this page for the MySQL database engine\") \\| PostgreSQL \\| [SQL Server](/sql/docs/sqlserver/langchain \"View this page for the SQL Server database engine\")\n\n\u003cbr /\u003e\n\n|\n| **Preview\n| --- LangChain**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page introduces how to build LLM-powered applications using\n[LangChain](https://www.langchain.com/). The overviews on this\npage link to procedure guides in GitHub.\n\nWhat is LangChain?\n------------------\n\nLangChain is an LLM orchestration framework that helps developers build\ngenerative AI applications or retrieval-augmented generation (RAG) workflows. It\nprovides the structure, tools, and components to streamline complex LLM\nworkflows.\n\nFor more information about LangChain, see the [Google\nLangChain](https://python.langchain.com/docs/integrations/platforms/google)\npage. For more information about the LangChain framework, see the [LangChain](https://python.langchain.com/docs/get_started/introduction)\nproduct documentation.\n\nLangChain components for Cloud SQL for PostgreSQL\n-------------------------------------------------\n\nCloud SQL for PostgreSQL offers the following LangChain interfaces:\n\n- [Vector store](#vector-store)\n- [Document loader](#document-loader)\n- [Chat message history](#chat-message-history)\n\nLearn how to use LangChain with the [LangChain Quickstart for\nCloud SQL for PostgreSQL](https://github.com/googleapis/langchain-google-cloud-sql-pg-python/blob/main/samples/langchain_quick_start.ipynb).\n\nVector store for Cloud SQL for PostgreSQL\n-----------------------------------------\n\nVector store retrieves and stores documents and metadata from a vector database.\nVector store gives an application the ability to perform semantic searches that\ninterpret the meaning of a user query. This type of search is a called a\nvector search, and it can find topics that match the query conceptually. At\nquery time, vector store retrieves the embedding vectors that are\nmost similar to the embedding of the search request. In LangChain, a vector\nstore takes care of storing embedded data and performing the vector search\nfor you.\n\nTo work with vector store in Cloud SQL for PostgreSQL, use the\n`PostgresVectorStore` class.\n\nFor more information, see the [LangChain Vector\nStores](https://python.langchain.com/docs/modules/data_connection/vectorstores/)\nproduct documentation.\n\n### Vector store procedure guide\n\nThe [Cloud SQL for PostgreSQL guide for vector\nstore](https://github.com/googleapis/langchain-google-cloud-sql-pg-python/blob/main/docs/vector_store.ipynb)\nshows you how to do the following:\n\n- Install the integration package and LangChain\n- Create a `PostgresEngine` object and configure a connection pool to your Cloud SQL for PostgreSQL database\n- Initialize a table\n- Create an embedding object using `VertexAIEmbeddings`\n- Initialize a default `PostgresVectorStore`\n- Add texts\n- Delete texts\n- Search for documents\n- Search for documents by vector\n- Add an index to accelerate vector search queries\n- Re-index\n- Remove an index\n- Create a custom vector store\n- Search for documents with a metadata filter\n\nDocument loader for Cloud SQL for PostgreSQL\n--------------------------------------------\n\nThe document loader saves, loads, and deletes a LangChain `Document`\nobjects. For example, you can load data for processing into embeddings and\neither store it in vector store or use it as a tool to provide specific context\nto chains.\n\nTo load documents from document loader in Cloud SQL for PostgreSQL, use the\n`PostgresLoader` class. `PostgresLoader` returns a list of documents from a\ntable using the first column for page content and all other columns for\nmetadata. The default table has the first column as page content and the second\ncolumn as JSON metadata. Each row becomes a document. Use the\n`PostgresDocumentSaver` class to save and delete documents.\n\nFor more information, see the [LangChain Document\nloaders](https://python.langchain.com/docs/modules/data_connection/document_loaders/) topic.\n\n### Document loader procedure guide\n\nThe [Cloud SQL for PostgreSQL guide for document\nloader](https://github.com/googleapis/langchain-google-cloud-sql-pg-python/blob/main/docs/document_loader.ipynb) shows you how to do the following:\n\n- Install the integration package and LangChain\n- Load documents from a table\n- Add a filter to the loader\n- Customize the connection and authentication\n- Customize Document construction by specifying customer content and metadata\n- How to use and customize a `PostgresDocumentSaver` to store and delete documents\n\nChat message history for Cloud SQL for PostgreSQL\n-------------------------------------------------\n\nQuestion and answer applications require a history of the things said in the\nconversation to give the application context for answering further questions\nfrom the user. The LangChain `ChatMessageHistory` class lets the application\nsave messages to a database and retrieve them when needed to formulate further\nanswers. A message can be a question, an answer, a statement, a greeting or any\nother piece of text that the user or application gives during the conversation.\n`ChatMessageHistory` stores each message and chains messages together for each\nconversation.\n\nCloud SQL for PostgreSQL extends this class with `PostgresChatMessageHistory`.\n\n### Chat message history procedure guide\n\nThe [Cloud SQL for PostgreSQL guide for chat message\nhistory](https://github.com/googleapis/langchain-google-cloud-sql-pg-python/blob/main/docs/chat_message_history.ipynb) shows you how to do the following:\n\n- Install LangChain and authenticate to Google Cloud\n- Create a `PostgresEngine` object and configure a connection pool to your Cloud SQL for PostgreSQL database\n- Initialize a table\n- Initialize the `PostgresChatMessageHistory` class to add and delete messages\n- Create a chain for message history using the LangChain Expression Language (LCEL) and Google's Vertex AI chat models\n\nWhat's next\n-----------\n\n- [Build generative AI applications using Cloud SQL](/sql/docs/postgres/ai-overview)\n\n- [Integrate Cloud SQL with Vertex AI](/sql/docs/postgres/integrate-cloud-sql-with-vertex-ai)\n\n- [Work with vector embeddings](/sql/docs/postgres/work-with-vectors)\n\n- [Invoke online predictions from Cloud SQL instances](/sql/docs/postgres/invoke-online-predictions)\n\n- [Understand an example of an embedding workflow](/sql/docs/postgres/understand-example-embedding-workflow)"]]