> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure DocumentDB

> Azure DocumentDB 的向量存储集成

> [Azure DocumentDB](https://learn.microsoft.com/azure/documentdb/) 使得创建具有完整原生 MongoDB 支持的数据库变得轻而易举。你可以运用你的 MongoDB 经验，并通过将应用程序指向连接字符串，继续使用你喜爱的 MongoDB 驱动程序、SDK 和工具。在 Azure DocumentDB 中使用向量搜索，可以将你的基于 AI 的应用程序与存储在 Azure DocumentDB 中的数据无缝集成。

Azure DocumentDB 为开发者提供了一个完全托管的、兼容 MongoDB 的数据库服务，用于构建具有熟悉架构的现代应用程序。

了解如何利用 Azure DocumentDB 的向量搜索功能，请参阅[此页面](https://learn.microsoft.com/azure/documentdb/vector-search)。如果你没有 Azure 账户，可以[创建一个免费账户](https://azure.microsoft.com/free/)来开始使用。

## 设置

你首先需要安装 [`@langchain/azure-cosmosdb`](https://www.npmjs.com/package/@langchain/azure-cosmosdb) 包：

<Tip>
  有关安装 LangChain 包的通用说明，请参阅[此部分](/oss/javascript/langchain/install)。
</Tip>

```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
npm install @langchain/azure-cosmosdb @langchain/core
```

你还需要一个正在运行的 Azure DocumentDB 实例。你可以按照[此指南](https://learn.microsoft.com/azure/documentdb/quickstart-portal)，在 Azure 门户上免费部署一个版本，无需任何费用。

一旦你的实例开始运行，请确保你拥有连接字符串和管理员密钥。你可以在 Azure 门户中，你的实例的“连接字符串”部分找到它们。然后你需要设置以下环境变量：

```bash .env vars theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AZURE_DOCUMENTDB_CONNECTION_STRING=
```

## 示例

以下是一个示例，它将文件中的文档索引到 Azure DocumentDB，运行向量搜索查询，最后使用一个链基于检索到的文档用自然语言回答问题。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import {
  AzureDocumentDBVectorStore,
  AzureDocumentDBSimilarityType,
} from "@langchain/azure-cosmosdb";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { createStuffDocumentsChain } from "@langchain/classic/chains/combine_documents";
import { createRetrievalChain } from "@langchain/classic/chains/retrieval";
import { TextLoader } from "@langchain/classic/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";

// 从文件加载文档
const loader = new TextLoader("./state_of_the_union.txt");
const rawDocuments = await loader.load();
const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 1000,
  chunkOverlap: 0,
});
const documents = await splitter.splitDocuments(rawDocuments);

// 创建 Azure DocumentDB 向量存储
const store = await AzureDocumentDBVectorStore.fromDocuments(
  documents,
  new OpenAIEmbeddings(),
  {
    databaseName: "langchain",
    collectionName: "documents",
    indexOptions: {
      numLists: 100,
      dimensions: 1536,
      similarity: AzureDocumentDBSimilarityType.COS,
    },
  }
);

// 执行相似性搜索
const resultDocuments = await store.similaritySearch(
  "总统对凯坦吉·布朗·杰克逊说了什么？"
);

console.log("相似性搜索结果：");
console.log(resultDocuments[0].pageContent);
/*
  今晚。我呼吁参议院：通过《自由投票法案》。通过《约翰·刘易斯投票权法案》。并且，在你们处理这些的同时，通过《披露法案》，以便美国人能够知道谁在资助我们的选举。

  今晚，我想向一位毕生致力于服务这个国家的人致敬：斯蒂芬·布雷耶大法官——一位陆军退伍军人、宪法学者，以及即将退休的美国最高法院大法官。布雷耶大法官，感谢您的服务。

  总统最严肃的宪法职责之一是提名某人在美国最高法院任职。

  我在四天前就这样做了，当时我提名了巡回上诉法院法官凯坦吉·布朗·杰克逊。她是我们国家顶尖的法律头脑之一，将继续布雷耶大法官卓越的遗产。
*/

// 将存储用作链的一部分
const model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" });
const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "根据以下上下文回答用户的问题：\n\n{context}",
  ],
  ["human", "{input}"],
]);

const combineDocsChain = await createStuffDocumentsChain({
  llm: model,
  prompt: questionAnsweringPrompt,
});

const chain = await createRetrievalChain({
  retriever: store.asRetriever(),
  combineDocsChain,
});

const res = await chain.invoke({
  input: "总统在价格方面的首要任务是什么？",
});

console.log("链响应：");
console.log(res.answer);
/*
  总统的首要任务是控制价格。
*/

// 清理
await store.delete();

await store.close();
```

## 相关内容

* 向量存储[集成概述](/oss/javascript/integrations/vectorstores)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/vectorstores/azure_documentdb.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
