Skip to main content
本页涵盖所有 LangChain 与 Microsoft Azure 及其相关项目的集成。 Azure AI、Dynamic Sessions、SQL Server 的集成包维护在 langchain-azure 仓库中。

聊天模型

我们建议开发者从 (langchain-azure-ai) 开始,以访问 Azure AI Foundry 中所有可用的模型。

Azure AI 聊天补全

使用 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-5.4",
    credential=DefaultAzureCredential(),
)

llm.invoke('Tell me a joke and include some emojis')

嵌入模型

用于嵌入的 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 CosmosDB NoSQL 向量搜索

Azure CosmosDB NoSQL 是一个完全托管的、 全球分布的、无服务器文档数据库,适用于现代应用程序。它以灵活的 JSON 文档形式存储数据,并使用类似 SQL 的查询语言。 这提供了高性能、低延迟以及自动、弹性的可扩展性。它还具有集成的向量搜索功能,适用于生成式 AI 和 RAG 等 AI 工作负载。 这允许您在同一个数据库中存储、索引和查询向量嵌入以及您的操作数据。您可以将向量相似性搜索与传统的基于关键字的搜索相结合以获得相关结果, 并从各种索引方法中选择以获得最佳性能。这种统一的方法简化了应用程序架构并确保了数据一致性。
我们需要安装 langchain-azure-cosmosdbazure-cosmos 包才能使用此向量存储。
pip install -qU langchain-azure-cosmosdb azure-cosmos
from langchain_azure_cosmosdb import AzureCosmosDBNoSqlVectorSearch

vector_search = AzureCosmosDBNoSqlVectorSearch.from_documents(
    documents=docs,
    embedding=openai_embeddings,
    cosmos_client=cosmos_client,
    database_name=database_name,
    container_name=container_name,
    vector_embedding_policy=vector_embedding_policy,
    full_text_policy=full_text_policy,
    indexing_policy=indexing_policy,
    cosmos_container_properties=cosmos_container_properties,
    cosmos_database_properties={},
    full_text_search_enabled=True,
)
查看 使用示例

Azure CosmosDB mongo vCore 向量搜索

Azure CosmosDB Mongo vCore 架构使得 创建具有完整原生 MongoDB 支持的数据库变得容易。您可以应用您的 MongoDB 经验,并通过将应用程序指向 API for MongoDB (vCore) 集群的连接字符串,继续使用您喜爱的 MongoDB 驱动程序、SDK 和工具。
我们需要安装 pymongo 包才能使用此向量存储。
pip install -qU pymongo
from langchain_azure_ai.vectorstores.azure_cosmos_db_mongo_vcore import (
    AzureCosmosDBMongoVCoreVectorSearch,
)

vectorstore = AzureCosmosDBMongoVCoreVectorSearch.from_documents(
    docs,
    openai_embeddings,
    collection=collection,
    index_name=INDEX_NAME,
)
查看 使用示例