> 语义缓存功能支持与 Azure Cosmos DB for NoSQL 集成,使用户能够根据用户输入与之前缓存结果之间的语义相似性检索缓存的响应。它利用 AzureCosmosDBNoSQLVectorStore,它存储缓存提示的向量嵌入。这些嵌入支持基于相似性的搜索,使系统能够检索相关的缓存结果。
如果您没有 Azure 账户,可以 创建一个免费账户 开始使用。
设置
您首先需要安装 @langchain/azure-cosmosdb package:
npm install @langchain/azure-cosmosdb @langchain/core
您还需要运行一个 Azure Cosmos DB for NoSQL 实例。您可以按照以下内容在 Azure Portal 上免费部署 本指南.
Once you have your instance running, make sure you have the connection string. If you are using Managed Identity, you need to have the endpoint. You can find them in the Azure Portal, under the "Settings / Keys" section of your instance.
使用示例
AzureCosmosDBNoSQLConfig,
AzureCosmosDBNoSQLSemanticCache,
} from "@langchain/azure-cosmosdb";
const embeddings = new OpenAIEmbeddings();
const config: AzureCosmosDBNoSQLConfig = {
databaseName: "",
containerName: "",
// use endpoint to initiate client with managed identity
connectionString: "",
};
/**
* 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: {}
}
*/