|
36 | 36 |
|
37 | 37 |
|
38 | 38 | class _WeaviateClientExecutor(Generic[ConnectionType]): |
39 | | - def __init__(self, connection: ConnectionType): |
40 | | - self._connection = connection |
| 39 | + _connection_type: Type[ConnectionType] |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + connection_params: Optional[ConnectionParams] = None, |
| 44 | + embedded_options: Optional[EmbeddedOptions] = None, |
| 45 | + auth_client_secret: Optional[AuthCredentials] = None, |
| 46 | + additional_headers: Optional[dict] = None, |
| 47 | + additional_config: Optional[AdditionalConfig] = None, |
| 48 | + skip_init_checks: bool = False, |
| 49 | + ) -> None: |
| 50 | + """Initialise a WeaviateClient/WeaviateClientAsync class instance to use when interacting with Weaviate. |
| 51 | +
|
| 52 | + Use this specific initializer when you want to create a custom Client specific to your Weaviate setup. |
| 53 | +
|
| 54 | + To simplify connections to Weaviate Cloud or local instances, use the weaviate.connect_to_weaviate_cloud |
| 55 | + or weaviate.connect_to_local helper functions. |
| 56 | +
|
| 57 | + Arguments: |
| 58 | + - `connection_params`: `weaviate.connect.ConnectionParams` or None, optional |
| 59 | + - The connection parameters to use for the underlying HTTP requests. |
| 60 | + - `embedded_options`: `weaviate.EmbeddedOptions` or None, optional |
| 61 | + - The options to use when provisioning an embedded Weaviate instance. |
| 62 | + - `auth_client_secret`: `weaviate.AuthCredentials` or None, optional |
| 63 | + - Authenticate to weaviate by using one of the given authentication modes: |
| 64 | + - `weaviate.auth.AuthBearerToken` to use existing access and (optionally, but recommended) refresh tokens |
| 65 | + - `weaviate.auth.AuthClientPassword` to use username and password for oidc Resource Owner Password flow |
| 66 | + - `weaviate.auth.AuthClientCredentials` to use a client secret for oidc client credential flow |
| 67 | + - `additional_headers`: `dict` or None, optional |
| 68 | + - Additional headers to include in the requests. |
| 69 | + - Can be used to set OpenAI/HuggingFace/Cohere etc. keys. |
| 70 | + - [Here](https://weaviate.io/developers/weaviate/modules/reader-generator-modules/generative-openai#providing-the-key-to-weaviate) is an |
| 71 | + example of how to set API keys within this parameter. |
| 72 | + - `additional_config`: `weaviate.AdditionalConfig` or None, optional |
| 73 | + - Additional and advanced configuration options for Weaviate. |
| 74 | + - `skip_init_checks`: `bool`, optional |
| 75 | + - If set to `True` then the client will not perform any checks including ensuring that weaviate has started. This is useful for air-gapped environments and high-performance setups. |
| 76 | + """ |
| 77 | + connection_params, embedded_db = self.__parse_connection_params_and_embedded_db( |
| 78 | + connection_params, embedded_options |
| 79 | + ) |
| 80 | + config = additional_config or AdditionalConfig() |
| 81 | + |
| 82 | + self._connection = ( |
| 83 | + self._connection_type( # pyright: ignore reportIncompatibleVariableOverride |
| 84 | + connection_params=connection_params, |
| 85 | + auth_client_secret=auth_client_secret, |
| 86 | + timeout_config=config.timeout, |
| 87 | + additional_headers=additional_headers, |
| 88 | + embedded_db=embedded_db, |
| 89 | + connection_config=config.connection, |
| 90 | + proxies=config.proxies, |
| 91 | + trust_env=config.trust_env, |
| 92 | + skip_init_checks=skip_init_checks, |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + self.integrations = _Integrations(self._connection) |
| 97 | + |
| 98 | + def __parse_connection_params_and_embedded_db( |
| 99 | + self, |
| 100 | + connection_params: Optional[ConnectionParams], |
| 101 | + embedded_options: Optional[EmbeddedOptions], |
| 102 | + ) -> Tuple[ConnectionParams, Optional[EmbeddedV4]]: |
| 103 | + if connection_params is None and embedded_options is None: |
| 104 | + raise TypeError("Either connection_params or embedded_options must be present.") |
| 105 | + elif connection_params is not None and embedded_options is not None: |
| 106 | + raise TypeError( |
| 107 | + f"connection_params is not expected to be set when using embedded_options but connection_params was {connection_params}" |
| 108 | + ) |
| 109 | + |
| 110 | + if embedded_options is not None: |
| 111 | + _validate_input( |
| 112 | + _ValidateArgument([EmbeddedOptions], "embedded_options", embedded_options) |
| 113 | + ) |
| 114 | + |
| 115 | + embedded_db = EmbeddedV4(options=embedded_options) |
| 116 | + embedded_db.start() |
| 117 | + return ( |
| 118 | + ConnectionParams( |
| 119 | + http=ProtocolParams( |
| 120 | + host="localhost", port=embedded_db.options.port, secure=False |
| 121 | + ), |
| 122 | + grpc=ProtocolParams( |
| 123 | + host="localhost", port=embedded_options.grpc_port, secure=False |
| 124 | + ), |
| 125 | + ), |
| 126 | + embedded_db, |
| 127 | + ) |
| 128 | + |
| 129 | + if not isinstance(connection_params, ConnectionParams): |
| 130 | + raise TypeError( |
| 131 | + f"connection_params is expected to be a ConnectionParams object but is {type(connection_params)}" |
| 132 | + ) |
| 133 | + |
| 134 | + return connection_params, None |
41 | 135 |
|
42 | 136 | async def __close_async(self) -> None: |
43 | 137 | await executor.aresult(self._connection.close("async")) |
@@ -184,106 +278,6 @@ def get_open_id_configuration( |
184 | 278 | method=self._connection.get_open_id_configuration, |
185 | 279 | ) |
186 | 280 |
|
187 | | - |
188 | | -class _WeaviateClientBase(Generic[ConnectionType], _WeaviateClientExecutor[ConnectionType]): |
189 | | - _connection: ConnectionType |
190 | | - _connection_type: Type[ConnectionType] |
191 | | - |
192 | | - def __init__( |
193 | | - self, |
194 | | - connection_params: Optional[ConnectionParams] = None, |
195 | | - embedded_options: Optional[EmbeddedOptions] = None, |
196 | | - auth_client_secret: Optional[AuthCredentials] = None, |
197 | | - additional_headers: Optional[dict] = None, |
198 | | - additional_config: Optional[AdditionalConfig] = None, |
199 | | - skip_init_checks: bool = False, |
200 | | - ) -> None: |
201 | | - """Initialise a WeaviateClient/WeaviateClientAsync class instance to use when interacting with Weaviate. |
202 | | -
|
203 | | - Use this specific initializer when you want to create a custom Client specific to your Weaviate setup. |
204 | | -
|
205 | | - To simplify connections to Weaviate Cloud or local instances, use the weaviate.connect_to_weaviate_cloud |
206 | | - or weaviate.connect_to_local helper functions. |
207 | | -
|
208 | | - Arguments: |
209 | | - - `connection_params`: `weaviate.connect.ConnectionParams` or None, optional |
210 | | - - The connection parameters to use for the underlying HTTP requests. |
211 | | - - `embedded_options`: `weaviate.EmbeddedOptions` or None, optional |
212 | | - - The options to use when provisioning an embedded Weaviate instance. |
213 | | - - `auth_client_secret`: `weaviate.AuthCredentials` or None, optional |
214 | | - - Authenticate to weaviate by using one of the given authentication modes: |
215 | | - - `weaviate.auth.AuthBearerToken` to use existing access and (optionally, but recommended) refresh tokens |
216 | | - - `weaviate.auth.AuthClientPassword` to use username and password for oidc Resource Owner Password flow |
217 | | - - `weaviate.auth.AuthClientCredentials` to use a client secret for oidc client credential flow |
218 | | - - `additional_headers`: `dict` or None, optional |
219 | | - - Additional headers to include in the requests. |
220 | | - - Can be used to set OpenAI/HuggingFace/Cohere etc. keys. |
221 | | - - [Here](https://weaviate.io/developers/weaviate/modules/reader-generator-modules/generative-openai#providing-the-key-to-weaviate) is an |
222 | | - example of how to set API keys within this parameter. |
223 | | - - `additional_config`: `weaviate.AdditionalConfig` or None, optional |
224 | | - - Additional and advanced configuration options for Weaviate. |
225 | | - - `skip_init_checks`: `bool`, optional |
226 | | - - If set to `True` then the client will not perform any checks including ensuring that weaviate has started. This is useful for air-gapped environments and high-performance setups. |
227 | | - """ |
228 | | - connection_params, embedded_db = self.__parse_connection_params_and_embedded_db( |
229 | | - connection_params, embedded_options |
230 | | - ) |
231 | | - config = additional_config or AdditionalConfig() |
232 | | - |
233 | | - connection = self._connection_type( # pyright: ignore reportIncompatibleVariableOverride |
234 | | - connection_params=connection_params, |
235 | | - auth_client_secret=auth_client_secret, |
236 | | - timeout_config=config.timeout, |
237 | | - additional_headers=additional_headers, |
238 | | - embedded_db=embedded_db, |
239 | | - connection_config=config.connection, |
240 | | - proxies=config.proxies, |
241 | | - trust_env=config.trust_env, |
242 | | - skip_init_checks=skip_init_checks, |
243 | | - ) |
244 | | - |
245 | | - self.integrations = _Integrations(connection) |
246 | | - |
247 | | - super().__init__(connection) |
248 | | - |
249 | | - def __parse_connection_params_and_embedded_db( |
250 | | - self, |
251 | | - connection_params: Optional[ConnectionParams], |
252 | | - embedded_options: Optional[EmbeddedOptions], |
253 | | - ) -> Tuple[ConnectionParams, Optional[EmbeddedV4]]: |
254 | | - if connection_params is None and embedded_options is None: |
255 | | - raise TypeError("Either connection_params or embedded_options must be present.") |
256 | | - elif connection_params is not None and embedded_options is not None: |
257 | | - raise TypeError( |
258 | | - f"connection_params is not expected to be set when using embedded_options but connection_params was {connection_params}" |
259 | | - ) |
260 | | - |
261 | | - if embedded_options is not None: |
262 | | - _validate_input( |
263 | | - _ValidateArgument([EmbeddedOptions], "embedded_options", embedded_options) |
264 | | - ) |
265 | | - |
266 | | - embedded_db = EmbeddedV4(options=embedded_options) |
267 | | - embedded_db.start() |
268 | | - return ( |
269 | | - ConnectionParams( |
270 | | - http=ProtocolParams( |
271 | | - host="localhost", port=embedded_db.options.port, secure=False |
272 | | - ), |
273 | | - grpc=ProtocolParams( |
274 | | - host="localhost", port=embedded_options.grpc_port, secure=False |
275 | | - ), |
276 | | - ), |
277 | | - embedded_db, |
278 | | - ) |
279 | | - |
280 | | - if not isinstance(connection_params, ConnectionParams): |
281 | | - raise TypeError( |
282 | | - f"connection_params is expected to be a ConnectionParams object but is {type(connection_params)}" |
283 | | - ) |
284 | | - |
285 | | - return connection_params, None |
286 | | - |
287 | 281 | @executor.no_wrapping |
288 | 282 | def is_connected(self) -> bool: |
289 | 283 | """Check if the client is connected to Weaviate. |
|
0 commit comments