alloydb-haystack 0.1.0__tar.gz

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.
Files changed (25) hide show
  1. alloydb_haystack-0.1.0/.gitignore +146 -0
  2. alloydb_haystack-0.1.0/CHANGELOG.md +7 -0
  3. alloydb_haystack-0.1.0/LICENSE.txt +73 -0
  4. alloydb_haystack-0.1.0/PKG-INFO +160 -0
  5. alloydb_haystack-0.1.0/README.md +133 -0
  6. alloydb_haystack-0.1.0/examples/embedding_retrieval.py +63 -0
  7. alloydb_haystack-0.1.0/examples/hybrid_retrieval.py +72 -0
  8. alloydb_haystack-0.1.0/pydoc/config_docusaurus.yml +15 -0
  9. alloydb_haystack-0.1.0/pyproject.toml +186 -0
  10. alloydb_haystack-0.1.0/src/haystack_integrations/components/retrievers/alloydb/__init__.py +8 -0
  11. alloydb_haystack-0.1.0/src/haystack_integrations/components/retrievers/alloydb/embedding_retriever.py +119 -0
  12. alloydb_haystack-0.1.0/src/haystack_integrations/components/retrievers/alloydb/keyword_retriever.py +104 -0
  13. alloydb_haystack-0.1.0/src/haystack_integrations/components/retrievers/py.typed +0 -0
  14. alloydb_haystack-0.1.0/src/haystack_integrations/document_stores/alloydb/__init__.py +7 -0
  15. alloydb_haystack-0.1.0/src/haystack_integrations/document_stores/alloydb/converters.py +81 -0
  16. alloydb_haystack-0.1.0/src/haystack_integrations/document_stores/alloydb/document_store.py +1312 -0
  17. alloydb_haystack-0.1.0/src/haystack_integrations/document_stores/alloydb/filters.py +269 -0
  18. alloydb_haystack-0.1.0/src/haystack_integrations/document_stores/py.typed +0 -0
  19. alloydb_haystack-0.1.0/tests/__init__.py +0 -0
  20. alloydb_haystack-0.1.0/tests/conftest.py +111 -0
  21. alloydb_haystack-0.1.0/tests/test_converters.py +157 -0
  22. alloydb_haystack-0.1.0/tests/test_document_store.py +249 -0
  23. alloydb_haystack-0.1.0/tests/test_filters.py +172 -0
  24. alloydb_haystack-0.1.0/tests/test_retrieval.py +134 -0
  25. alloydb_haystack-0.1.0/tests/test_retrievers.py +176 -0
@@ -0,0 +1,146 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+ MANIFEST
29
+
30
+ # PyInstaller
31
+ # Usually these files are written by a python script from a template
32
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
33
+ *.manifest
34
+ *.spec
35
+
36
+ # Installer logs
37
+ pip-log.txt
38
+ pip-delete-this-directory.txt
39
+
40
+ # Unit test / coverage reports
41
+ htmlcov/
42
+ .tox/
43
+ .nox/
44
+ .coverage
45
+ .coverage.*
46
+ .cache
47
+ nosetests.xml
48
+ coverage.xml
49
+ *.cover
50
+ *.py,cover
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ volumes/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ .python-version
87
+
88
+ # pipenv
89
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
90
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
91
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
92
+ # install all needed dependencies.
93
+ #Pipfile.lock
94
+
95
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
96
+ __pypackages__/
97
+
98
+ # Celery stuff
99
+ celerybeat-schedule
100
+ celerybeat.pid
101
+
102
+ # SageMath parsed files
103
+ *.sage.py
104
+
105
+ # Environments
106
+ .env
107
+ .venv
108
+ env/
109
+ venv/
110
+ ENV/
111
+ env.bak/
112
+ venv.bak/
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+
132
+ # IDEs
133
+ .vscode
134
+
135
+ # Docs generation artifacts
136
+ _readme_*.md
137
+ .idea
138
+
139
+ # macOS
140
+ .DS_Store
141
+
142
+ # http cache (requests-cache)
143
+ **/http_cache.sqlite
144
+
145
+ # ruff
146
+ .ruff_cache
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ### 🚀 First Release
6
+
7
+ - Initial implementation of `AlloyDBDocumentStore`, `AlloyDBEmbeddingRetriever`, and `AlloyDBKeywordRetriever`
@@ -0,0 +1,73 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
10
+
11
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
12
+
13
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
14
+
15
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
16
+
17
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
18
+
19
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
20
+
21
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
22
+
23
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
24
+
25
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
26
+
27
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
28
+
29
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
30
+
31
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
32
+
33
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
34
+
35
+ (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
36
+
37
+ (b) You must cause any modified files to carry prominent notices stating that You changed the files; and
38
+
39
+ (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40
+
41
+ (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
42
+
43
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
44
+
45
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
46
+
47
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
48
+
49
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
50
+
51
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
52
+
53
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
54
+
55
+ END OF TERMS AND CONDITIONS
56
+
57
+ APPENDIX: How to apply the Apache License to your work.
58
+
59
+ To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
60
+
61
+ Copyright [yyyy] [name of copyright owner]
62
+
63
+ Licensed under the Apache License, Version 2.0 (the "License");
64
+ you may not use this file except in compliance with the License.
65
+ You may obtain a copy of the License at
66
+
67
+ http://www.apache.org/licenses/LICENSE-2.0
68
+
69
+ Unless required by applicable law or agreed to in writing, software
70
+ distributed under the License is distributed on an "AS IS" BASIS,
71
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72
+ See the License for the specific language governing permissions and
73
+ limitations under the License.
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.4
2
+ Name: alloydb-haystack
3
+ Version: 0.1.0
4
+ Summary: An integration of Google Cloud AlloyDB with Haystack for vector search
5
+ Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations
6
+ Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/alloydb/README.md
7
+ Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
8
+ Author-email: deepset GmbH <info@deepset.ai>, Gary Badwal <gurpreet071999@gmail.com>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE.txt
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Programming Language :: Python :: 3.14
19
+ Classifier: Programming Language :: Python :: Implementation :: CPython
20
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
21
+ Requires-Python: >=3.10
22
+ Requires-Dist: google-cloud-alloydb-connector[psycopg]>=1.0.0
23
+ Requires-Dist: haystack-ai>=2.28.0
24
+ Requires-Dist: pgvector>=0.3.0
25
+ Requires-Dist: psycopg[binary]
26
+ Description-Content-Type: text/markdown
27
+
28
+ # AlloyDB Haystack Integration
29
+
30
+ [![PyPI - Version](https://img.shields.io/pypi/v/alloydb-haystack.svg)](https://pypi.org/project/alloydb-haystack)
31
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/alloydb-haystack.svg)](https://pypi.org/project/alloydb-haystack)
32
+
33
+ ---
34
+
35
+ [AlloyDB](https://cloud.google.com/alloydb) is a fully managed, PostgreSQL-compatible database service on Google Cloud,
36
+ optimised for demanding transactional and analytical workloads.
37
+
38
+ This package provides a Haystack `DocumentStore` backed by AlloyDB with the
39
+ [pgvector extension](https://cloud.google.com/alloydb/docs/ai/work-with-embeddings), enabling both dense vector
40
+ similarity search and full-text keyword search.
41
+
42
+ Connections are established through the
43
+ [AlloyDB Python Connector](https://github.com/GoogleCloudPlatform/alloydb-python-connector),
44
+ which handles IAM-based authentication and TLS encryption without requiring manual firewall rules or IP allowlisting.
45
+
46
+ ## Installation
47
+
48
+ ```console
49
+ pip install alloydb-haystack
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ ```python
55
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
56
+ from haystack_integrations.components.retrievers.alloydb import (
57
+ AlloyDBEmbeddingRetriever,
58
+ AlloyDBKeywordRetriever,
59
+ )
60
+ ```
61
+
62
+ ### Environment Variables
63
+
64
+ | Variable | Description |
65
+ |---|---|
66
+ | `ALLOYDB_INSTANCE_URI` | AlloyDB instance URI: `projects/P/locations/R/clusters/C/instances/I` |
67
+ | `ALLOYDB_USER` | Database user (or IAM principal for IAM auth) |
68
+ | `ALLOYDB_PASSWORD` | Database password (not required when `enable_iam_auth=True`) |
69
+
70
+ ### Basic Example
71
+
72
+ ```python
73
+ import os
74
+ from haystack import Document
75
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
76
+
77
+ # Requires ALLOYDB_INSTANCE_URI, ALLOYDB_USER, and ALLOYDB_PASSWORD env vars
78
+ store = AlloyDBDocumentStore(
79
+ db="my-database",
80
+ embedding_dimension=768,
81
+ recreate_table=True,
82
+ )
83
+
84
+ store.write_documents([
85
+ Document(content="Paris is the capital of France", embedding=[0.1] * 768),
86
+ Document(content="Berlin is the capital of Germany", embedding=[0.2] * 768),
87
+ ])
88
+
89
+ print(store.count_documents()) # 2
90
+ ```
91
+
92
+ ### IAM Authentication
93
+
94
+ When using a service account for database access:
95
+
96
+ ```python
97
+ store = AlloyDBDocumentStore(
98
+ db="my-database",
99
+ user=Secret.from_env_var("ALLOYDB_IAM_USER"), # e.g. "my-sa@my-project.iam"
100
+ enable_iam_auth=True,
101
+ embedding_dimension=768,
102
+ )
103
+ ```
104
+
105
+ ### Vector Similarity Search
106
+
107
+ ```python
108
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBEmbeddingRetriever
109
+
110
+ retriever = AlloyDBEmbeddingRetriever(document_store=store, top_k=5)
111
+ result = retriever.run(query_embedding=[0.1] * 768)
112
+ print(result["documents"])
113
+ ```
114
+
115
+ ### Keyword Search
116
+
117
+ ```python
118
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBKeywordRetriever
119
+
120
+ retriever = AlloyDBKeywordRetriever(document_store=store, top_k=5)
121
+ result = retriever.run(query="capital France")
122
+ print(result["documents"])
123
+ ```
124
+
125
+ ### HNSW Index
126
+
127
+ For large datasets, the HNSW index provides approximate nearest-neighbour search with significantly
128
+ better query throughput:
129
+
130
+ ```python
131
+ store = AlloyDBDocumentStore(
132
+ db="my-database",
133
+ embedding_dimension=768,
134
+ search_strategy="hnsw",
135
+ hnsw_index_creation_kwargs={"m": 16, "ef_construction": 64},
136
+ hnsw_ef_search=40,
137
+ )
138
+ ```
139
+
140
+ ## Integration Tests
141
+
142
+ Integration tests require a running AlloyDB instance. Set the following environment variables
143
+ before running:
144
+
145
+ ```console
146
+ export ALLOYDB_INSTANCE_URI="projects/MY_PROJECT/locations/MY_REGION/clusters/MY_CLUSTER/instances/MY_INSTANCE"
147
+ export ALLOYDB_USER="my-db-user"
148
+ export ALLOYDB_PASSWORD="my-db-password"
149
+ ```
150
+
151
+ Then run:
152
+
153
+ ```console
154
+ cd integrations/alloydb
155
+ hatch run test:integration
156
+ ```
157
+
158
+ ## License
159
+
160
+ `alloydb-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -0,0 +1,133 @@
1
+ # AlloyDB Haystack Integration
2
+
3
+ [![PyPI - Version](https://img.shields.io/pypi/v/alloydb-haystack.svg)](https://pypi.org/project/alloydb-haystack)
4
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/alloydb-haystack.svg)](https://pypi.org/project/alloydb-haystack)
5
+
6
+ ---
7
+
8
+ [AlloyDB](https://cloud.google.com/alloydb) is a fully managed, PostgreSQL-compatible database service on Google Cloud,
9
+ optimised for demanding transactional and analytical workloads.
10
+
11
+ This package provides a Haystack `DocumentStore` backed by AlloyDB with the
12
+ [pgvector extension](https://cloud.google.com/alloydb/docs/ai/work-with-embeddings), enabling both dense vector
13
+ similarity search and full-text keyword search.
14
+
15
+ Connections are established through the
16
+ [AlloyDB Python Connector](https://github.com/GoogleCloudPlatform/alloydb-python-connector),
17
+ which handles IAM-based authentication and TLS encryption without requiring manual firewall rules or IP allowlisting.
18
+
19
+ ## Installation
20
+
21
+ ```console
22
+ pip install alloydb-haystack
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```python
28
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
29
+ from haystack_integrations.components.retrievers.alloydb import (
30
+ AlloyDBEmbeddingRetriever,
31
+ AlloyDBKeywordRetriever,
32
+ )
33
+ ```
34
+
35
+ ### Environment Variables
36
+
37
+ | Variable | Description |
38
+ |---|---|
39
+ | `ALLOYDB_INSTANCE_URI` | AlloyDB instance URI: `projects/P/locations/R/clusters/C/instances/I` |
40
+ | `ALLOYDB_USER` | Database user (or IAM principal for IAM auth) |
41
+ | `ALLOYDB_PASSWORD` | Database password (not required when `enable_iam_auth=True`) |
42
+
43
+ ### Basic Example
44
+
45
+ ```python
46
+ import os
47
+ from haystack import Document
48
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
49
+
50
+ # Requires ALLOYDB_INSTANCE_URI, ALLOYDB_USER, and ALLOYDB_PASSWORD env vars
51
+ store = AlloyDBDocumentStore(
52
+ db="my-database",
53
+ embedding_dimension=768,
54
+ recreate_table=True,
55
+ )
56
+
57
+ store.write_documents([
58
+ Document(content="Paris is the capital of France", embedding=[0.1] * 768),
59
+ Document(content="Berlin is the capital of Germany", embedding=[0.2] * 768),
60
+ ])
61
+
62
+ print(store.count_documents()) # 2
63
+ ```
64
+
65
+ ### IAM Authentication
66
+
67
+ When using a service account for database access:
68
+
69
+ ```python
70
+ store = AlloyDBDocumentStore(
71
+ db="my-database",
72
+ user=Secret.from_env_var("ALLOYDB_IAM_USER"), # e.g. "my-sa@my-project.iam"
73
+ enable_iam_auth=True,
74
+ embedding_dimension=768,
75
+ )
76
+ ```
77
+
78
+ ### Vector Similarity Search
79
+
80
+ ```python
81
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBEmbeddingRetriever
82
+
83
+ retriever = AlloyDBEmbeddingRetriever(document_store=store, top_k=5)
84
+ result = retriever.run(query_embedding=[0.1] * 768)
85
+ print(result["documents"])
86
+ ```
87
+
88
+ ### Keyword Search
89
+
90
+ ```python
91
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBKeywordRetriever
92
+
93
+ retriever = AlloyDBKeywordRetriever(document_store=store, top_k=5)
94
+ result = retriever.run(query="capital France")
95
+ print(result["documents"])
96
+ ```
97
+
98
+ ### HNSW Index
99
+
100
+ For large datasets, the HNSW index provides approximate nearest-neighbour search with significantly
101
+ better query throughput:
102
+
103
+ ```python
104
+ store = AlloyDBDocumentStore(
105
+ db="my-database",
106
+ embedding_dimension=768,
107
+ search_strategy="hnsw",
108
+ hnsw_index_creation_kwargs={"m": 16, "ef_construction": 64},
109
+ hnsw_ef_search=40,
110
+ )
111
+ ```
112
+
113
+ ## Integration Tests
114
+
115
+ Integration tests require a running AlloyDB instance. Set the following environment variables
116
+ before running:
117
+
118
+ ```console
119
+ export ALLOYDB_INSTANCE_URI="projects/MY_PROJECT/locations/MY_REGION/clusters/MY_CLUSTER/instances/MY_INSTANCE"
120
+ export ALLOYDB_USER="my-db-user"
121
+ export ALLOYDB_PASSWORD="my-db-password"
122
+ ```
123
+
124
+ Then run:
125
+
126
+ ```console
127
+ cd integrations/alloydb
128
+ hatch run test:integration
129
+ ```
130
+
131
+ ## License
132
+
133
+ `alloydb-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -0,0 +1,63 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # Before running this example, ensure you have an AlloyDB instance running on Google Cloud.
6
+ # See: https://cloud.google.com/alloydb/docs/quickstart/create-and-connect
7
+ #
8
+ # Install required packages for this example:
9
+ # pip install alloydb-haystack "sentence-transformers>=2.2.0" markdown-it-py mdit_plain
10
+ #
11
+ # Download some Markdown files to index:
12
+ # git clone https://github.com/anakin87/neural-search-pills
13
+ #
14
+ # Set required environment variables:
15
+ # export ALLOYDB_INSTANCE_URI="projects/MY_PROJECT/locations/REGION/clusters/CLUSTER/instances/INSTANCE"
16
+ # export ALLOYDB_USER="my-db-user"
17
+ # export ALLOYDB_PASSWORD="my-db-password"
18
+
19
+ import glob
20
+
21
+ from haystack import Pipeline
22
+ from haystack.components.converters import MarkdownToDocument
23
+ from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
24
+ from haystack.components.preprocessors import DocumentSplitter
25
+ from haystack.components.writers import DocumentWriter
26
+
27
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBEmbeddingRetriever
28
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
29
+
30
+ # Initialize AlloyDBDocumentStore
31
+ document_store = AlloyDBDocumentStore(
32
+ db="postgres",
33
+ table_name="haystack_test",
34
+ embedding_dimension=768,
35
+ vector_function="cosine_similarity",
36
+ recreate_table=True,
37
+ search_strategy="hnsw",
38
+ )
39
+
40
+ # Create the indexing Pipeline and index some documents
41
+ file_paths = glob.glob("neural-search-pills/pills/*.md")
42
+
43
+ indexing = Pipeline()
44
+ indexing.add_component("converter", MarkdownToDocument())
45
+ indexing.add_component("splitter", DocumentSplitter(split_by="sentence", split_length=2))
46
+ indexing.add_component("embedder", SentenceTransformersDocumentEmbedder())
47
+ indexing.add_component("writer", DocumentWriter(document_store))
48
+ indexing.connect("converter", "splitter")
49
+ indexing.connect("splitter", "embedder")
50
+ indexing.connect("embedder", "writer")
51
+
52
+ indexing.run({"converter": {"sources": file_paths}})
53
+
54
+ # Create the querying Pipeline
55
+ querying = Pipeline()
56
+ querying.add_component("query_embedder", SentenceTransformersTextEmbedder())
57
+ querying.add_component("retriever", AlloyDBEmbeddingRetriever(document_store=document_store, top_k=3))
58
+ querying.connect("query_embedder.embedding", "retriever.query_embedding")
59
+
60
+ results = querying.run({"query_embedder": {"text": "What is Retrieval-Augmented Generation?"}})
61
+ for doc in results["retriever"]["documents"]:
62
+ print(doc.content)
63
+ print("---")
@@ -0,0 +1,72 @@
1
+ # SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # Before running this example, ensure you have an AlloyDB instance running on Google Cloud.
6
+ # See: https://cloud.google.com/alloydb/docs/quickstart/create-and-connect
7
+ #
8
+ # Install required packages for this example:
9
+ # pip install alloydb-haystack "sentence-transformers>=2.2.0" markdown-it-py mdit_plain
10
+ #
11
+ # Download some Markdown files to index:
12
+ # git clone https://github.com/anakin87/neural-search-pills
13
+ #
14
+ # Set required environment variables:
15
+ # export ALLOYDB_INSTANCE_URI="projects/MY_PROJECT/locations/REGION/clusters/CLUSTER/instances/INSTANCE"
16
+ # export ALLOYDB_USER="my-db-user"
17
+ # export ALLOYDB_PASSWORD="my-db-password"
18
+
19
+ import glob
20
+
21
+ from haystack import Pipeline
22
+ from haystack.components.converters import MarkdownToDocument
23
+ from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
24
+ from haystack.components.joiners import DocumentJoiner
25
+ from haystack.components.preprocessors import DocumentSplitter
26
+ from haystack.components.writers import DocumentWriter
27
+
28
+ from haystack_integrations.components.retrievers.alloydb import AlloyDBEmbeddingRetriever, AlloyDBKeywordRetriever
29
+ from haystack_integrations.document_stores.alloydb import AlloyDBDocumentStore
30
+
31
+ # Initialize AlloyDBDocumentStore
32
+ document_store = AlloyDBDocumentStore(
33
+ db="postgres",
34
+ table_name="haystack_hybrid_test",
35
+ embedding_dimension=768,
36
+ vector_function="cosine_similarity",
37
+ recreate_table=True,
38
+ )
39
+
40
+ # Create the indexing Pipeline and index some documents
41
+ file_paths = glob.glob("neural-search-pills/pills/*.md")
42
+
43
+ indexing = Pipeline()
44
+ indexing.add_component("converter", MarkdownToDocument())
45
+ indexing.add_component("splitter", DocumentSplitter(split_by="sentence", split_length=2))
46
+ indexing.add_component("embedder", SentenceTransformersDocumentEmbedder())
47
+ indexing.add_component("writer", DocumentWriter(document_store))
48
+ indexing.connect("converter", "splitter")
49
+ indexing.connect("splitter", "embedder")
50
+ indexing.connect("embedder", "writer")
51
+
52
+ indexing.run({"converter": {"sources": file_paths}})
53
+
54
+ # Create the hybrid querying Pipeline
55
+ querying = Pipeline()
56
+ querying.add_component("query_embedder", SentenceTransformersTextEmbedder())
57
+ querying.add_component("embedding_retriever", AlloyDBEmbeddingRetriever(document_store=document_store, top_k=5))
58
+ querying.add_component("keyword_retriever", AlloyDBKeywordRetriever(document_store=document_store, top_k=5))
59
+ querying.add_component("joiner", DocumentJoiner())
60
+ querying.connect("query_embedder.embedding", "embedding_retriever.query_embedding")
61
+ querying.connect("embedding_retriever", "joiner")
62
+ querying.connect("keyword_retriever", "joiner")
63
+
64
+ results = querying.run(
65
+ {
66
+ "query_embedder": {"text": "What is Retrieval-Augmented Generation?"},
67
+ "keyword_retriever": {"query": "Retrieval-Augmented Generation"},
68
+ }
69
+ )
70
+ for doc in results["joiner"]["documents"]:
71
+ print(doc.content)
72
+ print("---")
@@ -0,0 +1,15 @@
1
+ loaders:
2
+ - modules:
3
+ - haystack_integrations.components.retrievers.alloydb.embedding_retriever
4
+ - haystack_integrations.components.retrievers.alloydb.keyword_retriever
5
+ - haystack_integrations.document_stores.alloydb.document_store
6
+ search_path: [../src]
7
+ processors:
8
+ - type: filter
9
+ documented_only: true
10
+ skip_empty_modules: true
11
+ renderer:
12
+ description: AlloyDB integration for Haystack
13
+ id: integrations-alloydb
14
+ filename: alloydb.md
15
+ title: AlloyDB