Author(s): Harpreet Sahota Originally published on Towards AI. Photo by Andrew Ruiz on Unsplash If you randomly ended up on this blog, let me give you a bit of context. I’m writing a book on Retrieval Augmented Generation (RAG) for Wiley Publishing, and vector databases are an inescapable part of building a performant RAG system. I selected Qdrant as the vector database for my book and this series. Over the next several blogs, I’ll teach you everything you need to know to start working with the Qdrant vector database. In the first part of this series, you were introduced to vector databases and why they’re essential for building context-aware applications like RAG. Now that you have a solid understanding of vector databases and Qdrant, it’s time to roll up your sleeves and get your hands dirty. In this post, you’ll set up your development environment and start with Qdrant, the vector database you’ll use throughout this series. You’ll sign up for a Qdrant cloud account, install the necessary libraries, set up our environment variables, and instantiate a cluster — all the necessary steps to start building something. By the end of this post, you’ll have a fully functional Qdrant setup and be ready to experiment with vector embeddings and similarity searches. Prepare Your Cloud Environment The easiest way to get started with Qdrant is by using their managed cloud service, Qdrant Cloud. Of course, you can use Qdrant locally if you’d like. However, it will require you to set up Docker on your machine and be familiar with Docker containers. Check out the documentation to learn how to get set up locally. For this series and in my book, I will work strictly in the cloud. To sign up for Qdrant Cloud: 1) Head over to the Qdrant Cloud website. 2) Click the “Cloud” button in the top right corner. 3) Fill in your details and create your account. You don’t have to set up a paid account, as Qdrant offers a generous free tier perfect for development and testing. And, if you build a product using billions of vectors, Qdrant will scale smoothly. Go and sign up for a cloud account. Source: Author You’ll need to create your cluster and get your API key. To set up a cluster: • Locate the sidebar on the left-hand screen of the console console. • Click on the “Clusters” menu item. • Look for the “Create” button on the top right of the screen. • Type in “Practical_Retrieval_Augmented_Generation” as your cluster name, then press “Create Cluster.” Once the cluster is created, you’ll see a red button that says, “Get API Key.” Press that button and copy your API key. Be sure to keep it somewhere safe. You’ll also need your Cluster URL. You can get that by: Navigating to the Clusters item in the sidebar Clicking the right-facing carrot on your cluster name You can find it under “Cluster URL.” Copy that and keep it safe. I recommend setting these as environment variables. You can do that by opening up your terminal and running the following (assuming you’re on Linux or MacOS): export QDRANT_API_KEY=<your-api-key>export QDRANT_URL=<your-qdrant-cloud-url> I recommend keeping these in a .env file so you can easily load them into your notebooks and scripts. You can do that by running the following in your terminal: echo "QDRANT_API_KEY=$QDRANT_API_KEY" >> .env && echo "QDRANT_URL=$QDRANT_URL" >> .env Now, set up your development environment. Prepare Your Development Environment Create a Virtual Environment It’s a best practice to create a virtual environment before installing libraries and working on a project. So, get that out of the way, then start installing some packages: conda create -n p_rag python==3.10 Once that’s done, activate your environment: conda activate p_rag Install Dependencies To interact with Qdrant using the Python sdk, you’ll need to install the Qdrant client library. In addition, at least for right now and at a bare minimum, you’ll also need to download: python-dotenv OpenAI sentence-transformers transformers datasets There is no need for LangChain or LlamaIndex—yet. We won’t use them for the first several blogs in this series. This builds a deeper intuition for how data goes from its “natural” form to vectors and into a vector database. Any other requirements will be installed as needed. Now, open your terminal and run the following commands. Be sure to use the same versions as I do. pip install python-dotenv==1.0.1 qdrant-client==1.9.0 openai==1.23.6 transformers==4.40.1 sentence-transformers==2.7.0 datasets==2.19.0 Now, open up a Jupyter Notebook. It’s time to connect to your cluster programmatically and create your first collection. Start with the immediately necessary imports: import osfrom dotenv import load_dotenvfrom qdrant_client import QdrantClientload_dotenv('./.env') Now, instantiate the client. client = QdrantClient( url=os.getenv('QDRANT_URL'), api_key=os.getenv('QDRANT_API_KEY')) Run the following code, and notice that our list of collections is empty—as it should be—because you haven’t created one yet. client.get_collections() WTF Is a Collection? Before I have you create a collection, it’s gonna be useful for me to tell you what they are. A collection in Qdrant holds points. Points are the central entity in Qdrant, and they’re defined by: Vector: This represents the data (like an image, chunk of text, audio segment, video, etc.) as an array of floating-point numbers. Each vector has a unique identifier associated with it. Payload: Additional information about the data (basically metadata). This is just a JSON object. Each point within a collection must have a vector of the same dimensionality and be compared using a single similarity metric. This allows you to search points in a collection based on vector similarity. Before creating your collection, there are some design choices you need to make: Data type: What kind of data will you store? (For example, images, text, or both.) Vector size and distance metric: Vector size will depend on your chosen embedding model. The distance metric can be a dot product, cosine similarity, Euclidean distance, or Manhattan distance. What to put in the Payload: Decide what additional metadata you want to store with each vector. Each collection has its own set of parameters that […]
↧