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

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

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

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

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

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

## 使用示例

```typescript theme={null}
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>",
  // use endpoint to initiate client with managed identity
  connectionString: "<CONNECTION_STRING>",
};

/**
 * Sets the threshold similarity score for returning cached results based on vector distance.
 * Cached output is returned only if the similarity score meets or exceeds this threshold;
 * otherwise, a new result is generated. Default is 0.6, adjustable via the constructor
 * to suit various distance functions and use cases.
 * (see: 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 });

// Invoke the model to perform an action
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: {}
  }
*/
```

***

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


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