ibm-db-haystack 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from haystack_integrations.components.retrievers.ibm_db.embedding_retriever import IBMDb2EmbeddingRetriever
6
+
7
+ __all__ = ["IBMDb2EmbeddingRetriever"]
8
+
9
+ # Made with Bob
@@ -0,0 +1,109 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from typing import Any
6
+
7
+ from haystack import component, default_from_dict, default_to_dict
8
+ from haystack.dataclasses import Document
9
+ from haystack.document_stores.types import FilterPolicy
10
+ from haystack.document_stores.types.filter_policy import apply_filter_policy
11
+
12
+ from haystack_integrations.document_stores.ibm_db import IBMDb2DocumentStore
13
+
14
+
15
+ @component
16
+ class IBMDb2EmbeddingRetriever:
17
+ """
18
+ Retrieves documents from a IBMDb2DocumentStore using vector similarity.
19
+
20
+ Use inside a Haystack pipeline after a text embedder:
21
+
22
+ ```python
23
+ pipeline.add_component("embedder", SentenceTransformersTextEmbedder())
24
+ pipeline.add_component("retriever", IBMDb2EmbeddingRetriever(
25
+ document_store=store, top_k=5
26
+ ))
27
+ pipeline.connect("embedder.embedding", "retriever.query_embedding")
28
+ ```
29
+ """
30
+
31
+ def __init__(
32
+ self,
33
+ *,
34
+ document_store: IBMDb2DocumentStore,
35
+ filters: dict[str, Any] | None = None,
36
+ top_k: int = 10,
37
+ filter_policy: FilterPolicy = FilterPolicy.REPLACE,
38
+ ) -> None:
39
+ """
40
+ Initialize the IBMDb2EmbeddingRetriever.
41
+
42
+ :param document_store: An instance of `IBMDb2DocumentStore`.
43
+ :param filters: Filters applied to the retrieved Documents.
44
+ :param top_k: Maximum number of Documents to return.
45
+ :param filter_policy: Policy to determine how filters are applied.
46
+ :raises TypeError: If `document_store` is not an instance of `IBMDb2DocumentStore`.
47
+ """
48
+ if not isinstance(document_store, IBMDb2DocumentStore):
49
+ msg = "document_store must be an instance of IBMDb2DocumentStore"
50
+ raise TypeError(msg)
51
+ self.document_store = document_store
52
+ self.filters = filters or {}
53
+ self.top_k = top_k
54
+ self.filter_policy = FilterPolicy.from_str(filter_policy) if isinstance(filter_policy, str) else filter_policy
55
+
56
+ @component.output_types(documents=list[Document])
57
+ def run(
58
+ self,
59
+ query_embedding: list[float],
60
+ filters: dict[str, Any] | None = None,
61
+ top_k: int | None = None,
62
+ ) -> dict[str, list[Document]]:
63
+ """
64
+ Retrieve documents by vector similarity.
65
+
66
+ :param query_embedding: Dense float vector from an embedder component.
67
+ :param filters: Runtime filters, merged with constructor filters according to filter_policy.
68
+ :param top_k: Override the constructor top_k for this call.
69
+ :returns: A dictionary with key `documents` containing a list of matching :class:`Document` objects.
70
+ """
71
+ filters = apply_filter_policy(self.filter_policy, self.filters, filters)
72
+ docs = self.document_store._embedding_retrieval(
73
+ query_embedding,
74
+ filters=filters,
75
+ top_k=top_k if top_k is not None else self.top_k,
76
+ )
77
+ return {"documents": docs}
78
+
79
+ def to_dict(self) -> dict[str, Any]:
80
+ """
81
+ Serializes the component to a dictionary.
82
+
83
+ :returns:
84
+ Dictionary with serialized data.
85
+ """
86
+ return default_to_dict(
87
+ self,
88
+ document_store=self.document_store.to_dict(),
89
+ filters=self.filters,
90
+ top_k=self.top_k,
91
+ filter_policy=self.filter_policy.value,
92
+ )
93
+
94
+ @classmethod
95
+ def from_dict(cls, data: dict[str, Any]) -> "IBMDb2EmbeddingRetriever":
96
+ """
97
+ Deserializes the component from a dictionary.
98
+
99
+ :param data:
100
+ Dictionary to deserialize from.
101
+ :returns:
102
+ Deserialized component.
103
+ """
104
+ params = data.get("init_parameters", {})
105
+ if "document_store" in params:
106
+ params["document_store"] = IBMDb2DocumentStore.from_dict(params["document_store"])
107
+ if filter_policy := params.get("filter_policy"):
108
+ params["filter_policy"] = FilterPolicy.from_str(filter_policy)
109
+ return default_from_dict(cls, data)
File without changes
@@ -0,0 +1,8 @@
1
+ # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ from haystack_integrations.document_stores.ibm_db.document_store import IBMDb2DocumentStore
6
+ from haystack_integrations.document_stores.ibm_db.filters import FilterTranslator
7
+
8
+ __all__ = ["FilterTranslator", "IBMDb2DocumentStore"]