本页面上的部分或全部信息可能不适用于 Trusted Cloud by S3NS。
使用 LangChain 构建依托 LLM 的应用
本页面介绍了如何使用 LangChain 构建依托 LLM 的应用。本页上的概览链接到 GitHub 中的过程指南。
什么是 LangChain?
LangChain 是一个 LLM 编排框架,可帮助开发者构建生成式 AI 应用或检索增强生成 (RAG) 工作流。它提供了可简化复杂 LLM 工作流的结构、工具和组件。
如需详细了解 LangChain,请参阅 Google LangChain 页面。如需详细了解 LangChain 框架,请参阅
LangChain 产品文档。
适用于 Cloud SQL for MySQL 的 LangChain 组件
Cloud SQL for MySQL 提供以下 LangChain 接口:
如需了解如何使用 LangChain,请参阅适用于 Cloud SQL for MySQL 的 LangChain 快速入门。
Cloud SQL for MySQL 的矢量存储区
矢量存储区从矢量数据库中检索并存储文档和元数据。矢量存储区让应用能够执行解释用户查询含义的语义搜索。这种类型的搜索称为矢量搜索,它可以查找在概念上与查询匹配的主题。查询时,矢量存储区会检索与搜索请求的嵌入最相似的嵌入矢量。在 LangChain 中,矢量存储区负责存储嵌入的数据并为您执行矢量搜索。
如需使用 Cloud SQL for MySQL 中的矢量存储区,请使用 MySQLVectorStore
类。
如需了解详情,请参阅 LangChain 矢量存储区产品文档。
矢量存储区过程指南
关于向量存储区的 Cloud SQL for MySQL 指南介绍了如何执行以下操作:
- 安装集成软件包和 LangChain
- 创建
MySQLEngine
对象并配置与 Cloud SQL for MySQL 数据库的连接池
- 初始化表
- 使用
VertexAIEmbeddings
创建嵌入对象
- 初始化默认
MySQLVectorStore
- 添加文本
- 删除文本
- 搜索文档
- 按矢量搜索文档
- 添加索引以加快矢量搜索查询速度
- 移除索引
- 创建自定义矢量存储区
- 使用元数据过滤条件搜索文档
Cloud SQL for MySQL 的文档加载器
文档加载器会保存、加载和删除 LangChain Document
对象。例如,您可以将要处理的数据加载到嵌入中,并将其存储在矢量存储区,或将其用作向链提供特定上下文的工具。
如需从 Cloud SQL for MySQL 中的文档加载器加载文档,请使用 MySQLLoader
类。MySQLLoader
方法会从表中返回一个或多个文档。使用 MySQLDocumentSaver
类来保存和删除文档。
如需了解详情,请参阅 LangChain 文档加载器主题。
文档加载器过程指南
关于文档加载器的 Cloud SQL for MySQL 指南介绍了如何执行以下操作:
- 安装集成软件包和 LangChain
- 从表中加载文档
- 向加载器添加过滤条件
- 自定义连接和身份验证
- 通过指定客户内容和元数据来自定义文档构造
- 如何使用并自定义
MySQLDocumentSaver
来存储和删除文档
Cloud SQL for MySQL 的聊天消息记录
问答应用需要对话中所述内容的历史记录,以便提供应用上下文来回答用户的其他问题。LangChain ChatMessageHistory
类可让应用将消息保存到数据库,并在需要时检索消息来编制更多答案。消息可以是问题、答案、陈述句、问候语或者用户或应用在对话期间提供的其他任何一段文字。ChatMessageHistory
会存储每条消息,并将每个对话的消息链接在一起。
Cloud SQL for MySQL 使用 MySQLChatMessageHistory
扩展此类。
聊天消息记录过程指南
关于聊天消息记录的 Cloud SQL for MySQL 指南介绍了如何执行以下操作:
- 安装 LangChain 并向 Trusted Cloud by S3NS进行身份验证
- 创建
MySQLEngine
对象并配置与 Cloud SQL for MySQL 数据库的连接池
- 初始化表
- 初始化
MySQLChatMessageHistory
类以添加和删除消息
- 使用 LangChain 表达式语言 (LCEL) 和 Google 的 Vertex AI 聊天模型创建消息记录链
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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"]]