|
| 1 | +--- |
| 2 | +layout: integration |
| 3 | +name: IBM Db2 |
| 4 | +description: A Document Store for storing and retrieving documents from IBM Db2 |
| 5 | +authors: |
| 6 | + - name: Geetika Chugh |
| 7 | + socials: |
| 8 | + github: https://github.com/GeetikaChughIBM |
| 9 | + linkedin: https://in.linkedin.com/in/geetika-chugh-085251252 |
| 10 | +pypi: https://pypi.org/project/ibm-db-haystack/ |
| 11 | +repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ibm_db |
| 12 | +type: Document Store |
| 13 | +report_issue: https://github.com/deepset-ai/haystack-core-integrations/issues |
| 14 | +version: Haystack 2.0 |
| 15 | +toc: true |
| 16 | +--- |
| 17 | + |
| 18 | +[](https://pypi.org/project/ibm-db-haystack/) |
| 19 | +[](https://pypi.org/project/ibm-db-haystack/) |
| 20 | +[](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/ibm_db.yml) |
| 21 | + |
| 22 | +----- |
| 23 | + |
| 24 | +**Table of Contents** |
| 25 | + |
| 26 | +- IBM Db2 Document Store for Haystack |
| 27 | + - [Installation](#installation) |
| 28 | + - [Usage](#usage) |
| 29 | + - [License](#license) |
| 30 | + |
| 31 | +## Installation |
| 32 | +IBM Db2 (version 12.1.2 and later) provides a native `VECTOR` data type that adds support for vector similarity search directly inside the database, allowing Db2 to act as a fully featured vector store while keeping documents, embeddings, and metadata within your existing enterprise database. |
| 33 | + |
| 34 | +For more information on Db2 vector capabilities, visit the [IBM Db2 product page](https://www.ibm.com/products/db2). |
| 35 | + |
| 36 | +Use `pip` to install `ibm-db-haystack`: |
| 37 | + |
| 38 | +```bash |
| 39 | +pip install ibm-db-haystack |
| 40 | +``` |
| 41 | + |
| 42 | +## Usage |
| 43 | +Set your IBM Db2 credentials as environment variables: |
| 44 | + |
| 45 | +```bash |
| 46 | +export DB2_USERNAME="db2inst1" |
| 47 | +export DB2_PASSWORD="your_password" |
| 48 | +``` |
| 49 | + |
| 50 | +Once installed, initialize IBMDb2DocumentStore. |
| 51 | + |
| 52 | +```python |
| 53 | +from haystack.utils import Secret |
| 54 | +from haystack_integrations.document_stores.ibm_db import IBMDb2DocumentStore |
| 55 | + |
| 56 | +document_store = IBMDb2DocumentStore( |
| 57 | + database="BLUDB", |
| 58 | + hostname="your-db2-host", |
| 59 | + port=50000, |
| 60 | + username=Secret.from_env_var("DB2_USERNAME"), |
| 61 | + password=Secret.from_env_var("DB2_PASSWORD"), |
| 62 | + table_name="haystack_docs", |
| 63 | + embedding_dim=768, |
| 64 | + distance_metric="COSINE", |
| 65 | +) |
| 66 | +``` |
| 67 | + |
| 68 | +Supported distance metrics are `COSINE`, `EUCLIDEAN`, and `MANHATTAN`. |
| 69 | + |
| 70 | +### Writing Documents to IBMDb2DocumentStore |
| 71 | +To write documents to `IBMDb2DocumentStore`, create an indexing pipeline. |
| 72 | + |
| 73 | +```python |
| 74 | +from haystack import Pipeline |
| 75 | +from haystack.components.converters import TextFileToDocument |
| 76 | +from haystack.components.writers import DocumentWriter |
| 77 | +from haystack.components.embedders import SentenceTransformersDocumentEmbedder |
| 78 | + |
| 79 | +indexing = Pipeline() |
| 80 | +indexing.add_component("converter", TextFileToDocument()) |
| 81 | +indexing.add_component("embedder", SentenceTransformersDocumentEmbedder()) |
| 82 | +indexing.add_component("writer", DocumentWriter(document_store)) |
| 83 | +indexing.connect("converter", "embedder") |
| 84 | +indexing.connect("embedder", "writer") |
| 85 | +indexing.run({"converter": {"sources": file_paths}}) |
| 86 | +``` |
| 87 | + |
| 88 | +### Retrieval from IBMDb2DocumentStore |
| 89 | +You can retrieve semantically similar documents to a given query using a simple pipeline that includes the `IBMDb2EmbeddingRetriever`. |
| 90 | + |
| 91 | +```python |
| 92 | +from haystack import Pipeline |
| 93 | +from haystack.components.embedders import SentenceTransformersTextEmbedder |
| 94 | +from haystack_integrations.components.retrievers.ibm_db import IBMDb2EmbeddingRetriever |
| 95 | + |
| 96 | +querying = Pipeline() |
| 97 | +querying.add_component("embedder", SentenceTransformersTextEmbedder()) |
| 98 | +querying.add_component("retriever", IBMDb2EmbeddingRetriever(document_store=document_store, top_k=3)) |
| 99 | +querying.connect("embedder.embedding", "retriever.query_embedding") |
| 100 | + |
| 101 | +results = querying.run({"embedder": {"text": "my query"}}) |
| 102 | +``` |
| 103 | + |
| 104 | +You can also combine vector similarity search with metadata filtering, including compound AND/OR conditions, executed in the same query as the vector search. |
| 105 | + |
| 106 | +```python |
| 107 | +from haystack_integrations.components.retrievers.ibm_db import IBMDb2EmbeddingRetriever |
| 108 | + |
| 109 | +retriever = IBMDb2EmbeddingRetriever(document_store=document_store, top_k=3) |
| 110 | + |
| 111 | +results = retriever.run( |
| 112 | + query_embedding=query_embedding, |
| 113 | + filters={ |
| 114 | + "operator": "AND", |
| 115 | + "conditions": [ |
| 116 | + {"field": "meta.category", "operator": "==", "value": "security"}, |
| 117 | + {"field": "meta.priority", "operator": "==", "value": "high"}, |
| 118 | + ], |
| 119 | + }, |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +## License |
| 124 | +`ibm-db-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. |
0 commit comments