
QDRANT矢量搜索引擎的Python客户库。
客户库库和SDK,用于QDRANT矢量搜索引擎。
库包含所有QDRANT API的类型定义,并允许同步和异步请求。
客户端允许直接呼叫所有QDRANT API方法。它还为经常需要操作提供了一些其他帮助方法,例如初始收集上传。
有关更多详细信息,请参见Quickstart!
Python客户端API文档可在python-client.qdrant.tech上找到
pip install qdrant-client

Python客户端允许您在无需运行QDRANT服务器的情况下在本地模式下运行相同的代码。
只需这样的初始化客户端:
from qdrant_client import QdrantClient
client = QdrantClient ( ":memory:" )
# or
client = QdrantClient ( path = "path/to/db" ) # Persists changes to disk本地模式可用于开发,原型制作和测试。
pip install qdrant-client[fastembed]
Fastembed是用于在CPU上创建快速向量嵌入的库。它基于ONNX运行时,并允许使用类似GPU的性能对CPU进行推断。
QDRANT客户端可以使用快速培训来创建嵌入并将其上传到QDRANT。这允许简化API并使其更直观。
from qdrant_client import QdrantClient
# Initialize the client
client = QdrantClient ( ":memory:" ) # or QdrantClient(path="path/to/db")
# Prepare your documents, metadata, and IDs
docs = [ "Qdrant has Langchain integrations" , "Qdrant also has Llama Index integrations" ]
metadata = [
{ "source" : "Langchain-docs" },
{ "source" : "Linkedin-docs" },
]
ids = [ 42 , 2 ]
# Use the new add method
client . 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 )快速培训还可以利用GPU来更快地嵌入。要启用GPU支持,请安装
pip install ' qdrant-client[fastembed-gpu] ' from qdrant_client import QdrantClient
# Initialize the client
client = QdrantClient ( ":memory:" ) # or QdrantClient(path="path/to/db")
client . set_model ( client . DEFAULT_EMBEDDING_MODEL , providers = [ "CUDAExecutionProvider" , "CPUExecutionProvider" ])注意:
fastembed-gpu和fastembed是相互排斥的。您只能安装其中之一。如果您以前安装了
fastembed安装,则可能需要从新鲜的环境开始安装fastembed-gpu。
要连接到QDRANT服务器,只需指定主机和端口:
from qdrant_client import QdrantClient
client = QdrantClient ( host = "localhost" , port = 6333 )
# or
client = QdrantClient ( url = "http://localhost:6333" )您可以使用Docker本地运行QDrant Server:
docker run -p 6333:6333 qdrant/qdrant:latest查看QDRANT存储库中的更多启动选项。
您可以注册并使用Qdrant Cloud来获得1GB RAM的免费层帐户。
拥有群集和API键后,您可以这样连接到它:
from qdrant_client import QdrantClient
qdrant_client = QdrantClient (
url = "https://xxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxx.us-east.aws.cloud.qdrant.io:6333" ,
api_key = "<your-api-key>" ,
)创建一个新集合
from qdrant_client . models import Distance , VectorParams
client . create_collection (
collection_name = "my_collection" ,
vectors_config = VectorParams ( size = 100 , distance = Distance . COSINE ),
)将向量插入集合
import numpy as np
from qdrant_client . models import PointStruct
vectors = np . random . rand ( 100 , 100 )
# NOTE: consider splitting the data into chunks to avoid hitting the server's payload size limit
# or use `upload_collection` or `upload_points` methods which handle this for you
# WARNING: uploading points one-by-one is not recommended due to requests overhead
client . upsert (
collection_name = "my_collection" ,
points = [
PointStruct (
id = idx ,
vector = vector . tolist (),
payload = { "color" : "red" , "rand_number" : idx % 10 }
)
for idx , vector in enumerate ( vectors )
]
)搜索类似的向量
query_vector = np . random . rand ( 100 )
hits = client . search (
collection_name = "my_collection" ,
query_vector = query_vector ,
limit = 5 # Return 5 closest points
)搜索具有过滤条件的类似向量
from qdrant_client . models import Filter , FieldCondition , Range
hits = client . search (
collection_name = "my_collection" ,
query_vector = query_vector ,
query_filter = Filter (
must = [ # These conditions are required for search results
FieldCondition (
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
)在我们的文档中查看更多示例!
要使用GRPC上传(通常更快)(通常更快)的集合,请使用以下初始化:
from qdrant_client import QdrantClient
client = QdrantClient ( host = "localhost" , grpc_port = 6334 , prefer_grpc = True )从1.6.1版本开始,所有Python客户端方法均在异步版本中提供。
要使用它,只需导入AsyncQdrantClient而不是QdrantClient :
from qdrant_client import AsyncQdrantClient , models
import numpy as np
import asyncio
async def main ():
# Your async code using QdrantClient might be put here
client = AsyncQdrantClient ( url = "http://localhost:6333" )
await client . create_collection (
collection_name = "my_collection" ,
vectors_config = models . VectorParams ( size = 10 , distance = models . Distance . COSINE ),
)
await client . upsert (
collection_name = "my_collection" ,
points = [
models . PointStruct (
id = i ,
vector = np . random . rand ( 10 ). tolist (),
)
for i in range ( 100 )
],
)
res = await client . search (
collection_name = "my_collection" ,
query_vector = np . random . rand ( 10 ). tolist (), # type: ignore
limit = 10 ,
)
print ( res )
asyncio . run ( main ())在异步模式下支持GRPC和REST API。可以在此处找到更多示例。
该项目使用git挂钩运行代码格式。
安装使用PIP3安装pre-commit的预pip3 install pre-commit ,并设置带有pre-commit install的挂钩。
预先承诺需要python> = 3.8