|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +import os |
| 14 | +from functools import cached_property |
| 15 | +from pathlib import Path |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +from testcontainers.core.config import TIMEOUT |
| 19 | +from testcontainers.core.generic import DbContainer |
| 20 | +from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs |
| 21 | + |
| 22 | + |
| 23 | +class QdrantContainer(DbContainer): |
| 24 | + """ |
| 25 | + Qdrant vector database container. |
| 26 | +
|
| 27 | + Example: |
| 28 | + .. doctest:: |
| 29 | +
|
| 30 | + >>> from testcontainers.qdrant import QdrantContainer |
| 31 | +
|
| 32 | + >>> with QdrantContainer() as qdrant: |
| 33 | + ... client = qdrant.get_client() |
| 34 | + ... client.get_collections() |
| 35 | + CollectionsResponse(collections=[]) |
| 36 | + """ |
| 37 | + |
| 38 | + QDRANT_CONFIG_FILE_PATH = "/qdrant/config/config.yaml" |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + image: str = "qdrant/qdrant:v1.8.3", |
| 43 | + rest_port: int = 6333, |
| 44 | + grpc_port: int = 6334, |
| 45 | + api_key: Optional[str] = None, |
| 46 | + config_file_path: Optional[Path] = None, |
| 47 | + **kwargs, |
| 48 | + ) -> None: |
| 49 | + super().__init__(image, **kwargs) |
| 50 | + self._rest_port = rest_port |
| 51 | + self._grpc_port = grpc_port |
| 52 | + self._api_key = api_key or os.getenv("QDRANT_CONTAINER_API_KEY") |
| 53 | + |
| 54 | + if config_file_path: |
| 55 | + self.with_volume_mapping(host=str(config_file_path), container=QdrantContainer.QDRANT_CONFIG_FILE_PATH) |
| 56 | + |
| 57 | + self.with_exposed_ports(self._rest_port, self._grpc_port) |
| 58 | + |
| 59 | + def _configure(self) -> None: |
| 60 | + self.with_env("QDRANT__SERVICE__API_KEY", self._api_key) |
| 61 | + |
| 62 | + @wait_container_is_ready() |
| 63 | + def _connect(self) -> None: |
| 64 | + wait_for_logs(self, ".*Actix runtime found; starting in Actix runtime.*", TIMEOUT) |
| 65 | + |
| 66 | + def get_client(self, **kwargs): |
| 67 | + """ |
| 68 | + Get a `qdrant_client.QdrantClient` instance associated with the container. |
| 69 | +
|
| 70 | + Args: |
| 71 | + **kwargs: Additional keyword arguments to be passed to the `qdrant_client.QdrantClient` constructor. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + QdrantClient: An instance of the `qdrant_client.QdrantClient` class. |
| 75 | +
|
| 76 | + """ |
| 77 | + |
| 78 | + try: |
| 79 | + from qdrant_client import QdrantClient |
| 80 | + except ImportError as e: |
| 81 | + raise ImportError("To use the `get_client` method, you must install the `qdrant_client` package.") from e |
| 82 | + return QdrantClient( |
| 83 | + host=self.get_container_host_ip(), |
| 84 | + port=self.get_exposed_port(self._rest_port), |
| 85 | + grpc_port=self.get_exposed_port(self._grpc_port), |
| 86 | + api_key=self._api_key, |
| 87 | + https=False, |
| 88 | + **kwargs, |
| 89 | + ) |
| 90 | + |
| 91 | + def get_async_client(self, **kwargs): |
| 92 | + """ |
| 93 | + Get a `qdrant_client.AsyncQdrantClient` instance associated with the container. |
| 94 | +
|
| 95 | + Args: |
| 96 | + **kwargs: Additional keyword arguments to be passed to the `qdrant_client.AsyncQdrantClient` constructor. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + QdrantClient: An instance of the `qdrant_client.AsyncQdrantClient` class. |
| 100 | +
|
| 101 | + """ |
| 102 | + |
| 103 | + try: |
| 104 | + from qdrant_client import AsyncQdrantClient |
| 105 | + except ImportError as e: |
| 106 | + raise ImportError( |
| 107 | + "To use the `get_async_client` method, you must install the `qdrant_client` package." |
| 108 | + ) from e |
| 109 | + return AsyncQdrantClient( |
| 110 | + host=self.get_container_host_ip(), |
| 111 | + port=self.get_exposed_port(self._rest_port), |
| 112 | + grpc_port=self.get_exposed_port(self._grpc_port), |
| 113 | + api_key=self._api_key, |
| 114 | + https=False, |
| 115 | + **kwargs, |
| 116 | + ) |
| 117 | + |
| 118 | + @cached_property |
| 119 | + def rest_host_address(self) -> str: |
| 120 | + """ |
| 121 | + Get the REST host address of the Qdrant container. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + str: The REST host address of the Qdrant container. |
| 125 | + """ |
| 126 | + return f"{self.get_container_host_ip()}:{self.exposed_rest_port}" |
| 127 | + |
| 128 | + @cached_property |
| 129 | + def grpc_host_address(self) -> str: |
| 130 | + """ |
| 131 | + Get the GRPC host address of the Qdrant container. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + str: The GRPC host address of the Qdrant container. |
| 135 | + """ |
| 136 | + return f"{self.get_container_host_ip()}:{self.exposed_grpc_port}" |
| 137 | + |
| 138 | + @cached_property |
| 139 | + def exposed_rest_port(self) -> int: |
| 140 | + """ |
| 141 | + Get the exposed REST port of the Qdrant container. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + int: The REST port of the Qdrant container. |
| 145 | + """ |
| 146 | + return self.get_exposed_port(self._rest_port) |
| 147 | + |
| 148 | + @cached_property |
| 149 | + def exposed_grpc_port(self) -> int: |
| 150 | + """ |
| 151 | + Get the exposed GRPC port of the Qdrant container. |
| 152 | +
|
| 153 | + Returns: |
| 154 | + int: The GRPC port of the Qdrant container. |
| 155 | + """ |
| 156 | + return self.get_exposed_port(self._grpc_port) |
0 commit comments