From 3154f521d9a0820b0d178bacaf35d48fe24da1ab Mon Sep 17 00:00:00 2001 From: Emanuel Ferreira <contatoferreirads@gmail.com> Date: Thu, 25 Jan 2024 17:58:37 -0300 Subject: [PATCH] chore: add qdrant readme (#444) --- .changeset/funny-mice-fail.md | 6 ++ apps/docs/docs/modules/documents_and_nodes.md | 2 +- apps/docs/docs/modules/llm.md | 2 +- .../docs/modules/vectorStores/_category_.yml | 2 + apps/docs/docs/modules/vectorStores/qdrant.md | 88 +++++++++++++++++++ .../storage/vectorStore/QdrantVectorStore.ts | 8 +- 6 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 .changeset/funny-mice-fail.md create mode 100644 apps/docs/docs/modules/vectorStores/_category_.yml create mode 100644 apps/docs/docs/modules/vectorStores/qdrant.md diff --git a/.changeset/funny-mice-fail.md b/.changeset/funny-mice-fail.md new file mode 100644 index 000000000..6055262e9 --- /dev/null +++ b/.changeset/funny-mice-fail.md @@ -0,0 +1,6 @@ +--- +"llamaindex": minor +"docs": patch +--- + +chore: add qdrant readme diff --git a/apps/docs/docs/modules/documents_and_nodes.md b/apps/docs/docs/modules/documents_and_nodes.md index 443d1c983..d4a36ca6c 100644 --- a/apps/docs/docs/modules/documents_and_nodes.md +++ b/apps/docs/docs/modules/documents_and_nodes.md @@ -1,5 +1,5 @@ --- -sidebar_position: 0 +sidebar_position: 1 --- # Documents and Nodes diff --git a/apps/docs/docs/modules/llm.md b/apps/docs/docs/modules/llm.md index fe53292a4..6de5f4225 100644 --- a/apps/docs/docs/modules/llm.md +++ b/apps/docs/docs/modules/llm.md @@ -1,5 +1,5 @@ --- -sidebar_position: 0 +sidebar_position: 1 --- # LLM diff --git a/apps/docs/docs/modules/vectorStores/_category_.yml b/apps/docs/docs/modules/vectorStores/_category_.yml new file mode 100644 index 000000000..ac980e1f1 --- /dev/null +++ b/apps/docs/docs/modules/vectorStores/_category_.yml @@ -0,0 +1,2 @@ +label: "Vector Stores" +position: 0 diff --git a/apps/docs/docs/modules/vectorStores/qdrant.md b/apps/docs/docs/modules/vectorStores/qdrant.md new file mode 100644 index 000000000..6c86aaa6a --- /dev/null +++ b/apps/docs/docs/modules/vectorStores/qdrant.md @@ -0,0 +1,88 @@ +# Qdrant Vector Store + +To run this example, you need to have a Qdrant instance running. You can run it with Docker: + +```bash +docker pull qdrant/qdrant +docker run -p 6333:6333 qdrant/qdrant +``` + +## Importing the modules + +```ts +import fs from "node:fs/promises"; +import { Document, VectorStoreIndex, QdrantVectorStore } from "llamaindex"; +``` + +## Load the documents + +```ts +const path = "node_modules/llamaindex/examples/abramov.txt"; +const essay = await fs.readFile(path, "utf-8"); +``` + +## Setup Qdrant + +```ts +const vectorStore = new QdrantVectorStore({ + url: "http://localhost:6333", + port: 6333, +}); +``` + +## Setup the index + +```ts +const document = new Document({ text: essay, id_: path }); + +const index = await VectorStoreIndex.fromDocuments([document], { + vectorStore, +}); +``` + +## Query the index + +```ts +const queryEngine = index.asQueryEngine(); + +const response = await queryEngine.query({ + query: "What did the author do in college?", +}); + +// Output response +console.log(response.toString()); +``` + +## Full code + +```ts +import fs from "node:fs/promises"; +import { Document, VectorStoreIndex, QdrantVectorStore } from "llamaindex"; + +async function main() { + const path = "node_modules/llamaindex/examples/abramov.txt"; + const essay = await fs.readFile(path, "utf-8"); + + const vectorStore = new QdrantVectorStore({ + url: "http://localhost:6333", + port: 6333, + }); + + const document = new Document({ text: essay, id_: path }); + + const index = await VectorStoreIndex.fromDocuments([document], { + vectorStore, + }); + + const queryEngine = index.asQueryEngine(); + + const response = await queryEngine.query({ + query: "What did the author do in college?", + }); + + // Output response + console.log(response.toString()); +} + +main().catch(console.error); +``` diff --git a/packages/core/src/storage/vectorStore/QdrantVectorStore.ts b/packages/core/src/storage/vectorStore/QdrantVectorStore.ts index d5764c562..1e8afe1d6 100644 --- a/packages/core/src/storage/vectorStore/QdrantVectorStore.ts +++ b/packages/core/src/storage/vectorStore/QdrantVectorStore.ts @@ -54,11 +54,9 @@ export class QdrantVectorStore implements VectorStore { apiKey, batchSize, }: QdrantParams) { - if (!client && (!url || !apiKey)) { - if (!url || !apiKey || !collectionName) { - throw new Error( - "QdrantVectorStore requires url, apiKey and collectionName", - ); + if (!client && !url) { + if (!url || !collectionName) { + throw new Error("QdrantVectorStore requires url and collectionName"); } } -- GitLab