Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
379c9f4
feat(crewai-tools): add db2 search tool
PawanThakurIBM May 21, 2026
02a44b9
refactor(crewai-tools): improve db2 search tool implementation
PawanThakurIBM May 21, 2026
54f0006
feat(tools): improve DB2VectorSearchTool validation, security, and co…
PawanThakurIBM May 21, 2026
d2ac4c8
docs: add DB2SearchTool documentation
PawanThakurIBM May 25, 2026
fccb943
feat: add DB2 search tool
PawanThakurIBM May 25, 2026
61d668b
Merge branch 'main' into feat/db2-search-tool
PawanThakurIBM May 26, 2026
a53a27a
docs: update DB2SearchTool documentation
PawanThakurIBM May 26, 2026
d14196e
fix: address CodeRabbit review feedback
PawanThakurIBM May 26, 2026
0e1b992
fix: validate non-empty filter_by in DB2ToolSchema
PawanThakurIBM May 26, 2026
60f9dc1
chore: trigger CodeRabbit re-review
PawanThakurIBM May 26, 2026
468149e
feat: fortify DB2 tool; fixed JSON response shape, added input guards…
PawanThakurIBM May 26, 2026
9655947
Merge branch 'main' into feat/db2-search-tool
priyanshu-krishnan1 Jun 2, 2026
f95d91d
refactor(db2): replace DB2Config with connection_string field
GeetikaChughIBM Jul 6, 2026
eac9fa6
refactor(db2): remove dead _setup_db2 validator and importlib import
GeetikaChughIBM Jul 6, 2026
4770f20
refactor(db2): remove dead guard in _connect as _disconnect() is cal…
GeetikaChughIBM Jul 6, 2026
1123815
fix(db2): tighten _validate_identifier regex. Old regex allowed leadi…
GeetikaChughIBM Jul 6, 2026
ab89454
fix(db2): replace __import__ with importlib.import_module in _generat…
GeetikaChughIBM Jul 6, 2026
9c7026e
perf(db2): cache OpenAI client in _openai_client to avoid re-instanti…
GeetikaChughIBM Jul 6, 2026
d3de978
docs(db2): clarify tool description to mention embedding fallback
GeetikaChughIBM Jul 6, 2026
aa0cec1
docs(db2): update README supported features to clarify embedding beha…
GeetikaChughIBM Jul 6, 2026
0ff4087
updated both code examples to use the correct import path and public …
GeetikaChughIBM Jul 6, 2026
c598d99
feat(crewai-tools): add db2 search tool
PawanThakurIBM May 21, 2026
d4c1bdd
refactor(crewai-tools): improve db2 search tool implementation
PawanThakurIBM May 21, 2026
0ec84a5
feat(tools): improve DB2VectorSearchTool validation, security, and co…
PawanThakurIBM May 21, 2026
0afe85f
docs: add DB2SearchTool documentation
PawanThakurIBM May 25, 2026
8dd6b26
feat: add DB2 search tool
PawanThakurIBM May 25, 2026
03ceb52
docs: update DB2SearchTool documentation
PawanThakurIBM May 26, 2026
d62c7d4
fix: address CodeRabbit review feedback
PawanThakurIBM May 26, 2026
c7533fe
fix: validate non-empty filter_by in DB2ToolSchema
PawanThakurIBM May 26, 2026
df32ebb
chore: trigger CodeRabbit re-review
PawanThakurIBM May 26, 2026
b22fe41
feat: fortify DB2 tool; fixed JSON response shape, added input guards…
PawanThakurIBM May 26, 2026
bf412c7
Merge branch 'crewAIInc:main' into feat/db2-search-tool
priyanshu-krishnan1 Jul 13, 2026
48d83eb
Merge remote-tracking branch 'prev-origin/feat/db2-search-tool' into …
GeetikaChughIBM Jul 13, 2026
2583350
fix(db2): address ruff and mypy linter errors
GeetikaChughIBM Jul 13, 2026
f468692
style(db2): apply ruff format to db2_search_tool.py
GeetikaChughIBM Jul 13, 2026
471cb00
Merge branch 'crewAIInc:main' into feat/db2-search-tool
GeetikaChughIBM Jul 15, 2026
a3c3c04
Merge branch 'main' into feat/db2-search-tool
lorenzejay Jul 21, 2026
77f4efc
fix(db2-search-tool): address PR review comments
Jul 22, 2026
f77b609
Merge pull request #2 from PawanThakurIBM/feat/db2-search-tool-review…
PawanThakurIBM Jul 22, 2026
0cf1309
fix(db2-search-tool): resolve default and string db2 package imports …
PawanThakurIBM Jul 22, 2026
484c303
fix(db2-search-tool): export DB2VectorSearchTool and DB2ToolSchema fr…
PawanThakurIBM Jul 22, 2026
4a44a21
docs(db2-search-tool): fix installation command and import path in RE…
PawanThakurIBM Jul 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@
"edge/en/tools/database-data/qdrantvectorsearchtool",
"edge/en/tools/database-data/weaviatevectorsearchtool",
"edge/en/tools/database-data/mongodbvectorsearchtool",
"edge/en/tools/database-data/singlestoresearchtool"
"edge/en/tools/database-data/singlestoresearchtool",
"edge/en/tools/database-data/db2searchtool"
]
},
{
Expand Down
211 changes: 211 additions & 0 deletions docs/edge/en/tools/database-data/db2searchtool.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
---
title: Db2 Vector Search Tool
description: Semantic vector search for CrewAI agents using IBM Db2 native VECTOR_DISTANCE capabilities.
icon: database
mode: "wide"
---

# `DB2VectorSearchTool`

## Description

Perform semantic vector similarity searches against IBM Db2 tables using the native `VECTOR_DISTANCE` function.
Supports configurable distance metrics, OpenAI or custom embeddings, metadata filtering, and result shaping.

## Installation

```bash
pip install ibm_db openai
```

Or with uv:

```bash
uv add ibm_db openai
```

## Environment Variables

```bash
OPENAI_API_KEY=your_openai_key # Required when using default OpenAI embeddings
DB2_CONNECTION_STRING=DATABASE=TESTDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;
```

## Basic Usage

```python
from crewai import Agent
from crewai_tools import DB2VectorSearchTool

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import still fails: DB2VectorSearchTool was added to crewai_tools.tools, but not the package-level crewai_tools/__init__.py. Please export it there and add a public-import test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this one — DB2VectorSearchTool was only exported from crewai_tools.tools, not from the top-level crewai_tools package, so from crewai_tools import DB2VectorSearchTool would fail at import. Both DB2VectorSearchTool and DB2ToolSchema are now in crewai_tools/init.py and all. Added test_public_import_from_crewai_tools to cover the package-level import explicitly. All 70 tests passing.


tool = DB2VectorSearchTool(
connection_string="DATABASE=TESTDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;",
table_name="documents",
vector_column="embedding",
)

agent = Agent(
role="Research Assistant",
goal="Find relevant information in documents",
tools=[tool],
)
```

## Full Semantic Search Workflow

```python
import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process
from crewai_tools import DB2VectorSearchTool

load_dotenv()

db2_tool = DB2VectorSearchTool(
connection_string=os.getenv("DB2_CONNECTION_STRING"),
table_name="documents",
vector_column="embedding",
return_columns=["content", "category"],
limit=3,
distance_metric="COSINE",
max_distance=0.35,
)

search_agent = Agent(
role="Senior Semantic Search Agent",
goal="Find and analyse documents based on semantic search",
backstory="You are an expert research assistant who can find relevant information using semantic search in a Db2 database.",
tools=[db2_tool],
verbose=True,
)

answer_agent = Agent(
role="Senior Answer Assistant",
goal="Generate answers based on retrieved context",
backstory="You are an expert assistant who generates answers from provided context.",
tools=[db2_tool],
verbose=True,
)

search_task = Task(
description="""Search for relevant documents about {query}.
Include the relevant information found, vector distances, and returned fields.""",
agent=search_agent,
)

answer_task = Task(
description="Given the retrieved Db2 context, generate a final answer.",
agent=answer_agent,
)

crew = Crew(
agents=[search_agent, answer_agent],
tasks=[search_task, answer_task],
process=Process.sequential,
verbose=True,
)

result = crew.kickoff(inputs={"query": "What is the role of X in the document?"})
print(result)
```

## Tool Parameters

| Parameter | Type | Default | Description |
|---|---|---|---|
| `connection_string` | `str` | required | Db2 connection string. Format: `DATABASE=x;HOSTNAME=x;PORT=50000;PROTOCOL=TCPIP;UID=x;PWD=x;` |
| `table_name` | `str` | `"documents"` | Table to search. Supports `schema.table` notation. |
| `vector_column` | `str` | `"embedding"` | Column storing the vector embeddings. |
| `embedding_model` | `str` | `"text-embedding-3-large"` | OpenAI model used when no custom embedding function is provided. |
| `return_columns` | `list[str]` | `["content"]` | Columns to include in each result. Must contain at least one entry. |
| `limit` | `int` | `3` | Maximum number of results (1–100). |
| `distance_metric` | `str` | `"COSINE"` | Db2 distance metric. See supported values below. |
| `max_distance` | `float \| None` | `None` | Drop results whose distance exceeds this value. |
| `custom_embedding_fn` | `Callable[[str], list[float]] \| None` | `None` | Custom embedding function. Overrides OpenAI when provided. |

## Supported Distance Metrics

The following values map directly to the Db2 `VECTOR_DISTANCE` function:

- `COSINE`
- `EUCLIDEAN`
- `EUCLIDEAN_SQUARED`
- `DOT`
- `HAMMING`
- `MANHATTAN`

Reference: [IBM Db2 VECTOR_DISTANCE documentation](https://www.ibm.com/docs/en/db2/12.1.x?topic=functions-vector-distance)

## Schema Parameters (per query)

| Parameter | Type | Required | Description |
|---|---|---|---|
| `query` | `str` | ✅ | The search query. |
| `filter_by` | `str \| None` | ❌ | Column name for metadata filtering. Must be paired with `filter_value`. |
| `filter_value` | `Any \| None` | ❌ | Value to filter on. Must be paired with `filter_by`. |

## Return Format

```json
{
"success": true,
"results": [
{
"distance": 0.1401,
"data": {
"content": "Document content here",
"category": "research"
}
}
]
}
```

On error:

```json
{
"success": false,
"error": "Description of what went wrong",
"error_type": "ExceptionClassName"
}
```

## Metadata Filtering

```python
result = db2_tool.run(
query="machine learning",
filter_by="category",
filter_value="research",
)
```

`filter_by` and `filter_value` must always be provided together. Providing only one raises a validation error.

## Custom Embeddings

Use any embedding model by supplying a `custom_embedding_fn`:

```python
from sentence_transformers import SentenceTransformer
from crewai_tools import DB2VectorSearchTool

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

def custom_embeddings(text: str) -> list[float]:
return model.encode(text).tolist()

tool = DB2VectorSearchTool(
connection_string="DATABASE=TESTDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;",
table_name="documents",
custom_embedding_fn=custom_embeddings,
)
```

When `custom_embedding_fn` is provided, `OPENAI_API_KEY` is not required.

## Security Features

- SQL identifier validation (table, column names must match `^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)?$`)
- Parameterised SQL queries — values never interpolated into SQL strings
- Distance metric whitelist — only valid Db2 metric names accepted
6 changes: 6 additions & 0 deletions lib/crewai-tools/src/crewai_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
from crewai_tools.tools.databricks_query_tool.databricks_query_tool import (
DatabricksQueryTool,
)
from crewai_tools.tools.db2_search_tool import (
DB2ToolSchema,
DB2VectorSearchTool,
)
from crewai_tools.tools.daytona_sandbox_tool import (
DaytonaExecTool,
DaytonaFileTool,
Expand Down Expand Up @@ -247,6 +251,8 @@
"CrewaiPlatformTools",
"DOCXSearchTool",
"DallETool",
"DB2ToolSchema",
"DB2VectorSearchTool",
"DatabricksQueryTool",
"DaytonaExecTool",
"DaytonaFileTool",
Expand Down
6 changes: 6 additions & 0 deletions lib/crewai-tools/src/crewai_tools/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
DaytonaFileTool,
DaytonaPythonTool,
)
from crewai_tools.tools.db2_search_tool import (
DB2ToolSchema,
DB2VectorSearchTool,
)
from crewai_tools.tools.directory_read_tool.directory_read_tool import (
DirectoryReadTool,
)
Expand Down Expand Up @@ -233,6 +237,8 @@
"DOCXSearchTool",
"DallETool",
"DatabricksQueryTool",
"DB2ToolSchema",
"DB2VectorSearchTool",
"DaytonaExecTool",
"DaytonaFileTool",
"DaytonaPythonTool",
Expand Down
91 changes: 91 additions & 0 deletions lib/crewai-tools/src/crewai_tools/tools/db2_search_tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# DB2 Vector Search Tool

IBM DB2 Vector Search Tool for CrewAI.

Supports:

- IBM DB2 native VECTOR search
- OpenAI embeddings
- Custom embedding functions
- Metadata filtering
- Runtime dynamic imports
- Standardized CrewAI tool architecture

---

# Installation

```bash
uv add ibm_db openai
```

---

# Environment Variables

```env
OPENAI_API_KEY=your_openai_key

DB2_CONNECTION_STRING=DATABASE=TESTDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;
```

---

# Example Usage

```python
from crewai_tools import DB2VectorSearchTool

tool = DB2VectorSearchTool(
connection_string="DATABASE=TESTDB;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;UID=db2user;PWD=password;",
table_name="documents",
)

result = tool.run(
query="What is machine learning?",
)

print(result)
```

---

# Example With Metadata Filtering

```python
result = tool.run(
query="AI papers",
filter_by="category",
filter_value="AI",
)
```

---

# Supported Features

- DB2 VECTOR datatype
- VECTOR_DISTANCE search
- COSINE similarity
- Metadata filtering
- Uses a custom embedding function if supplied, otherwise OpenAI embeddings

---

# Architecture

This tool follows the same architecture as:

- QdrantVectorSearchTool
- WeaviateVectorSearchTool

Responsibilities:

- Generate query embeddings
- Perform vector similarity search
- Apply optional metadata filters
- Return normalized JSON results

This tool is retrieval-only.

Document ingestion should be handled separately.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from crewai_tools.tools.db2_search_tool.db2_search_tool import (
DB2ToolSchema,
DB2VectorSearchTool,
)


__all__ = [
"DB2ToolSchema",
"DB2VectorSearchTool",
]
Loading