Azure Cosmos DB for MongoDB vCore
Azure Cosmos DB for MongoDB vCore 可以轻松创建完全原生支持 MongoDB 的数据库。你可以将应用指向 MongoDB vCore 账户连接字符串的 API,从而运用你的 MongoDB 经验并继续使用你喜欢的 MongoDB 驱动程序、SDK 和工具。在 Azure Cosmos DB 中为 MongoDB vCore 使用向量搜索,将你的 AI 应用与存储在 Azure Cosmos DB 中的数据无缝集成。
¥Azure Cosmos DB for MongoDB vCore makes it easy to create a database with full native MongoDB support. You can apply your MongoDB experience and continue to use your favorite MongoDB drivers, SDKs, and tools by pointing your application to the API for MongoDB vCore account’s connection string. Use vector search in Azure Cosmos DB for MongoDB vCore to seamlessly integrate your AI-based applications with your data that’s stored in Azure Cosmos DB.
Azure Cosmos DB for MongoDB vCore 为开发者提供完全托管的 MongoDB 兼容数据库服务,用于构建具有熟悉架构的现代应用。
¥Azure Cosmos DB for MongoDB vCore provides developers with a fully managed MongoDB-compatible database service for building modern applications with a familiar architecture.
了解如何利用 Azure Cosmos DB for MongoDB vCore 的向量搜索功能(此页面 版本)。如果你没有 Azure 账户,可以使用 创建免费账户 开始使用。
¥Learn how to leverage the vector search capabilities of Azure Cosmos DB for MongoDB vCore from this page. If you don't have an Azure account, you can create a free account to get started.
设置
¥Setup
你首先需要安装 @langchain/azure-cosmosdb
软件包:
¥You'll first need to install the @langchain/azure-cosmosdb
package:
- npm
- Yarn
- pnpm
npm install @langchain/azure-cosmosdb @langchain/core
yarn add @langchain/azure-cosmosdb @langchain/core
pnpm add @langchain/azure-cosmosdb @langchain/core
你还需要运行一个 Azure Cosmos DB for MongoDB vCore 实例。你可以按照 此指南 在 Azure 门户上免费部署一个版本。
¥You'll also need to have an Azure Cosmos DB for MongoDB vCore instance running. You can deploy a free version on Azure Portal without any cost, following this guide.
实例运行后,请确保你拥有连接字符串和管理密钥。你可以在 Azure 门户中实例的 "连接字符串" 部分下找到它们。然后,你需要设置以下环境变量:
¥Once you have your instance running, make sure you have the connection string and the admin key. You can find them in the Azure Portal, under the "Connection strings" section of your instance. Then you need to set the following environment variables:
AZURE_COSMOSDB_MONGODB_CONNECTION_STRING=
API Reference:
示例
¥Example
以下示例从 Azure Cosmos DB for MongoDB vCore 中的文件索引文档,运行向量搜索查询,并最终使用链式查询,根据检索到的文档以自然语言回答问题。
¥Below is an example that indexes documents from a file in Azure Cosmos DB for MongoDB vCore, runs a vector search query, and finally uses a chain to answer a question in natural language based on the retrieved documents.
import {
AzureCosmosDBMongoDBVectorStore,
AzureCosmosDBMongoDBSimilarityType,
} from "@langchain/azure-cosmosdb";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
import { createRetrievalChain } from "langchain/chains/retrieval";
import { TextLoader } from "langchain/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 Cosmos DB for MongoDB vCore vector store
const store = await AzureCosmosDBMongoDBVectorStore.fromDocuments(
documents,
new OpenAIEmbeddings(),
{
databaseName: "langchain",
collectionName: "documents",
indexOptions: {
numLists: 100,
dimensions: 1536,
similarity: AzureCosmosDBMongoDBSimilarityType.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();
API Reference:
- AzureCosmosDBMongoDBVectorStore from
@langchain/azure-cosmosdb
- AzureCosmosDBMongoDBSimilarityType from
@langchain/azure-cosmosdb
- ChatPromptTemplate from
@langchain/core/prompts
- ChatOpenAI from
@langchain/openai
- OpenAIEmbeddings from
@langchain/openai
- createStuffDocumentsChain from
langchain/chains/combine_documents
- createRetrievalChain from
langchain/chains/retrieval
- TextLoader from
langchain/document_loaders/fs/text
- RecursiveCharacterTextSplitter from
@langchain/textsplitters
相关
¥Related
向量存储 概念指南
¥Vector store conceptual guide
向量存储 操作指南
¥Vector store how-to guides