LlamaIndex Integration with Qdrant
Llama Index acts as an interface between your external data and Large Language Models. So you can bring your private data and augment LLMs with it. LlamaIndex simplifies data ingestion and indexing, integrating Qdrant as a vector index.
Pre-requisites
- A running instance of Qdrant Vector-Database. See Creating a new Qdrant on TIR.
- Python 3.8 or higher. You may use TIR Nodes for easy execution.
Installing LlamaIndex and Qdrant-Client
-
To install llama-index:
pip install llama-index llama-index-vector-stores-qdrant
-
For installing Qdrant-Client:
pip install qdrant-client
Importing Libraries
-
First, we need to import some libraries provided by LlamaIndex to help us in converting text into chunks and then embeddings and putting them into the Qdrant Vector Database.
import qdrant_client
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.embeddings.fastembed import FastEmbedEmbedding
from llama_index.core import Settings
Specifying Embedding Model
-
Then, we need to specify in the Llama Index setting which model we will be using for generating our vector embeddings from text. We will be using the
BAAI/bge-base-en-v1.5
model for this example.Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5")
Settings.llm = None
Reading the File
-
Then, we need to read the contents of the file we need to embed. We can read multiple files stored in the directory by using the SimpleDirectoryReader class of LlamaIndex.
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
Embedding and Inserting into Qdrant
-
Now, we need to convert these chunks of text into vector embeddings and insert them into Qdrant. For converting chunks into vector embeddings, we will be using a HuggingFace Model as it is compatible with the LangChain library. You also need to specify your API Key and URL for accessing the Qdrant cluster in the code below.
documents = SimpleDirectoryReader("<<FILE_NAME>>").load_data()
Initializing the Qdrant Client
-
Llama Index requires providing an instance of
QdrantClient
, so it can interact with the Qdrant server.Note:
You can get your Qdrant URL and API Key from the Overview tab in Vector Database on TIR. See the Qdrant Details subsection for more information.
host = "<<YOUR_QDRANT_ENDPOINT_URL>>"
# HTTP Port = 6333
# gRPC Port = 6334
port = "<<YOUR_PORT>>"
api_key = "<<YOUR_QDRANT_API_KEY>>"
client = qdrant_client.QdrantClient(host=host, port=port, api_key=api_key)
Inserting into the Vector Database
-
Once everything is initialized, we can put the chunks of our data into our vector database. The process of chunking, embedding, and inserting into Qdrant is done by LlamaIndex itself.
vector_store = QdrantVectorStore(client=client, collection_name="<<YOUR_COLLECTION_NAME>>")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
)
Now, you have all your vector embeddings chunked and inserted into the Qdrant vector database.
Similarity Search
The last step is to query the created index by providing a string as a query input. It will query the Qdrant to return the output based on Similarity Search.
query_engine = index.as_query_engine()
query_str = "<<QUERY>>" # a query string
response = query_engine.query(query_str)
What's Next?
This was a quick start guide to get you started with the Qdrant Integration with LangChain.
-
You can explore more features of the LlamaIndex integration in the Qdrant LlamaIndex Documentation.
-
For Integration with LangChain tutorial, head over to Qdrant LangChain Integration.