Skip to main content
本页涵盖所有 LangChain 与 Microsoft Azure 和其他 Microsoft 产品的集成。

聊天模型

Microsoft 通过 Azure 提供三种主要选项来访问聊天模型:
  1. Azure OpenAI - 通过 Microsoft Azure 的安全企业平台提供对 OpenAI 强大模型(如 o3、4.1 和其他模型)的访问。
  2. Azure AI - 通过统一 API 提供对来自不同提供商(包括 Anthropic、DeepSeek、Cohere、Phi 和 Mistral)的各种模型的访问。
  3. Azure ML - 允许使用 Azure Machine Learning 部署和管理您自己的自定义模型或微调的开源模型。

Azure OpenAI

Microsoft Azure,通常称为 Azure,是一个由 Microsoft 运营的云计算平台,通过全球数据中心提供应用程序和服务的访问、管理和开发。它提供一系列功能,包括软件即服务 (SaaS)、平台即服务 (PaaS) 和基础设施即服务 (IaaS)。Microsoft Azure 支持多种编程语言、工具和框架,包括 Microsoft 特定和第三方软件及系统。
Azure OpenAI 是一项 Azure 服务,提供来自 OpenAI 的强大语言模型,包括用于内容生成、摘要、语义搜索和自然语言到代码翻译的 GPT-3Codex 和 Embeddings 模型系列。
pip install langchain-openai
设置环境变量以访问 Azure OpenAI 服务。
import os

os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<your-endpoint.openai.azure.com/"
os.environ["AZURE_OPENAI_API_KEY"] = "your AzureOpenAI key"
参见使用示例
from langchain_openai import AzureChatOpenAI

Azure AI

Azure AI Foundry 通过 AzureAIOpenAIApiChatModel 类提供对来自各种提供商(包括 Azure OpenAI、DeepSeek R1、Cohere、Phi 和 Mistral)的广泛模型的访问。
pip install -U langchain-azure-ai
配置您的端点。您可以使用带有 DefaultAzureCredential 的项目端点,或直接设置 API 密钥。
export AZURE_AI_PROJECT_ENDPOINT=your-project-endpoint
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
from azure.identity import DefaultAzureCredential

llm = AzureAIOpenAIApiChatModel(
    model="gpt-4.1",
    credential=DefaultAzureCredential(),
)
参见使用示例

Azure ML 聊天在线端点

参见 Azure ML 聊天端点文档 以访问托管在 Azure Machine Learning 上的聊天模型。

LLM

Azure ML

参见使用示例
from langchain_community.llms.azureml_endpoint import AzureMLOnlineEndpoint

Azure OpenAI

参见使用示例
from langchain_openai import AzureOpenAI

嵌入模型

Microsoft 通过 Azure 提供两种主要选项来访问嵌入模型:

Azure OpenAI

参见使用示例
from langchain_openai import AzureOpenAIEmbeddings

Azure AI

pip install -U langchain-azure-ai
配置您的端点。您可以使用带有 DefaultAzureCredential 的项目端点,或直接设置 API 密钥。
export AZURE_AI_PROJECT_ENDPOINT=your-project-endpoint
from langchain_azure_ai.embeddings import AzureAIOpenAIApiEmbeddingsModel
from azure.identity import DefaultAzureCredential

embed_model = AzureAIOpenAIApiEmbeddingsModel(
    model="text-embedding-ada-002",
    credential=DefaultAzureCredential(),
)

中间件

Azure AI 内容安全中间件

Azure AI 内容安全 提供可通过中间件应用于 LangChain 代理的防护措施。langchain-azure-ai 包当前导出用于文本审核、图像审核、提示注入检测、受保护材料检测和基础性评估的中间件。
安装中间件包:
pip install -U langchain-azure-ai
参见 Microsoft Foundry 中间件指南
from langchain_azure_ai.agents.middleware import AzureContentModerationMiddleware

文档加载器

Azure AI 数据

Azure AI Foundry(原 Azure AI Studio) 提供将数据资产上传到云存储并从以下来源注册现有数据资产的功能:
  • Microsoft OneLake
  • Azure Blob Storage
  • Azure Data Lake gen 2
首先,您需要安装几个 Python 包。
pip install azureml-fsspec, azure-ai-generative
参见使用示例
from langchain.document_loaders import AzureAIDataLoader

Azure AI 文档智能

Azure AI 文档智能(原称 Azure Form Recognizer)是一项基于机器学习的服务,可从数字或扫描的 PDF、图像、Office 和 HTML 文件中提取文本(包括手写内容)、表格、文档结构和键值对。 文档智能支持 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML
首先,您需要安装一个 Python 包。
pip install azure-ai-documentintelligence
参见使用示例
from langchain.document_loaders import AzureAIDocumentIntelligenceLoader

Azure Blob 存储

Azure Blob 存储 是 Microsoft 的云对象存储解决方案。Blob 存储针对存储大量非结构化数据进行了优化。非结构化数据是不遵循特定数据模型或定义的数据,例如文本或二进制数据。 Azure Blob 存储 设计用于:
  • 直接向浏览器提供图像或文档。
  • 存储文件以供分布式访问。
  • 流式传输视频和音频。
  • 写入日志文件。
  • 存储用于备份和恢复、灾难恢复和归档的数据。
  • 存储用于本地或 Azure 托管服务分析的数据。
pip install langchain-azure-storage
参见 Azure Blob 存储加载器的使用示例
from langchain_azure_storage.document_loaders import AzureBlobStorageLoader

Microsoft OneDrive

Microsoft OneDrive(原 SkyDrive)是由 Microsoft 运营的文件托管服务。
首先,您需要安装一个 Python 包。
pip install o365
参见使用示例
from langchain_community.document_loaders import OneDriveLoader

Microsoft OneDrive 文件

Microsoft OneDrive(原 SkyDrive)是由 Microsoft 运营的文件托管服务。
首先,您需要安装一个 Python 包。
pip install o365
from langchain_community.document_loaders import OneDriveFileLoader

Microsoft Word

Microsoft Word 是由 Microsoft 开发的文字处理器。
参见使用示例
from langchain_community.document_loaders import UnstructuredWordDocumentLoader

Microsoft Excel

Microsoft Excel 是由 Microsoft 为 Windows、macOS、Android、iOS 和 iPadOS 开发的电子表格编辑器。 它具有计算功能、图表工具、数据透视表和称为 Visual Basic for Applications (VBA) 的宏编程语言。Excel 是 Microsoft 365 软件套件的一部分。
UnstructuredExcelLoader 用于加载 Microsoft Excel 文件。该加载器适用于 .xlsx.xls 文件。 页面内容将是 Excel 文件的原始文本。如果您在 "elements" 模式下使用加载器,则 Excel 文件的 HTML 表示将在文档元数据的 text_as_html 键下可用。 参见使用示例
from langchain_community.document_loaders import UnstructuredExcelLoader

Microsoft SharePoint

Microsoft SharePoint 是一个基于网站的协作系统,使用工作流应用程序、“列表”数据库和其他 Web 部件及安全功能来赋能业务团队协同工作,由 Microsoft 开发。
参见使用示例
from langchain_community.document_loaders.sharepoint import SharePointLoader

Microsoft PowerPoint

Microsoft PowerPoint 是 Microsoft 的演示文稿程序。
参见使用示例
from langchain_community.document_loaders import UnstructuredPowerPointLoader

Microsoft OneNote

首先,安装依赖项:
pip install bs4 msal
参见使用示例
from langchain_community.document_loaders.onenote import OneNoteLoader

Playwright URL 加载器

Playwright 是一个由 Microsoft 开发的开源自动化工具,允许您以编程方式控制和自动化 Web 浏览器。它专为端到端测试、抓取和自动化跨各种 Web 浏览器(如 ChromiumFirefoxWebKit)的任务而设计。
首先,安装依赖项:
pip install playwright unstructured
参见使用示例
from langchain_community.document_loaders.onenote import OneNoteLoader

内存

Azure Cosmos DB 聊天消息历史

Azure Cosmos DB 为对话式 AI 应用程序提供聊天消息历史存储,使您能够以低延迟和高可用性持久化和检索对话历史。
pip install langchain-azure-ai
配置您的 Azure Cosmos DB 连接:
from langchain_azure_ai.chat_message_histories import CosmosDBChatMessageHistory

history = CosmosDBChatMessageHistory(
    cosmos_endpoint="https://<your-account>.documents.azure.com:443/",
    cosmos_database="<your-database>",
    cosmos_container="<your-container>",
    session_id="<session-id>",
    user_id="<user-id>",
    credential="<your-credential>"  # 或使用 connection_string
)

向量存储

Azure Cosmos DB

AI 代理可以依赖 Azure Cosmos DB 作为统一的内存系统解决方案,享受速度、规模和简单性。该服务成功支持了 OpenAI 的 ChatGPT 服务以高可靠性和低维护性动态扩展。由原子记录序列引擎驱动,它是世界上第一个全局分布式 NoSQL关系型向量数据库 服务,提供无服务器模式。 以下是两个可用的 Azure Cosmos DB API,它们可以提供向量存储功能。

Azure Cosmos DB for MongoDB (vCore)

Azure Cosmos DB for MongoDB vCore 使得创建具有完整原生 MongoDB 支持的数据库变得容易。 您可以应用您的 MongoDB 经验,并通过将应用程序指向 MongoDB vCore API 帐户的连接字符串,继续使用您喜欢的 MongoDB 驱动程序、SDK 和工具。 在 Azure Cosmos DB for MongoDB vCore 中使用向量搜索,可以将您的基于 AI 的应用程序与存储在 Azure Cosmos DB 中的数据无缝集成。
安装和设置
参见详细配置说明 我们需要安装 langchain-azure-aipymongo Python 包。
pip install langchain-azure-ai pymongo
在 Microsoft Azure 上部署 Azure Cosmos DB
Azure Cosmos DB for MongoDB vCore 为开发人员提供完全托管的 MongoDB 兼容数据库服务,用于构建具有熟悉架构的现代应用程序。 使用 Cosmos DB for MongoDB vCore,开发人员可以享受原生 Azure 集成、低总体拥有成本 (TCO) 以及熟悉的 vCore 架构的优势,用于迁移现有应用程序或构建新应用程序。 注册 免费开始使用。 参见使用示例
from langchain_azure_ai.vectorstores import AzureCosmosDBMongoVCoreVectorSearch

Azure Cosmos DB NoSQL

Azure Cosmos DB for NoSQL 现在提供预览版的向量索引和搜索功能。 此功能旨在处理高维向量,实现任何规模的高效和准确向量搜索。您现在可以直接将向量与数据一起存储在文档中。这意味着数据库中的每个文档不仅可以包含传统的无模式数据,还可以包含作为文档其他属性的高维向量。数据和向量的这种共置允许高效的索引和搜索,因为向量与它们所代表的数据存储在同一逻辑单元中。这简化了数据管理、AI 应用程序架构以及基于向量的操作效率。
安装和设置
参见详细配置说明 我们需要安装 langchain-azure-aiazure-cosmos Python 包。
pip install langchain-azure-ai azure-cosmos
在 Microsoft Azure 上部署 Azure Cosmos DB
Azure Cosmos DB 通过非常响应式的动态和弹性自动扩展,为现代应用程序和智能工作负载提供解决方案。它在每个 Azure 区域都可用,并可以自动将数据复制到更靠近用户的位置。它具有 SLA 保证的低延迟和高可用性。 注册 免费开始使用。 参见使用示例
from langchain_azure_ai.vectorstores import AzureCosmosDBNoSqlVectorSearch

Azure Database for PostgreSQL

Azure Database for PostgreSQL - Flexible Server 是一个基于开源 Postgres 数据库引擎的关系数据库服务。它是一个完全托管的数据库即服务,可以处理具有可预测性能、安全性、高可用性和动态可扩展性的关键任务工作负载。
参见 Azure Database for PostgreSQL 的设置说明 只需使用来自 Azure 门户的连接字符串 由于 Azure Database for PostgreSQL 是开源的 Postgres,您可以使用 LangChain 的 Postgres 支持 连接到 Azure Database for PostgreSQL。

Azure SQL Database

Azure SQL Database 是一项强大的服务,结合了可扩展性、安全性和高可用性,提供了现代数据库解决方案的所有优势。它还提供专用的向量数据类型和内置函数,简化了向量嵌入在关系数据库内的存储和查询。这消除了对单独向量数据库和相关集成的需求,提高了解决方案的安全性,同时降低了整体复杂性。
通过利用您当前的 SQL Server 数据库进行向量搜索,您可以在最小化费用的同时增强数据能力,并避免迁移到新系统的挑战。
安装和设置
参见详细配置说明 我们需要安装 langchain-sqlserver Python 包。
!pip install langchain-sqlserver==0.1.1
在 Microsoft Azure 上部署 Azure SQL DB
注册 免费开始使用。 参见使用示例
from langchain_sqlserver import SQLServer_VectorStore

Azure AI 搜索

Azure AI Search 是一项云搜索服务,为开发人员提供基础设施、API 和工具,用于大规模检索向量、关键字和混合查询。参见 Azure AI Search 使用示例 了解使用示例。
from langchain_community.vectorstores.azuresearch import AzureSearch

检索器

Azure AI 搜索

Azure AI Search(原称 Azure SearchAzure Cognitive Search)是一项云搜索服务,为开发人员提供基础设施、API 和工具,用于在 Web、移动和企业应用程序中构建丰富的私有、异构内容搜索体验。 搜索是任何向用户显示文本的应用程序的基础,常见场景包括目录或文档搜索、在线零售应用程序或专有内容的数据探索。创建搜索服务时,您将使用以下功能:
  • 用于对包含用户自有内容的搜索索引进行全文搜索的搜索引擎
  • 丰富的索引功能,具有词汇分析和可选的 AI 增强功能,用于内容提取和转换
  • 用于文本搜索、模糊搜索、自动完成、地理搜索等的丰富查询语法
  • 通过 REST API 和 Azure SDK 中的客户端库进行可编程性
  • 在数据层、机器学习层和 AI(AI 服务)的 Azure 集成
参见设置说明 参见使用示例
from langchain_community.retrievers import AzureAISearchRetriever

向量存储

Azure Database for PostgreSQL

Azure Database for PostgreSQL - Flexible Server 是一个基于开源 Postgres 数据库引擎的关系数据库服务。它是一个完全托管的数据库即服务,可以处理具有可预测性能、安全性、高可用性和动态可扩展性的关键任务工作负载。
参见 Azure Database for PostgreSQL 的设置说明 您需要在数据库中启用 pgvector 扩展 才能将 Postgres 用作向量存储。启用扩展后,您可以使用 LangChain 中的 PGVector 连接到 Azure Database for PostgreSQL。 参见使用示例。只需使用来自 Azure 门户的连接字符串

工具

Azure Container Apps 动态会话

我们需要从 Azure Container Apps 服务获取 POOL_MANAGEMENT_ENDPOINT 环境变量。 参见 Azure 动态会话设置说明 我们需要安装一个 Python 包。
pip install langchain-azure-dynamic-sessions
参见使用示例
from langchain_azure_dynamic_sessions import SessionsPythonREPLTool

Bing 搜索

遵循 Bing 搜索工具文档 获取此工具的详细说明和指导。 环境变量 BING_SUBSCRIPTION_KEYBING_SEARCH_URL 是 Bing Search 资源所必需的。
from langchain_community.tools.bing_search import BingSearchResults
from langchain_community.utilities import BingSearchAPIWrapper

api_wrapper = BingSearchAPIWrapper()
tool = BingSearchResults(api_wrapper=api_wrapper)

工具包

Azure AI 服务

我们需要安装几个 Python 包。
pip install azure-ai-formrecognizer azure-cognitiveservices-speech azure-ai-vision-imageanalysis
参见使用示例
from langchain_community.agent_toolkits import azure_ai_services

Azure AI 服务单独工具

azure_ai_services 工具包包括以下工具:

Azure 认知服务

我们需要安装几个 Python 包。
pip install azure-ai-formrecognizer azure-cognitiveservices-speech azure-ai-vision-imageanalysis
参见使用示例
from langchain_community.agent_toolkits import AzureCognitiveServicesToolkit

Azure AI 服务单独工具

azure_ai_services 工具包包括查询 Azure Cognitive Services 的工具:
  • AzureCogsFormRecognizerTool:Form Recognizer API
  • AzureCogsImageAnalysisTool:Image Analysis API
  • AzureCogsSpeech2TextTool:Speech2Text API
  • AzureCogsText2SpeechTool:Text2Speech API
  • AzureCogsTextAnalyticsHealthTool:Text Analytics for Health API
from langchain_community.tools.azure_cognitive_services import (
    AzureCogsFormRecognizerTool,
    AzureCogsImageAnalysisTool,
    AzureCogsSpeech2TextTool,
    AzureCogsText2SpeechTool,
    AzureCogsTextAnalyticsHealthTool,
)

Microsoft Office 365 电子邮件和日历

我们需要安装 O365 Python 包。
pip install O365
参见使用示例
from langchain_community.agent_toolkits import O365Toolkit

Office 365 单独工具

您可以使用 Office 365 工具包中的单独工具:
  • O365CreateDraftMessage:在 Office 365 中创建草稿电子邮件
  • O365SearchEmails:在 Office 365 中搜索电子邮件消息
  • O365SearchEvents:在 Office 365 中搜索日历事件
  • O365SendEvent:在 Office 365 中发送日历事件
  • O365SendMessage:在 Office 365 中发送电子邮件
from langchain_community.tools.office365 import O365CreateDraftMessage
from langchain_community.tools.office365 import O365SearchEmails
from langchain_community.tools.office365 import O365SearchEvents
from langchain_community.tools.office365 import O365SendEvent
from langchain_community.tools.office365 import O365SendMessage

Microsoft Azure PowerBI

我们需要安装 azure-identity Python 包。
pip install azure-identity
参见使用示例
from langchain_community.agent_toolkits import PowerBIToolkit
from langchain_community.utilities.powerbi import PowerBIDataset

PowerBI 单独工具

您可以使用 Azure PowerBI 工具包中的单独工具:
  • InfoPowerBITool:获取 PowerBI 数据集的元数据
  • ListPowerBITool:获取表名
  • QueryPowerBITool:查询 PowerBI 数据集
from langchain_community.tools.powerbi.tool import InfoPowerBITool
from langchain_community.tools.powerbi.tool import ListPowerBITool
from langchain_community.tools.powerbi.tool import QueryPowerBITool

PlayWright 浏览器工具包

Playwright 是一个由 Microsoft 开发的开源自动化工具,允许您以编程方式控制和自动化 Web 浏览器。它专为端到端测试、抓取和自动化跨各种 Web 浏览器(如 ChromiumFirefoxWebKit)的任务而设计。
我们需要安装几个 Python 包。
pip install playwright lxml
参见使用示例
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit

PlayWright 浏览器单独工具

您可以使用 PlayWright 浏览器工具包中的单独工具。
from langchain_community.tools.playwright import ClickTool
from langchain_community.tools.playwright import CurrentWebPageTool
from langchain_community.tools.playwright import ExtractHyperlinksTool
from langchain_community.tools.playwright import ExtractTextTool
from langchain_community.tools.playwright import GetElementsTool
from langchain_community.tools.playwright import NavigateTool
from langchain_community.tools.playwright import NavigateBackTool

Azure Cosmos DB for Apache Gremlin

我们需要安装一个 Python 包。
pip install gremlinpython
参见使用示例
from langchain_community.graphs import GremlinGraph
from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship

实用工具

Bing 搜索 API

Microsoft Bing,通常称为 BingBing Search,是由 Microsoft 拥有和运营的 Web 搜索引擎。
参见使用示例
from langchain_community.utilities import BingSearchAPIWrapper

更多

Microsoft Presidio

Presidio(源自拉丁语 praesidium ‘保护、驻军’) 有助于确保敏感数据得到适当管理和治理。它提供快速识别和匿名化模块,用于文本和图像中的私人实体,如信用卡号、姓名、地点、社会安全号码、比特币钱包、美国电话号码、金融数据等。
首先,您需要安装几个 Python 包并下载 SpaCy 模型。
pip install langchain-experimental openai presidio-analyzer presidio-anonymizer spacy Faker
python -m spacy download en_core_web_lg
参见使用示例
from langchain_experimental.data_anonymizer import PresidioAnonymizer, PresidioReversibleAnonymizer