> ## 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 Cosmos DB NoSQL 语义集成

> 使用 LangChain JavaScript 与 Azure Cosmos DB NoSQL 语义缓存集成。

> 语义缓存功能支持与 Azure Cosmos DB for NoSQL 集成，使用户能够根据用户输入与先前缓存结果之间的语义相似性来检索缓存的响应。它利用 [AzureCosmosDBNoSQLVectorStore](/oss/javascript/integrations/vectorstores/azure_cosmosdb_nosql)，该存储保存缓存提示的向量嵌入。这些嵌入支持基于相似性的搜索，允许系统检索相关的缓存结果。

如果您没有 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 Cosmos DB for NoSQL 实例。您可以按照 [此指南](https://learn.microsoft.com/azure/cosmos-db/nosql/quickstart-portal) 在 Azure 门户上免费部署一个版本，无需任何费用。

一旦您的实例运行起来，请确保您拥有连接字符串。如果您使用托管身份，则需要端点。您可以在 Azure 门户中，在实例的“设置 / 密钥”部分下找到它们。

<Info>
  **使用 Azure 托管身份和基于角色的访问控制时，必须确保数据库和容器已预先创建。RBAC 不提供创建数据库和容器的权限。您可以在 [Azure Cosmos DB 文档](https://learn.microsoft.com/azure/cosmos-db/how-to-setup-rbac#permission-model) 中获取有关权限模型的更多信息。**
</Info>

## 使用示例

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import {
  AzureCosmosDBNoSQLConfig,
  AzureCosmosDBNoSQLSemanticCache,
} from "@langchain/azure-cosmosdb";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings();
const config: AzureCosmosDBNoSQLConfig = {
  databaseName: "<DATABASE_NAME>",
  containerName: "<CONTAINER_NAME>",
  // 使用端点通过托管身份初始化客户端
  connectionString: "<CONNECTION_STRING>",
};

/**
 * 设置基于向量距离返回缓存结果的阈值相似度分数。
 * 仅当相似度分数达到或超过此阈值时才返回缓存输出；
 * 否则，将生成新结果。默认值为 0.6，可通过构造函数调整
 * 以适应各种距离函数和用例。
 * (参见：https://aka.ms/CosmosVectorSearch)。
 */

const similarityScoreThreshold = 0.5;
const cache = new AzureCosmosDBNoSQLSemanticCache(
  embeddings,
  config,
  similarityScoreThreshold
);

const model = new ChatOpenAI({ model: "gpt-5.4-mini", cache });

// 调用模型执行操作
const response1 = await model.invoke("Do something random!");
console.log(response1);
/*
  AIMessage {
    content: "Sure! I'll generate a random number for you: 37",
    additional_kwargs: {}
  }
*/

const response2 = await model.invoke("Do something random!");
console.log(response2);
/*
  AIMessage {
    content: "Sure! I'll generate a random number for you: 37",
    additional_kwargs: {}
  }
*/
```

***

<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/llm_caching/azure_cosmosdb_nosql.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
