使用 LangChain 建構採用 LLM 的應用程式
本頁介紹如何使用 LangChain 建構 LLM 輔助應用程式。本頁面的總覽會連結至 GitHub 中的程序指南。
什麼是 LangChain?
LangChain 是 LLM 自動化調度管理框架,可協助開發人員建構生成式 AI 應用程式或檢索增強生成 (RAG) 工作流程。並提供結構、工具和元件,簡化複雜的 LLM 工作流程。
如要進一步瞭解 LangChain,請參閱 Google LangChain 頁面。如要進一步瞭解 LangChain 架構,請參閱
LangChain 產品說明文件。
MySQL 適用的 Cloud SQL 的 LangChain 元件
MySQL 適用的 Cloud SQL 提供下列 LangChain 介面:
瞭解如何搭配使用 LangChain 和
MySQL 適用的 Cloud SQL LangChain 快速入門。
MySQL 適用的 Cloud SQL 向量儲存區
向量儲存區會從向量資料庫擷取及儲存文件和中繼資料。
向量儲存空間可讓應用程式執行語意搜尋,解讀使用者查詢的含意。這類搜尋稱為向量搜尋,可找出概念上與查詢相符的主題。在查詢時,向量儲存區會擷取與搜尋要求嵌入最相似的嵌入向量。在 LangChain 中,向量儲存庫會負責儲存嵌入資料,並為您執行向量搜尋。
如要在 MySQL 適用的 Cloud SQL 中使用向量儲存區,請使用 MySQLVectorStore
類別。
詳情請參閱 LangChain Vector Stores 產品說明文件。
向量儲存庫程序指南
向量儲存空間適用的 MySQL 適用的 Cloud SQL 指南說明如何執行下列操作:
- 安裝整合套件和 LangChain
- 建立
MySQLEngine
物件,並設定連線集區至 MySQL 適用的 Cloud SQL 資料庫
- 初始化資料表
- 使用
VertexAIEmbeddings
建立嵌入物件
- 初始化預設
MySQLVectorStore
- 新增文字
- 刪除文字
- 搜尋文件
- 依向量搜尋文件
- 新增索引來加快向量搜尋查詢速度
- 移除索引
- 建立自訂向量儲存空間
- 使用中繼資料篩選器搜尋文件
MySQL 適用的 Cloud SQL 文件載入器
文件載入器會儲存、載入及刪除 LangChain Document
物件。舉例來說,您可以將資料載入嵌入,以便處理,然後將資料儲存在向量儲存空間,或做為工具,為鏈結提供特定脈絡。
如要從 MySQL 適用的 Cloud SQL 中的文件載入器載入文件,請使用 MySQLLoader
類別。MySQLLoader
方法會從表格傳回一或多份文件。使用 MySQLDocumentSaver
類別儲存及刪除文件。
詳情請參閱 LangChain 文件載入器主題。
文件載入器程序指南
文件載入器適用的 MySQL 適用的 Cloud SQL 指南說明如何執行下列操作:
- 安裝整合套件和 LangChain
- 從資料表載入文件
- 在載入器中新增篩選器
- 自訂連線和驗證
- 指定客戶內容和中繼資料,自訂文件建構方式
- 如何使用及自訂
MySQLDocumentSaver
來儲存及刪除文件
MySQL 適用的 Cloud SQL 的即時通訊訊息記錄
問答應用程式需要對話記錄,才能瞭解對話脈絡,進而回答使用者提出的問題。應用程式可透過 LangChain ChatMessageHistory
類別將訊息儲存至資料庫,並在需要時擷取訊息,以制定進一步的答案。訊息可以是問題、答案、陳述、問候語,或使用者/應用程式在對話期間提供的任何其他文字。ChatMessageHistory
會儲存每則訊息,並將每則對話的訊息串連在一起。
MySQL 適用的 Cloud SQL 會使用 MySQLChatMessageHistory
擴充這個類別。
Chat 訊息記錄程序指南
這份 MySQL 適用的 Cloud SQL 指南說明如何執行下列操作:
- 安裝 LangChain 並驗證 Trusted Cloud by S3NS
- 建立
MySQLEngine
物件,並設定連線集區至 MySQL 適用的 Cloud SQL 資料庫
- 初始化資料表
- 初始化
MySQLChatMessageHistory
類別,即可新增及刪除訊息
- 使用 LangChain 運算式語言 (LCEL) 和 Google 的 Vertex AI 聊天模型,建立訊息記錄的鏈結
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-19 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-19 (世界標準時間)。"],[],[],null,["# Build LLM-powered applications using LangChain\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/sql/docs/postgres/langchain \"View this page for the PostgreSQL database engine\") \\| [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 MySQL\n--------------------------------------------\n\nCloud SQL for MySQL 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\n[LangChain Quickstart for Cloud SQL for MySQL](https://github.com/googleapis/langchain-google-cloud-sql-mysql-python/blob/main/samples/langchain_quick_start.ipynb).\n\nVector store for Cloud SQL for MySQL\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 MySQL, use the\n`MySQLVectorStore` 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 MySQL guide for vector\nstore](https://github.com/googleapis/langchain-google-cloud-sql-mysql-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 `MySQLEngine` object and configure a connection pool to your Cloud SQL for MySQL database\n- Initialize a table\n- Create an embedding object using `VertexAIEmbeddings`\n- Initialize a default `MySQLVectorStore`\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- Remove an index\n- Create a custom vector store\n- Search for documents with a metadata filter\n\nDocument loader for Cloud SQL for MySQL\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 MySQL, use the\n`MySQLLoader` class. `MySQLLoader` methods return one or more documents from a\ntable. Use the `MySQLDocumentSaver` 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 MySQL guide for document\nloader](https://github.com/googleapis/langchain-google-cloud-sql-mysql-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 `MySQLDocumentSaver` to store and delete documents\n\nChat message history for Cloud SQL for MySQL\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 MySQL extends this class with `MySQLChatMessageHistory`.\n\n### Chat message history procedure guide\n\nThe [Cloud SQL for MySQL guide for chat message\nhistory](https://github.com/googleapis/langchain-google-cloud-sql-mysql-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 `MySQLEngine` object and configure a connection pool to your Cloud SQL for MySQL database\n- Initialize a table\n- Initialize the `MySQLChatMessageHistory` 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"]]