> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.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 兼容数据库服务，用于构建具有熟悉架构的现代应用程序。

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

## 设置

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

> [!TIP]
>
> 请参阅 [有关安装 LangChain 包的通用说明部分](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/install.md)。

```bash npm theme={null}
npm install @langchain/azure-cosmosdb @langchain/core
```

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

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

```bash .env vars theme={null}
AZURE_DOCUMENTDB_CONNECTION_STRING=
```

## 示例

下面是一个示例，它从 Azure DocumentDB 中索引文件中的文档，运行向量搜索查询，并最终使用链根据检索到的文档用自然语言回答一个问题。

```typescript theme={null}
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";

// Load documents from file
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);

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

// Performs a similarity search
const resultDocuments = await store.similaritySearch(
  "What did the president say about Ketanji Brown Jackson?"
);

console.log("Similarity search results:");
console.log(resultDocuments[0].pageContent);
/*
  Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you're at it, pass the Disclose Act so Americans can know who is funding our elections.

  Tonight, I'd like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service.

  One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.

  And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation's top legal minds, who will continue Justice Breyer's legacy of excellence.
*/

// Use the store as part of a chain
const model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" });
const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "Answer the user's questions based on the below context:\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: "What is the president's top priority regarding prices?",
});

console.log("Chain response:");
console.log(res.answer);
/*
  The president's top priority is getting prices under control.
*/

// Clean up
await store.delete();

await store.close();
```

## 相关

* 向量存储 [集成概述](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores.md)

***

> 官方原文：[src/oss/javascript/integrations/vectorstores/azure\_documentdb.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/vectorstores/azure_documentdb.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.