LangChain BM25 and hybrid retrievers for SAP HANA Cloud
Project description
langchain-hana-retriever
LangChain BM25 and hybrid retrievers for SAP HANA Cloud.
SAP HANA Cloud doesn't have built-in BM25/full-text ranking. This package provides two LangChain-compatible retrievers that enable proper hybrid retrieval for RAG pipelines without requiring PAL.
How it works
HANABm25Retriever— Uses SQLLOCATEto fetch keyword-matching candidates from HANA, then scores them with BM25Okapi in Python.HANAHybridRetriever— Combines a HANA vector store (semantic search) with the BM25 retriever, merging results via Reciprocal Rank Fusion.
Installation
pip install langchain-hana-retriever
For hybrid retrieval (requires a vector store):
pip install "langchain-hana-retriever[hybrid]"
Usage
BM25 keyword retriever
import hdbcli.dbapi
from langchain_hana_retriever import HANABm25Retriever
connection = hdbcli.dbapi.connect(
address="your-host.hanacloud.ondemand.com",
port=443,
user="your_user",
password="your_password",
encrypt=True,
)
retriever = HANABm25Retriever(
connection=connection,
table_name="YOUR_TABLE",
content_column="VEC_TEXT", # column containing document text
metadata_columns=["SOURCE"], # optional metadata columns to return
k=10, # number of results
candidate_limit=50, # SQL LIMIT for initial candidate fetch
)
docs = retriever.invoke("your search query")
Hybrid retriever (vector + BM25)
from langchain_community.vectorstores import HanaDB
from langchain_openai import OpenAIEmbeddings
from langchain_hana_retriever import HANABm25Retriever, HANAHybridRetriever
# Set up vector store
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector_store = HanaDB(
connection=connection,
embedding=embeddings,
table_name="YOUR_TABLE",
)
# Set up keyword retriever
keyword_retriever = HANABm25Retriever(
connection=connection,
table_name="YOUR_TABLE",
k=10,
)
# Combine both
hybrid = HANAHybridRetriever(
vector_store=vector_store,
keyword_retriever=keyword_retriever,
alpha=0.5, # 0 = keyword only, 1 = vector only
k=10,
)
docs = hybrid.invoke("your search query")
Parameters
HANABm25Retriever
| Parameter | Type | Default | Description |
|---|---|---|---|
connection |
hdbcli.dbapi.Connection |
required | HANA database connection |
table_name |
str |
required | Table to search |
content_column |
str |
"VEC_TEXT" |
Column containing document text |
metadata_columns |
list[str] |
[] |
Additional columns to include in metadata |
k |
int |
10 |
Number of results to return |
candidate_limit |
int |
50 |
SQL LIMIT for candidate fetching |
max_tokens_in_query |
int |
5 |
Max query tokens sent to SQL WHERE clause |
HANAHybridRetriever
| Parameter | Type | Default | Description |
|---|---|---|---|
vector_store |
HanaDB |
required | HANA vector store for semantic search |
keyword_retriever |
HANABm25Retriever |
required | BM25 retriever for keyword search |
alpha |
float |
0.5 |
Balance between vector (1.0) and keyword (0.0) |
k |
int |
10 |
Number of results to return |
Development
git clone https://github.com/stubborncoder/langchain-hana-retriever.git
cd langchain-hana-retriever
pip install -e ".[dev]"
# Run unit tests
pytest tests/test_utils.py tests/test_bm25.py tests/test_hybrid.py -v
# Run integration tests (requires HANA credentials in .env)
pytest tests/test_integration.py -v
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_hana_retriever-0.1.0.tar.gz.
File metadata
- Download URL: langchain_hana_retriever-0.1.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1384aeca8322b89d14fa330650645ab3f51c03c0b5633a68fef71f52ffaa9ead
|
|
| MD5 |
3040971618f7def21e2d1884e159bbc4
|
|
| BLAKE2b-256 |
7839e04da57558597463f1dfba7b0f823cfb45ba7d1a4aa1d60a2fb823d3f752
|
File details
Details for the file langchain_hana_retriever-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_hana_retriever-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8aa9aa7a17977ad3ee80c9c0892496018a8dc7e30c64dbc529e78d1ef5034524
|
|
| MD5 |
8bad1c8aaeeee0fe6b6291f8d82c45c7
|
|
| BLAKE2b-256 |
27a0848e507d29a6fdc0676f48d247781834bf5941f0fd659e2921db1e8786cb
|