Skip to content

Get started with Vector Database

View as Markdown

Create a Qdrant vector database, connect to it from Python, and run your first similarity search.

You need a TIR project in Delhi or Chennai, enough credit to launch the nodes, and Python 3.8 or later for the client. An instance in the project can run it.

  1. In TIR, open your project. In the side menu, under Storage, select Vector Database.
  2. Select Create Database. For a project’s first database, the button reads Launch Database.
  3. Keep the Cluster Name TIR fills in, or type your own.
  4. In Default Collection, keep Cosine for Distance, and set Vector Dimension to the size of the vectors you will store. Keep Shard Number at 1 and Replication Factor at 3.
  5. Under Select Plan, keep E2E Cluster and choose a plan. Keep Node Count at 3.
  6. Select Launch.

The database appears in the list as Deploying and becomes Running after a few minutes. See Create a vector database for what each choice means.

When the database is Running, select its row and open Overview. Copy the Endpoint URL, then show and copy the API Key.

Install the Python client:

pip install qdrant-client

The script connects, creates a second collection with the same settings as the default one (Cosine distance, one shard, replication factor 3), and adds six points. It then searches for the three points nearest to a query vector. Replace <your-endpoint-url> with the Endpoint URL you copied, leaving off the https:// at the start, and <your-api-key> with the API Key.

from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams, PointStruct
client = QdrantClient(host="<your-endpoint-url>", port=6333, https=True, api_key="<your-api-key>")
client.create_collection(
collection_name="my_first_collection",
vectors_config=VectorParams(size=4, distance=Distance.COSINE),
shard_number=1,
replication_factor=3,
)
points = [
PointStruct(id=1, vector=[0.05, 0.61, 0.76, 0.74], payload={"city": "Berlin"}),
PointStruct(id=2, vector=[0.19, 0.81, 0.75, 0.11], payload={"city": "London"}),
PointStruct(id=3, vector=[0.36, 0.55, 0.47, 0.94], payload={"city": "Moscow"}),
PointStruct(id=4, vector=[0.18, 0.01, 0.85, 0.80], payload={"city": "New York"}),
PointStruct(id=5, vector=[0.24, 0.18, 0.22, 0.44], payload={"city": "Beijing"}),
PointStruct(id=6, vector=[0.35, 0.08, 0.11, 0.44], payload={"city": "Chennai"}),
]
client.upsert(collection_name="my_first_collection", points=points, wait=True)
results = client.search(
collection_name="my_first_collection",
query_vector=[0.2, 0.1, 0.9, 0.7],
limit=3,
)
for point in results:
print(f"ID: {point.id}, City: {point.payload['city']}, Score: {point.score}")

The script prints three lines, one for each of the nearest points, with their scores. To use gRPC instead of REST, connect on port 6334 with the gRPC option turned on:

client = QdrantClient(
host="<your-endpoint-url>", port=6334, https=True, api_key="<your-api-key>", prefer_grpc=True
)

For help with any message, see Troubleshooting Vector Database.