Azure Cosmos DB NoSQL 시맨틱 통합

Azure Cosmos DB NoSQL 시맨틱 통합

LangChain JavaScript로 Azure Cosmos DB NoSQL 시맨틱 캐시와 통합하는 방법을 안내할게요.

출처: 문서

본문

시맨틱 캐시(Semantic Cache) 기능은 Azure Cosmos DB for NoSQL 통합에서 지원되며, 사용자 입력과 이전에 캐시된 결과 사이의 시맨틱 유사성을 기반으로 캐시된 응답을 검색할 수 있게 해줘요. 이는 캐시된 프롬프트의 벡터 임베딩을 저장하는 AzureCosmosDBNoSQLVectorStore를 활용해요. 이러한 임베딩은 유사성 기반 검색을 가능하게 하여 시스템이 관련 캐시 결과를 검색할 수 있게 해줘요.

Azure 계정이 없다면 무료 계정을 만들어 시작할 수 있어요.

설정

먼저 @langchain/azure-cosmosdb 패키지를 설치해야 해요:

See [this section for general instructions on installing LangChain packages](/oss/javascript/langchain/install).
npm install @langchain/azure-cosmosdb @langchain/core

실행 중인 Azure Cosmos DB for NoSQL 인스턴스도 필요해요. 이 가이드에 따라 Azure Portal에서 비용 없이 무료 버전을 배포할 수 있어요.

인스턴스가 실행 중이면 연결 문자열이 있어야 해요. Managed Identity를 사용한다면 엔드포인트가 필요해요. 이들은 Azure Portal에서 인스턴스의 "Settings / Keys" 섹션에서 찾을 수 있어요.

**When using Azure Managed Identity and role-based access control, you must ensure that the database and container have been created beforehand. RBAC does not provide permissions to create databases and containers. You can get more information about the permission model in the [Azure Cosmos DB documentation](https://learn.microsoft.com/azure/cosmos-db/how-to-setup-rbac#permission-model).**

사용 예제

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: {}
  }
*/

[Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers. [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/llm_caching/azure_cosmosdb_nosql.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기