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 和嵌入模型系列,用于内容生成、摘要、语义搜索和自然语言到代码的转换。
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 通过 AzureAIChatCompletionsModel 类提供对来自 Azure OpenAI、DeepSeek R1、Cohere、Phi 和 Mistral 等多种提供商的广泛模型访问。
pip install -U langchain-azure-ai
配置 API 密钥和端点。
export AZURE_AI_CREDENTIAL=your-api-key
export AZURE_AI_ENDPOINT=your-endpoint
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel

llm = AzureAIChatCompletionsModel(
    model_name="gpt-4.1",
    api_version="2024-05-01-preview",
)
查看使用示例

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
配置 API 密钥和端点。
export AZURE_AI_CREDENTIAL=your-api-key
export AZURE_AI_ENDPOINT=your-endpoint
from langchain_azure_ai.embeddings import AzureAIEmbeddingsModel

embed_model = AzureAIEmbeddingsModel(
    model_name="text-embedding-ada-002"
)

文档加载器

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

Azure Blob 存储

Azure Blob Storage 是 Microsoft 面向云的对象存储解决方案。Blob Storage 针对存储海量非结构化数据进行了优化。非结构化数据是指不符合特定数据模型或定义的数据,例如文本或二进制数据。
Azure Blob Storage 专为以下场景设计:
  • 直接向浏览器提供图像或文档。
  • 存储供分布式访问的文件。
  • 流式传输视频和音频。
  • 写入日志文件。
  • 存储用于备份和恢复、灾难恢复及归档的数据。
  • 存储供本地或 Azure 托管服务分析的数据。
pip install langchain-azure-storage
查看 Azure Blob Storage 加载器的使用示例
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 是 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 加载器

PlaywrightMicrosoft 开发的开源自动化工具,允许您以编程方式控制和自动化 Web 浏览器。它专为端到端测试、爬取和自动化 ChromiumFirefoxWebKit 等各种 Web 浏览器上的任务而设计。
首先安装依赖项:
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>"  # or use connection_string
)

向量存储

Azure Cosmos DB

AI Agent 可以依赖 Azure Cosmos DB 作为统一的内存系统解决方案,享受速度、规模和简洁性。该服务成功支撑了 OpenAI ChatGPT 服务以高可靠性和低维护成本动态扩展。它由原子-记录-序列引擎驱动,是全球首款全球分布式 NoSQL关系型向量数据库服务,提供无服务器模式。 以下是两种可提供向量存储功能的 Azure Cosmos DB API。

Azure Cosmos DB for MongoDB(vCore)

Azure Cosmos DB for MongoDB vCore 可轻松创建具有完整原生 MongoDB 支持的数据库。 您可以继续使用熟悉的 MongoDB 驱动程序、SDK 和工具,只需将应用程序指向 MongoDB vCore 账户的连接字符串即可。 在 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 - 灵活服务器是一款基于开源 Postgres 数据库引擎的关系数据库服务。它是完全托管的数据库即服务,可以以可预测的性能、安全性、高可用性和动态可扩展性处理任务关键型工作负载。
查看 Azure Database for PostgreSQL 的设置说明 直接使用 Azure 门户中的连接字符串即可。 由于 Azure Database for PostgreSQL 是开源 Postgres,您可以使用 LangChain 的 Postgres 支持连接到 Azure Database for PostgreSQL。

Azure SQL 数据库

Azure SQL 数据库是一款强大的服务,结合了可扩展性、安全性和高可用性,提供现代数据库解决方案的所有优势。它还提供专用的 Vector 数据类型和内置函数,简化了在关系数据库中直接存储和查询向量嵌入的操作。这消除了对独立向量数据库及相关集成的需求,在降低整体复杂性的同时提高了解决方案的安全性。
通过利用现有 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 和工具,支持大规模向量、关键字和混合查询的信息检索。查看这里获取使用示例。
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 - 灵活服务器是一款基于开源 Postgres 数据库引擎的关系数据库服务。它是完全托管的数据库即服务,可以以可预测的性能、安全性、高可用性和动态可扩展性处理任务关键型工作负载。
查看 Azure Database for PostgreSQL 的设置说明 您需要在数据库中启用 pgvector 扩展才能将 Postgres 用作向量存储。启用扩展后,您可以使用 LangChain 中的 PGVector 连接到 Azure Database for PostgreSQL。 查看使用示例。直接使用 Azure 门户中的连接字符串即可。

工具

Azure Container Apps 动态会话

我们需要从 Azure Container Apps 服务获取 POOL_MANAGEMENT_ENDPOINT 环境变量。 请参阅此处的说明。 需要安装一个 Python 包。
pip install langchain-azure-dynamic-sessions
查看使用示例
from langchain_azure_dynamic_sessions import SessionsPythonREPLTool

Bing 搜索

请参阅此处的文档获取此工具的详细说明和使用指南。 需要来自 Bing Search 资源的环境变量 BING_SUBSCRIPTION_KEYBING_SEARCH_URL
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:图像分析 API
  • AzureCogsSpeech2TextTool:语音转文本 API
  • AzureCogsText2SpeechTool:文本转语音 API
  • AzureCogsTextAnalyticsHealthTool:健康文本分析 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 Toolkit 中的独立工具:
  • 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 Toolkit 中的独立工具:
  • 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 浏览器工具包

PlaywrightMicrosoft 开发的开源自动化工具,允许您以编程方式控制和自动化 Web 浏览器。它专为在 ChromiumFirefoxWebKit 等各种 Web 浏览器上进行端到端测试、爬取和自动化任务而设计。
需要安装几个 Python 包。
pip install playwright lxml
查看使用示例
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit

PlayWright 浏览器独立工具

您可以使用 PlayWright Browser Toolkit 中的独立工具。
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