> ## 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.

# QdrantVectorStore 集成

> 使用 LangChain JavaScript 集成 QdrantVectorStore。

> [!TIP]
>
> **兼容性**: 仅适用于 Node.js。

[Qdrant](https://qdrant.tech/) 是一个向量相似性搜索引擎。它提供了一个生产就绪的服务，具有便捷的 API 来存储、搜索和管理点（带有附加负载的向量）。

本指南提供了快速入门 Qdrant [向量存储](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores.md) 的概述。有关所有 `QdrantVectorStore` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-qdrant/QdrantVectorStore)。

## 概述

### 集成细节

| 类 | 包 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores/qdrant.md) | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: |
| [`QdrantVectorStore`](https://reference.langchain.com/javascript/langchain-qdrant/QdrantVectorStore) | [`@langchain/qdrant`](https://www.npmjs.com/package/@langchain/qdrant) | ✅ | ![NPM - 下载量](https://img.shields.io/npm/dm/@langchain/qdrant?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/qdrant?style=flat-square\&label=%20&) |

## 设置

要使用 Qdrant 向量存储，设置一个 Qdrant 实例并安装 `@langchain/qdrant` 和 `@langchain/core`。`@langchain/qdrant` 包含了 Qdrant REST 客户端（`@qdrant/js-client-rest`）。

本指南使用 [OpenAI 嵌入](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/embeddings/openai.md) 作为示例。您可以使用 [其他支持的嵌入模型](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/embeddings.md) 代替。

```bash npm theme={null}
npm install @langchain/qdrant @langchain/core @langchain/openai
```

```bash yarn theme={null}
yarn add @langchain/qdrant @langchain/core @langchain/openai
```

```bash pnpm theme={null}
pnpm add @langchain/qdrant @langchain/core @langchain/openai
```

安装完所需依赖后，按照 [Qdrant 设置说明](https://qdrant.tech/documentation/quickstart/) 在您的计算机上使用 Docker 运行一个 Qdrant 实例。记下您的容器运行的 URL。

### 凭证

设置一个 `QDRANT_URL` 环境变量：

```typescript theme={null}
// e.g. http://localhost:6333
process.env.QDRANT_URL = "your-qdrant-url"
```

如果您在本指南中使用 OpenAI 嵌入，请设置您的 OpenAI 密钥：

```typescript theme={null}
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
```

如果您想自动跟踪您的模型调用，您也可以通过取消注释以下内容来设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```typescript theme={null}
// process.env.LANGSMITH_TRACING="true"
// process.env.LANGSMITH_API_KEY="your-api-key"
```

## 实例化

```typescript theme={null}
import { QdrantVectorStore } from "@langchain/qdrant";
import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

const vectorStore = await QdrantVectorStore.fromExistingCollection(embeddings, {
  url: process.env.QDRANT_URL,
  collectionName: "langchainjs-testing",
});
```

## 管理向量存储

### 向向量存储添加项

```typescript theme={null}
import type { Document } from "@langchain/core/documents";

const document1: Document = {
  pageContent: "The powerhouse of the cell is the mitochondria",
  metadata: { source: "https://example.com" }
};

const document2: Document = {
  pageContent: "Buildings are made out of brick",
  metadata: { source: "https://example.com" }
};

const document3: Document = {
  pageContent: "Mitochondria are made out of lipids",
  metadata: { source: "https://example.com" }
};

const document4: Document = {
  pageContent: "The 2024 Olympics are in Paris",
  metadata: { source: "https://example.com" }
}

const documents = [document1, document2, document3, document4];

await vectorStore.addDocuments(documents);
```

目前不支持顶层文档 ID 和删除。

## 查询向量存储

一旦您的向量存储创建完成，并且相关文档已添加，您很可能希望在您的链或智能体运行期间查询它。

### 直接查询

执行简单的相似性搜索可以按以下方式完成：

```typescript theme={null}
const filter = {
  "must": [
      { "key": "metadata.source", "match": { "value": "https://example.com" } },
  ]
};

const similaritySearchResults = await vectorStore.similaritySearch("biology", 2, filter);

for (const doc of similaritySearchResults) {
  console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
```

```text theme={null}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
```

有关 Qdrant 过滤语法的更多信息，请参阅 [此页面](https://qdrant.tech/documentation/concepts/filtering/)。请注意，所有值都必须以 `metadata.` 开头。

如果您想执行相似性搜索并接收相应的分数，可以运行：

```typescript theme={null}
const similaritySearchWithScoreResults = await vectorStore.similaritySearchWithScore("biology", 2, filter)

for (const [doc, score] of similaritySearchWithScoreResults) {
  console.log(`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(doc.metadata)}]`);
}
```

```text theme={null}
* [SIM=0.165] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.148] Mitochondria are made out of lipids [{"source":"https://example.com"}]
```

### 将其转换为检索器

您还可以将向量存储转换为 [检索器](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/retrieval.md)，以便在您的链中更轻松地使用。

```typescript theme={null}
const retriever = vectorStore.asRetriever({
  // Optional filter
  filter: filter,
  k: 2,
});
await retriever.invoke("biology");
```

```javascript theme={null}
[
  Document {
    pageContent: 'The powerhouse of the cell is the mitochondria',
    metadata: { source: 'https://example.com' },
    id: undefined
  },
  Document {
    pageContent: 'Mitochondria are made out of lipids',
    metadata: { source: 'https://example.com' },
    id: undefined
  }
]
```

### 用于检索增强生成

有关如何使用此向量存储进行检索增强生成（RAG）的指南，请参阅以下部分：

* [使用 LangChain 构建 RAG 应用](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/rag.md)。
* [智能体 RAG](https://langchain-docs-zh.mintlify.site/langchain/oss/langgraph/agentic-rag.md)
* [检索文档](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/retrieval.md)

***

## API 参考

有关所有 `QdrantVectorStore` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-qdrant/QdrantVectorStore)。

***

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


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