|
| 1 | +use std::collections::HashMap; |
1 | 2 | use std::time::Duration; |
2 | 3 |
|
3 | 4 | use crate::{Qdrant, QdrantError}; |
@@ -33,6 +34,9 @@ pub struct QdrantConfig { |
33 | 34 | /// Optional API key or token to use for authorization |
34 | 35 | pub api_key: Option<String>, |
35 | 36 |
|
| 37 | + /// Optional API keys for external embedding providers (OpenAI, JINA, Cohere, OpenRouter) |
| 38 | + pub external_api_keys: Option<HashMap<String, String>>, |
| 39 | + |
36 | 40 | /// Optional compression schema to use for API requests |
37 | 41 | pub compression: Option<CompressionEncoding>, |
38 | 42 |
|
@@ -89,6 +93,22 @@ impl QdrantConfig { |
89 | 93 | self |
90 | 94 | } |
91 | 95 |
|
| 96 | + /// Set an optional map of external API keys for embedding providers (OpenAI, JINA, Cohere, OpenRouter) |
| 97 | + /// |
| 98 | + /// # Examples |
| 99 | + /// ```rust,no_run |
| 100 | + ///# use std::collections::HashMap; |
| 101 | + ///# let config: HashMap<&str, String> = HashMap::new(); |
| 102 | + ///# use qdrant_client::Qdrant; |
| 103 | + /// let client = Qdrant::from_url("http://localhost:6334") |
| 104 | + /// .external_api_keys(config.get("external_api_keys")) |
| 105 | + /// .build(); |
| 106 | + /// ``` |
| 107 | + pub fn external_api_keys(mut self, external_api_keys: impl AsOptionExternalApiKeys) -> Self { |
| 108 | + self.external_api_keys = external_api_keys.external_api_keys(); |
| 109 | + self |
| 110 | + } |
| 111 | + |
92 | 112 | /// Keep the connection alive while idle |
93 | 113 | pub fn keep_alive_while_idle(mut self) -> Self { |
94 | 114 | self.keep_alive_while_idle = true; |
@@ -201,6 +221,7 @@ impl Default for QdrantConfig { |
201 | 221 | connect_timeout: Duration::from_secs(5), |
202 | 222 | keep_alive_while_idle: true, |
203 | 223 | api_key: None, |
| 224 | + external_api_keys: None, |
204 | 225 | compression: None, |
205 | 226 | check_compatibility: true, |
206 | 227 | pool_size: 3, |
@@ -303,3 +324,69 @@ impl<E: Sized> AsOptionApiKey for Result<String, E> { |
303 | 324 | self.ok() |
304 | 325 | } |
305 | 326 | } |
| 327 | + |
| 328 | +/// Set an optional API key from various types |
| 329 | +/// |
| 330 | +/// For example: |
| 331 | +/// |
| 332 | +/// ```rust |
| 333 | +///# use std::time::Duration; |
| 334 | +///# use qdrant_client::Qdrant; |
| 335 | +///# let mut config = Qdrant::from_url("http://localhost:6334"); |
| 336 | +/// config |
| 337 | +/// .external_api_keys(("openai-api-key", "<YOUR_OPENAI_API_KEY>")) |
| 338 | +/// .external_api_keys((String::from("openai-api-key"), String::from("<YOUR_OPENAI_API_KEY>"))) |
| 339 | +/// .external_api_keys((String::from("openai-api-key").unwrap(), std::env::var("OPENAI_API_KEY").unwrap())); |
| 340 | +/// ``` |
| 341 | +/// |
| 342 | +/// /// ```rust |
| 343 | +///# use std::time::Duration; |
| 344 | +///# use qdrant_client::Qdrant; |
| 345 | +///# let mut config = Qdrant::from_url("http://localhost:6334"); |
| 346 | +///# let ext_api_keys = HashMap::from([("openai-api-key", "<YOUR_OPENAI_API_KEY>"), ("cohere-api-key", "<YOUR_COHERE_API_KEY>")]) |
| 347 | +/// config |
| 348 | +/// .external_api_keys(ext_api_keys); |
| 349 | +/// ``` |
| 350 | +pub trait AsOptionExternalApiKeys { |
| 351 | + fn external_api_keys(self) -> Option<HashMap<String, String>>; |
| 352 | +} |
| 353 | + |
| 354 | +impl<K, V> AsOptionExternalApiKeys for (K, V) |
| 355 | +where |
| 356 | + K: Into<String>, |
| 357 | + V: Into<String>, |
| 358 | +{ |
| 359 | + fn external_api_keys(self) -> Option<HashMap<String, String>> { |
| 360 | + let (k, v) = self; |
| 361 | + Some(HashMap::from([(k.into(), v.into())])) |
| 362 | + } |
| 363 | +} |
| 364 | + |
| 365 | +impl<K, V> AsOptionExternalApiKeys for Option<(K, V)> |
| 366 | +where |
| 367 | + K: Into<String>, |
| 368 | + V: Into<String>, |
| 369 | +{ |
| 370 | + fn external_api_keys(self) -> Option<HashMap<String, String>> { |
| 371 | + let (k, v) = self?; |
| 372 | + Some(HashMap::from([(k.into(), v.into())])) |
| 373 | + } |
| 374 | +} |
| 375 | + |
| 376 | +impl AsOptionExternalApiKeys for Option<HashMap<String, String>> { |
| 377 | + fn external_api_keys(self) -> Option<HashMap<String, String>> { |
| 378 | + self |
| 379 | + } |
| 380 | +} |
| 381 | + |
| 382 | +impl AsOptionExternalApiKeys for HashMap<String, String> { |
| 383 | + fn external_api_keys(self) -> Option<HashMap<String, String>> { |
| 384 | + Some(self) |
| 385 | + } |
| 386 | +} |
| 387 | + |
| 388 | +impl<E: Sized> AsOptionExternalApiKeys for Result<HashMap<String, String>, E> { |
| 389 | + fn external_api_keys(self) -> Option<HashMap<String, String>> { |
| 390 | + self.ok() |
| 391 | + } |
| 392 | +} |
0 commit comments