Skip to main content

Qdrant

Qdrant 是一个向量相似度搜索引擎。它提供了一个方便的API来存储、搜索和管理带有附加有效负载的点 - 向量。

兼容性

仅适用于Node.js。

配置

  1. 根据 Qdrant 设置说明 在您的计算机上使用Docker运行Qdrant实例。

  2. 安装Qdrant Node.js SDK。


    npm install -S @qdrant/js-client-rest

  1. 运行代码前为Qdrant设置Env变量。

    3.1 OpenAI


export OPENAI_API_KEY=YOUR_OPENAI_API_KEY_HERE

export QDRANT_URL=YOUR_QDRANT_URL_HERE # for example http://localhost:6333

3.2 Azure OpenAI


export AZURE_OPENAI_API_KEY=YOUR_AZURE_OPENAI_API_KEY_HERE

export AZURE_OPENAI_API_INSTANCE_NAME=YOUR_AZURE_OPENAI_INSTANCE_NAME_HERE

export AZURE_OPENAI_API_DEPLOYMENT_NAME=YOUR_AZURE_OPENAI_DEPLOYMENT_NAME_HERE

export AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME=YOUR_AZURE_OPENAI_COMPLETIONS_DEPLOYMENT_NAME_HERE

export AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME=YOUR_AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME_HERE

export AZURE_OPENAI_API_VERSION=YOUR_AZURE_OPENAI_API_VERSION_HERE

export QDRANT_URL=YOUR_QDRANT_URL_HERE # for example http://localhost:6333

用法

从文本中创建新的索引

import { QdrantVectorStore } from "langchain/vectorstores/qdrant";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
// text sample from Godel, Escher, Bach
const vectorStore = await QdrantVectorStore.fromTexts(
[
`Tortoise: Labyrinth? Labyrinth? Could it Are we in the notorious Little
Harmonic Labyrinth of the dreaded Majotaur?`,
`Achilles: Yiikes! What is that?`,
`Tortoise: They say-although I person never believed it myself-that an I
Majotaur has created a tiny labyrinth sits in a pit in the middle of
it, waiting innocent victims to get lost in its fears complexity.
Then, when they wander and dazed into the center, he laughs and
laughs at them-so hard, that he laughs them to death!`,
`Achilles: Oh, no!`,
`Tortoise: But it's only a myth. Courage, Achilles.`,
],
[{ id: 2 }, { id: 1 }, { id: 3 }, { id: 4 }, { id: 5 }],
new OpenAIEmbeddings(),
{
url: process.env.QDRANT_URL,
collectionName: "goldel_escher_bach",
}
);

const response = await vectorStore.similaritySearch("scared", 2);

console.log(response);

/*
[
Document { pageContent: 'Achilles: Oh, no!', metadata: {} },
Document {
pageContent: 'Achilles: Yiikes! What is that?',
metadata: { id: 1 }
}
]
*/

从文档中创建新的索引

import { QdrantVectorStore } from "langchain/vectorstores/qdrant";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { TextLoader } from "langchain/document_loaders/fs/text";

// Create docs with a loader
const loader = new TextLoader("src/document_loaders/example_data/example.txt");
const docs = await loader.load();

const vectorStore = await QdrantVectorStore.fromDocuments(
docs,
new OpenAIEmbeddings(),
{
url: process.env.QDRANT_URL,
collectionName: "a_test_collection",
}
);

// Search for the most similar document
const response = await vectorStore.similaritySearch("hello", 1);

console.log(response);
/*
[
Document {
pageContent: 'Foo\nBar\nBaz\n\n',
metadata: { source: 'src/document_loaders/example_data/example.txt' }
}
]
*/

从现有集合查询文档

import { QdrantVectorStore } from "langchain/vectorstores/qdrant";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

const vectorStore = await QdrantVectorStore.fromExistingCollection(
new OpenAIEmbeddings(),
{
url: process.env.QDRANT_URL,
collectionName: "goldel_escher_bach",
}
);

const response = await vectorStore.similaritySearch("scared", 2);

console.log(response);

/*
[
Document { pageContent: 'Achilles: Oh, no!', metadata: {} },
Document {
pageContent: 'Achilles: Yiikes! What is that?',
metadata: { id: 1 }
}
]
*/