Skip to content

Commit 9387b74

Browse files
committed
more linting fixes
1 parent 2fd89bd commit 9387b74

21 files changed

+439
-78
lines changed

elasticsearch/vectorstore/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.

elasticsearch/vectorstore/_async/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from elasticsearch.vectorstore._async.vectorestore import AsyncVectorStore
219

320
__all__ = [

elasticsearch/vectorstore/_async/_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from elasticsearch import (
219
AsyncElasticsearch,
320
BadRequestError,

elasticsearch/vectorstore/_async/embedding_service.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from abc import ABC, abstractmethod
219
from typing import List, Optional
320

elasticsearch/vectorstore/_async/strategies.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from abc import ABC, abstractmethod
219
from enum import Enum
320
from typing import Any, Dict, List, Literal, Optional, Union, cast
@@ -23,9 +40,9 @@ async def es_query(
2340
query: Optional[str],
2441
k: int,
2542
num_candidates: int,
26-
filter: List[dict] = [],
43+
filter: List[Dict[str, Any]] = [],
2744
query_vector: Optional[List[float]] = None,
28-
) -> Dict:
45+
) -> Dict[str, Any]:
2946
"""
3047
Returns the Elasticsearch query body for the given parameters.
3148
The store will execute the query.
@@ -46,7 +63,7 @@ async def create_index(
4663
self,
4764
client: AsyncElasticsearch,
4865
index_name: str,
49-
metadata_mapping: Optional[dict[str, str]],
66+
metadata_mapping: Optional[Dict[str, str]],
5067
) -> None:
5168
"""
5269
Create the required index and do necessary preliminary work, like
@@ -95,9 +112,9 @@ async def es_query(
95112
query: Optional[str],
96113
k: int,
97114
num_candidates: int,
98-
filter: List[dict] = [],
115+
filter: List[Dict[str, Any]] = [],
99116
query_vector: Optional[List[float]] = None,
100-
) -> Dict:
117+
) -> Dict[str, Any]:
101118
if query_vector:
102119
raise ValueError(
103120
"Cannot do sparse retrieval with a query_vector. "
@@ -117,12 +134,12 @@ async def create_index(
117134
self,
118135
client: AsyncElasticsearch,
119136
index_name: str,
120-
metadata_mapping: Optional[dict[str, str]],
137+
metadata_mapping: Optional[Dict[str, str]],
121138
) -> None:
122139
if self.model_id:
123140
await model_must_be_deployed(client, self.model_id)
124141

125-
mappings: dict[str, Any] = {
142+
mappings: Dict[str, Any] = {
126143
"properties": {
127144
self.inference_field: {
128145
"type": "semantic_text",
@@ -155,9 +172,9 @@ async def es_query(
155172
query: Optional[str],
156173
k: int,
157174
num_candidates: int,
158-
filter: List[dict] = [],
175+
filter: List[Dict[str, Any]] = [],
159176
query_vector: Optional[List[float]] = None,
160-
) -> Dict:
177+
) -> Dict[str, Any]:
161178
if query_vector:
162179
raise ValueError(
163180
"Cannot do sparse retrieval with a query_vector. "
@@ -189,7 +206,7 @@ async def create_index(
189206
self,
190207
client: AsyncElasticsearch,
191208
index_name: str,
192-
metadata_mapping: Optional[dict[str, str]],
209+
metadata_mapping: Optional[Dict[str, str]],
193210
) -> None:
194211
pipeline_name = f"{self.model_id}_sparse_embedding"
195212

@@ -214,7 +231,7 @@ async def create_index(
214231
],
215232
)
216233

217-
mappings = {
234+
mappings: Dict[str, Any] = {
218235
"properties": {
219236
self.vector_field: {
220237
"properties": {self._tokens_field: {"type": "rank_features"}}
@@ -244,7 +261,7 @@ def __init__(
244261
model_id: Optional[str] = None,
245262
num_dimensions: Optional[int] = None,
246263
hybrid: bool = False,
247-
rrf: Union[bool, dict] = True,
264+
rrf: Union[bool, Dict[str, Any]] = True,
248265
text_field: Optional[str] = "text_field",
249266
):
250267
if embedding_service and model_id:
@@ -273,9 +290,9 @@ async def es_query(
273290
query: Optional[str],
274291
k: int,
275292
num_candidates: int,
276-
filter: List[dict] = [],
293+
filter: List[Dict[str, Any]] = [],
277294
query_vector: Optional[List[float]] = None,
278-
) -> Dict:
295+
) -> Dict[str, Any]:
279296
knn = {
280297
"filter": filter,
281298
"field": self.vector_field,
@@ -308,7 +325,7 @@ async def create_index(
308325
self,
309326
client: AsyncElasticsearch,
310327
index_name: str,
311-
metadata_mapping: Optional[dict[str, str]],
328+
metadata_mapping: Optional[Dict[str, str]],
312329
) -> None:
313330
if self.embedding_service and not self.num_dimensions:
314331
self.num_dimensions = len(
@@ -351,7 +368,9 @@ async def embed_for_indexing(self, text: str) -> Dict[str, Any]:
351368
return {self.vector_field: vector}
352369
return {}
353370

354-
def _hybrid(self, query: str, knn: dict, filter: list):
371+
def _hybrid(
372+
self, query: str, knn: Dict[str, Any], filter: List[Dict[str, Any]]
373+
) -> Dict[str, Any]:
355374
# Add a query to the knn query.
356375
# RRF is used to even the score from the knn query and text query
357376
# RRF has two optional parameters: {'rank_constant':int, 'window_size':int}
@@ -374,7 +393,7 @@ def _hybrid(self, query: str, knn: dict, filter: list):
374393
},
375394
}
376395

377-
if isinstance(self.rrf, dict):
396+
if isinstance(self.rrf, Dict[str, Any]):
378397
query_body["rank"] = {"rrf": self.rrf}
379398
elif isinstance(self.rrf, bool) and self.rrf is True:
380399
query_body["rank"] = {"rrf": {}}
@@ -402,9 +421,9 @@ async def es_query(
402421
query: Optional[str],
403422
k: int,
404423
num_candidates: int,
405-
filter: List[dict] = [],
424+
filter: List[Dict[str, Any]] = [],
406425
query_vector: Optional[List[float]] = None,
407-
) -> Dict:
426+
) -> Dict[str, Any]:
408427
if self.distance is DistanceMetric.COSINE:
409428
similarityAlgo = (
410429
f"cosineSimilarity(params.query_vector, '{self.vector_field}') + 1.0"
@@ -429,7 +448,7 @@ async def es_query(
429448
else:
430449
raise ValueError(f"Similarity {self.distance} not supported.")
431450

432-
queryBool: Dict = {"match_all": {}}
451+
queryBool: Dict[str, Any] = {"match_all": {}}
433452
if filter:
434453
queryBool = {"bool": {"filter": filter}}
435454

@@ -459,7 +478,7 @@ async def create_index(
459478
self,
460479
client: AsyncElasticsearch,
461480
index_name: str,
462-
metadata_mapping: Optional[dict[str, str]],
481+
metadata_mapping: Optional[Dict[str, str]],
463482
) -> None:
464483
if not self.num_dimensions:
465484
self.num_dimensions = len(
@@ -502,9 +521,9 @@ async def es_query(
502521
query: Optional[str],
503522
k: int,
504523
num_candidates: int,
505-
filter: List[dict] = [],
524+
filter: List[Dict[str, Any]] = [],
506525
query_vector: Optional[List[float]] = None,
507-
) -> Dict:
526+
) -> Dict[str, Any]:
508527
return {
509528
"query": {
510529
"bool": {
@@ -526,11 +545,11 @@ async def create_index(
526545
self,
527546
client: AsyncElasticsearch,
528547
index_name: str,
529-
metadata_mapping: Optional[dict[str, str]],
548+
metadata_mapping: Optional[Dict[str, str]],
530549
) -> None:
531550
similarity_name = "custom_bm25"
532551

533-
mappings: Dict = {
552+
mappings: Dict[str, Any] = {
534553
"properties": {
535554
self.text_field: {
536555
"type": "text",
@@ -541,7 +560,7 @@ async def create_index(
541560
if metadata_mapping:
542561
mappings["properties"]["metadata"] = {"properties": metadata_mapping}
543562

544-
bm25: Dict = {
563+
bm25: Dict[str, Any] = {
545564
"type": "BM25",
546565
}
547566
if self.k1 is not None:

elasticsearch/vectorstore/_async/vectorestore.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
import logging
219
import uuid
320
from typing import Any, Callable, Dict, List, Optional
@@ -29,7 +46,7 @@ def __init__(
2946
retrieval_strategy: RetrievalStrategy,
3047
text_field: str = "text_field",
3148
vector_field: str = "vector_field",
32-
metadata_mapping: Optional[dict[str, str]] = None,
49+
metadata_mapping: Optional[Dict[str, str]] = None,
3350
) -> None:
3451
"""
3552
Args:
@@ -61,7 +78,7 @@ def __init__(
6178
self.vector_field = vector_field
6279
self.metadata_mapping = metadata_mapping
6380

64-
async def close(self):
81+
async def close(self) -> None:
6582
return await self.es_client.close()
6683

6784
async def add_texts(
@@ -196,8 +213,10 @@ async def search(
196213
k: int = 4,
197214
num_candidates: int = 50,
198215
fields: Optional[List[str]] = None,
199-
filter: Optional[List[dict]] = None,
200-
custom_query: Optional[Callable[[Dict, Optional[str]], Dict]] = None,
216+
filter: Optional[List[Dict[str, Any]]] = None,
217+
custom_query: Optional[
218+
Callable[[Dict[str, Any], Optional[str]], Dict[str, Any]]
219+
] = None,
201220
) -> List[Dict[str, Any]]:
202221
"""
203222
Args:
@@ -263,8 +282,10 @@ async def max_marginal_relevance_search(
263282
num_candidates: int = 20,
264283
lambda_mult: float = 0.5,
265284
fields: Optional[List[str]] = None,
266-
custom_query: Optional[Callable[[Dict, Optional[str]], Dict]] = None,
267-
) -> List[Dict]:
285+
custom_query: Optional[
286+
Callable[[Dict[str, Any], Optional[str]], Dict[str, Any]]
287+
] = None,
288+
) -> List[Dict[str, Any]]:
268289
"""Return docs selected using the maximal marginal relevance.
269290
270291
Maximal marginal relevance optimizes for similarity to query AND diversity

elasticsearch/vectorstore/_sync/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
118
from elasticsearch.vectorstore._sync.vectorestore import VectorStore
219

320
__all__ = [

0 commit comments

Comments
 (0)