Package: qdrant-client
Python Client library for theQdrantvector search engine.Python Qdrant ClientClient library and SDK for theQdrantvector search engine. Python Client API Documentation is availablehere.Library contains type definitions for all Qdrant API and allows to make both Sync and Async requests.Client allows calls for allQdrant API methodsdirectly.
It also provides some additional helper methods for frequently required operations, e.g. initial collection uploading.SeeQuickStartfor more details!Installationpip install qdrant-clientFeaturesType hints for all API methodsLocal mode - use same API without running serverREST and gRPC supportMinimal dependenciesExtensive Test CoverageLocal modePython client allows you to run same code in local mode without running Qdrant server.Simply initialize client like this:fromqdrant_clientimportQdrantClientclient=QdrantClient(":memory:")# orclient=QdrantClient(path="path/to/db")# Persists changes to diskLocal mode is useful for development, prototyping and testing.You can use it to run tests in your CI/CD pipeline.Run it in Colab or Jupyter Notebook, no extra dependencies required. See anexampleWhen you need to scale, simply switch to server mode.Fast Embeddings + Simpler APIpip install qdrant-client[fastembed]FastEmbed is a library for creating fast vector embeddings on CPU. It is based on ONNX Runtime and allows to run inference on CPU with GPU-like performance.Qdrant Client can use FastEmbed to create embeddings and upload them to Qdrant. This allows to simplify API and make it more intuitive.fromqdrant_clientimportQdrantClient# Initialize the clientclient=QdrantClient(":memory:")# or QdrantClient(path="path/to/db")# Prepare your documents, metadata, and IDsdocs=["Qdrant has Langchain integrations","Qdrant also has Llama Index integrations"]metadata=[{"source":"Langchain-docs"},{"source":"Linkedin-docs"},]ids=[42,2]# Use the new add methodclient.add(collection_name="demo_collection",documents=docs,metadata=metadata,ids=ids)search_result=client.query(collection_name="demo_collection",query_text="This is a query document")print(search_result)Connect to Qdrant serverTo connect to Qdrant server, simply specify host and port:fromqdrant_clientimportQdrantClientclient=QdrantClient(host="localhost",port=6333)# orclient=QdrantClient(url="http://localhost:6333")You can run Qdrant server locally with docker:dockerrun-p6333:6333qdrant/qdrant:latestSee more launch options inQdrant repository.Connect to Qdrant cloudYou can register and useQdrant Cloudto get a free tier account with 1GB RAM.Once you have your cluster and API key, you can connect to it like this:fromqdrant_clientimportQdrantClientqdrant_client=QdrantClient(url="https://xxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxx.us-east.aws.cloud.qdrant.io:6333",api_key="<your-api-key>",)ExamplesCreate a new collectionfromqdrant_client.modelsimportDistance,VectorParamsclient.recreate_collection(collection_name="my_collection",vectors_config=VectorParams(size=100,distance=Distance.COSINE),)Insert vectors into a collectionimportnumpyasnpfromqdrant_client.modelsimportPointStructvectors=np.random.rand(100,100)client.upsert(collection_name="my_collection",points=[PointStruct(id=idx,vector=vector.tolist(),payload={"color":"red","rand_number":idx%10})foridx,vectorinenumerate(vectors)])Search for similar vectorsquery_vector=np.random.rand(100)hits=client.search(collection_name="my_collection",query_vector=query_vector,limit=5# Return 5 closest points)Search for similar vectors with filtering conditionfromqdrant_client.modelsimportFilter,FieldCondition,Rangehits=client.search(collection_name="my_collection",query_vector=query_vector,query_filter=Filter(must=[# These conditions are required for search resultsFieldCondition(key='rand_number',# Condition based on values of `rand_number` field.range=Range(gte=3# Select only those results where `rand_number` >= 3))]),limit=5# Return 5 closest points)See more examples in ourDocumentation!gRPCTo enable (typically, much faster) collection uploading with gRPC, use the following initialization:fromqdrant_clientimportQdrantClientclient=QdrantClient(host="localhost",grpc_port=6334,prefer_grpc=True)Async clientStarting from version 1.6.1, all python client methods are available in async version.To use it, just importAsyncQdrantClientinstead ofQdrantClient:fromqdrant_clientimportAsyncQdrantClient,modelsimportnumpyasnpimportasyncioasyncdefmain():# Your async code using QdrantClient might be put hereclient=AsyncQdrantClient(url="http://localhost:6333")awaitclient.create_collection(collection_name="my_collection",vectors_config=models.VectorParams(size=10,distance=models.Distance.COSINE),)awaitclient.upsert(collection_name="my_collection",points=[models.PointStruct(id=i,vector=np.random.rand(10).tolist(),)foriinrange(100)],)res=awaitclient.search(collection_name="my_collection",query_vector=np.random.rand(10).tolist(),# type: ignorelimit=10,)print(res)asyncio.run(main())Both, gRPC and REST API are supported in async mode.
More examples can be foundhere.DevelopmentThis project uses git hooks to run code formatters.Installpre-commitwithpip3 install pre-commitand set up hooks withpre-commit install.pre-commit requires python>=3.8