Qdrant
Qdrant is a vector similarity search engine. It provides a production-ready service with a convenient API to store, search, and manage points - vectors with an additional payload.
Setup
Run a Qdrant instance with Docker on your computer by following the Qdrant setup instructions.
Install the Qdrant Node.js SDK.
- npm
- Yarn
- pnpm
npm install -S @langchain/qdrant
yarn add @langchain/qdrant
pnpm add @langchain/qdrant
Setup Env variables for Qdrant before running the code
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY_HERE
export QDRANT_URL=YOUR_QDRANT_URL_HERE # for example http://localhost:6333
Usage
Create a new index from texts
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
import { QdrantVectorStore } from "@langchain/qdrant";
import { OpenAIEmbeddings } from "@langchain/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 }
}
]
*/
API Reference:
- QdrantVectorStore from
@langchain/qdrant
- OpenAIEmbeddings from
@langchain/openai
Create a new index from docs
import { QdrantVectorStore } from "@langchain/qdrant";
import { OpenAIEmbeddings } from "@langchain/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' }
}
]
*/
API Reference:
- QdrantVectorStore from
@langchain/qdrant
- OpenAIEmbeddings from
@langchain/openai
- TextLoader from
langchain/document_loaders/fs/text
Query docs from existing collection
import { QdrantVectorStore } from "@langchain/qdrant";
import { OpenAIEmbeddings } from "@langchain/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 }
}
]
*/
API Reference:
- QdrantVectorStore from
@langchain/qdrant
- OpenAIEmbeddings from
@langchain/openai
Related
- Vector store conceptual guide
- Vector store how-to guides