Welcome to the fourth lesson in our module on long-term memory. In our previous lessons, we established a strong conceptual foundation. You learned how text is transformed into numerical embeddings and explored the theory behind vector similarity search, including the distance metrics that measure semantic closeness and the crucial difference between exact and approximate search methods.
Today, we transition from theory to practice. Our objective is to set up a vector index on a managed service (Qdrant) and perform CRUD operations. You will create your own cloud-hosted vector database, connect to it using Python, and learn the fundamental commands for creating, reading, updating, and deleting vector data. This is the hands-on skill set required to build the persistent memory store for our autonomous agents.
The Anatomy of a Vector Database: Qdrant
Before we begin, let's establish a clear mental model of the system we're about to build. A vector database like Qdrant is a specialized system optimized for storing and searching high-dimensional vectors.
{"type":"image","url":"https://raw.githubusercontent.com/ramonpzg/mlops-sydney-2023/main/images/qdrant_overview_high_level.png","caption":"This diagram shows the high-level architecture of Qdrant. Programmers interact with Qdrant via clients (like the Python one we'll use). Qdrant stores data in 'Collections,' which contain 'Points.' Each point consists of a vector and an associated 'Payload' (metadata). The vectors themselves are typically generated by a deep learning model from raw data.","isV2":true,"blockId":"560474ba-2cab-4c3f-be4f-790a8614f65e","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
Let's define the key terms you'll be working with:
- Cluster: This is your dedicated, managed instance of the Qdrant service running in the cloud.
- Collection: A named set of points, analogous to a table in a traditional SQL database. A collection is configured with specific parameters, most importantly the size (dimensionality) of the vectors it will store and the distance metric (e.g.,
Cosine,Euclidean) it will use for similarity calculations. This directly applies the concepts from our last lesson. - Point: The fundamental data record, analogous to a row in a SQL table. Each point is composed of:
- ID: A unique identifier for the point.
- Vector: The numerical embedding representing the data's semantic meaning.
- Payload: An optional JSON object containing metadata. For an e-commerce application, this could be
{ "product_name": "Leather Wallet", "price": 79.99, "in_stock": true }. The payload is critical for filtering search results and providing context back to the agent.
The following video segment provides a clear explanation of these core concepts.
{"type":"video","title":"Setting Up Your Qdrant Vector Database","learning_duration":72,"video_id":"mHrwS6ZoNKc","par_intro":"This video explains the concepts of collections and points within Qdrant, providing a solid conceptual bridge from relational databases to vector databases.","par_directions":"Please watch the segment from <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"e10233c0\" data-range-start=\"189\" data-range-end=\"261\">the explanation of collections</span>, focusing on how a point is defined by its vector and payload, and the importance of consistent dimensionality and a chosen similarity metric.","video_duration":381,"isV2":true,"blockId":"39712589-20cb-408e-802b-e729e3e28d29","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
{
"type": "exercise",
"id": "b1c31c05-4b3c-4f7a-a069-65faf7efe8ff"
}
Step 1: Setting Up a Qdrant Cloud Cluster
For this course, we will use Qdrant Cloud, a managed service that handles the infrastructure for us. This allows us to focus on the application logic rather than server maintenance. Your first task is to create a free-tier cluster.
The following video provides a complete walkthrough of the signup and cluster creation process.
{"type":"video","title":"Setting Up Your Qdrant Vector Database","learning_duration":381,"video_id":"mHrwS6ZoNKc","par_intro":"This video guides you through the entire process of creating a Qdrant Cloud account and setting up your first cluster.","par_directions":"Watch from the beginning to <ts start=\"00:01:27\">the end of the setup process</ts>. Follow the steps to sign up, create a new cluster, and, most importantly, **copy your Cluster URL and create and copy an API Key**.","video_duration":381,"isV2":true,"blockId":"26ce3a0a-45ec-497e-b85a-fefea8b4863e","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
As you follow the video, you can refer to the official Qdrant documentation for a textual guide.
{"type":"reading","par_intro":"These are the official instructions for creating a cluster on Qdrant Cloud.","par_directions":"Follow the steps under the heading <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"edfbe504\" data-range-start=\"Create your cluster\" data-range-end=\"on the cluster page.\">Create your cluster</span>. You will sign up, create a free cluster, and retrieve your API key. Store the **Cluster URL** and **API Key** securely; you will need them in the next step.","learning_duration":"5 minutes","url":"https://qdrant.tech/course/essentials/day-0/qdrant-cloud/","title":"Qdrant Setup","isV2":true,"blockId":"4535f583-7648-4ef7-a98f-1f81602ca57c","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
{"type":"image","url":"https://qdrant.tech/docs/gettingstarted/gui-quickstart/create-cluster.png","caption":"The Qdrant Cloud dashboard, where you can create a new free cluster. You'll need to provide a name and select a cloud provider and region.","isV2":true,"blockId":"2ca502bf-f846-4c68-a354-d168a68307cb","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
Step 2: Connecting and Managing Collections with Python
With your cluster running, you can now interact with it programmatically. We'll use the official qdrant-client library for Python.
First, ensure you have the necessary libraries installed. It's also a best practice to manage your credentials using environment variables rather than hardcoding them in your script.
# Install the Qdrant client
pip install qdrant-client
# It's also recommended to install python-dotenv to manage environment variables
pip install python-dotenv
Create a file named .env in your project's root directory and add your credentials:
QDRANT_URL="YOUR_CLUSTER_URL_HERE"
QDRANT_API_KEY="YOUR_API_KEY_HERE"
Now, you can use the following Python code to connect to your cluster and manage collections.
import os
from dotenv import load_dotenv
from qdrant_client import QdrantClient, models
# Load environment variables from .env file
load_dotenv()
# Initialize the Qdrant client
client = QdrantClient(
url=os.getenv("QDRANT_URL"),
api_key=os.getenv("QDRANT_API_KEY"),
)
# Define the collection name
collection_name = "my_first_collection"
# --- Collection Management ---
# 1. Create a new collection
# We must specify the vector size and distance metric.
# OpenAI's text-embedding-ada-002 model has a dimensionality of 1536.
# Based on our last lesson, Cosine similarity is the best choice for text embeddings.
try:
client.recreate_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(size=1536, distance=models.Distance.COSINE),
)
print(f"Collection '{collection_name}' created successfully.")
except Exception as e:
print(f"Could not create collection: {e}")
# 2. List all collections
collections = client.get_collections()
print("Existing collections:")
for collection in collections.collections:
print(f"- {collection.name}")
# 3. Get information about a specific collection
collection_info = client.get_collection(collection_name=collection_name)
print(f"\nInfo for '{collection_name}':")
print(f" - Vector count: {collection_info.vectors_count}")
print(f" - Indexed vector count: {collection_info.indexed_vectors_count}")
# 4. Delete the collection
# client.delete_collection(collection_name=collection_name)
# print(f"\nCollection '{collection_name}' deleted.")
{
"type": "exercise",
"id": "808af3d2-013b-420c-a5ac-14613015ce0c"
}
Run this script. You should see it successfully create a collection and then print its details. You can also log in to your Qdrant Cloud UI to see the collection appear in the dashboard.
Step 3: CRUD Operations on Points
Now for the core of today's lesson: managing the data points within a collection. The acronym CRUD stands for Create, Read, Update, and Delete.
The following resource provides a concise, all-in-one Python script demonstrating these operations. It uses a different embedding model (all-MiniLM-L6-v2) for simplicity, but the principles are identical.
{"type":"reading","par_intro":"This article section provides a clear, self-contained Python script that demonstrates the full lifecycle of a vector point: creation, searching, updating, and deletion.","par_directions":"Focus on the code block under the heading <tf start=\"Qdrant CRUD Operations\" end=\"Match: {result.payload['text']} (Score: {result.score:.4f})\")\">Qdrant CRUD Operations</tf>. Read through the code and its comments, which are organized into five parts:\n1. **CREATE**: Shows how to use `upsert` to insert new points. Notice how each `PointStruct` contains an `id`, `vector`, and `payload`.\n2. **READ**: Demonstrates the `search` method to find similar vectors.\n3. **UPDATE**: Illustrates that `upsert` is also used to modify an existing vector by providing the same `id` with a new vector or payload.\n4. **DELETE**: Shows how to remove a point using its `id`.\n5. **VERIFY**: Confirms the deletion by performing the search again.\n\nTry running this code yourself. You'll need to `pip install sentence-transformers` to run the example as-is.","learning_duration":"15 minutes","url":"https://blog.stackademic.com/10-essential-insights-on-qdrant-for-building-superior-llm-based-applications-cc02bb9eb359","title":"10 Essential Insights on Qdrant for Building Superior LLM- ...","isV2":true,"blockId":"ca0baf0b-fb86-47fd-b0e0-2cc4748bf1f3","lessonId":"ecf62f7e-61f1-4d67-bb09-9cc4b4e2d6e4"}
Let's summarize the key methods from that example:
-
Create / Update: Qdrant uses a single, powerful method:
client.upsert().- If you provide a point with an
idthat doesn't exist in the collection, a new point is created. - If you provide an
idthat already exists, the existing point is updated with the new vector and/or payload. This is highly efficient for dynamic datasets, like updating product stock levels in an e-commerce catalog.
- If you provide a point with an
-
Read: There are two primary ways to read data:
client.retrieve(collection_name, ids=[...]): Fetches one or more points directly using their known IDs. This is useful when you need the exact data for a specific item.client.search(collection_name, query_vector=...): Performs a similarity search. This is the main operation for finding semantically relevant information based on a query embedding. You can also add aquery_filterto constrain the search to points whose payload matches certain criteria (e.g.,category: "electronics").
-
Delete: The
client.delete(collection_name, points_selector=...)method removes points from a collection. You can select which points to delete by providing a list of their IDs.
By mastering these four operations, you have complete control over the long-term memory store for an agent.
{
"type": "exercise",
"id": "825dea36-7d17-49b9-bd30-de92a3883b11"
}
Conclusion
In this lesson, you have taken a significant step from abstract concepts to concrete implementation. You now have a working vector database in the cloud and the skills to manage its contents.
Here are the key takeaways:
- Vector Databases like Qdrant organize data into Collections of Points. Each point has a unique ID, a vector, and a payload for metadata.
- Qdrant Cloud provides a managed service, allowing you to quickly deploy a production-ready vector database without managing infrastructure.
- The
qdrant-clientfor Python is the primary tool for programmatic interaction, allowing you to manage collections and perform data operations. - The core CRUD operations are handled by a few key methods:
- Create/Update:
upsert() - Read:
retrieve()(by ID) andsearch()(by similarity) - Delete:
delete()
- Create/Update:
You have successfully built the "bookshelf" for your agent's library. In our next lesson, we will put it to use by implementing a Retrieval-Augmented Generation (RAG) pattern. You will integrate the Qdrant vector store you just built as a retrieval tool in an agent, enabling it to answer questions by dynamically pulling in relevant knowledge from its long-term memory.
Can't find a good explanation? Sign up and we'll make it for you