Skip to content

Commit 1866ad7

Browse files
leslieluyupre-commit-ci[bot]xiguiw
authored
enable support for HNSW in dataprep and retriever for better performance (#1779)
* enable support vector-schema in dataprep redis Signed-off-by: leslieluyu <yu1.lu@intel.com> * enable support HNSW in retriever Signed-off-by: leslieluyu <yu1.lu@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: leslieluyu <yu1.lu@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: xiguiw <111278656+xiguiw@users.noreply.github.com>
1 parent 7aaabcd commit 1866ad7

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

comps/dataprep/src/integrations/redis.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
TIMEOUT_SECONDS = int(os.getenv("TIMEOUT_SECONDS", 600))
5353
SEARCH_BATCH_SIZE = int(os.getenv("SEARCH_BATCH_SIZE", 10))
5454

55+
# Vector Schema Configuration
56+
DEFAULT_VECTOR_SCHEMA = {"algorithm": "HNSW", "m": 16, "ef_construction": 200}
57+
VECTOR_SCHEMA = os.getenv("VECTOR_SCHEMA", json.dumps(DEFAULT_VECTOR_SCHEMA))
58+
5559
# Redis Connection Information
5660
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
5761
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
@@ -200,6 +204,12 @@ async def ingest_chunks_to_redis(file_name: str, chunks: List, embedder, index_n
200204

201205
# if data will be saved to a different index name than the default one
202206
ingest_index_name = index_name if index_name else INDEX_NAME
207+
# Parse vector schema
208+
try:
209+
vector_schema = json.loads(VECTOR_SCHEMA)
210+
except json.JSONDecodeError as e:
211+
logger.error(f"Invalid VECTOR_SCHEMA format: {e}")
212+
vector_schema = DEFAULT_VECTOR_SCHEMA
203213

204214
file_ids = []
205215
for i in range(0, num_chunks, batch_size):
@@ -214,6 +224,7 @@ async def ingest_chunks_to_redis(file_name: str, chunks: List, embedder, index_n
214224
embedding=embedder,
215225
index_name=ingest_index_name,
216226
redis_url=REDIS_URL,
227+
vector_schema=vector_schema,
217228
)
218229
if logflag:
219230
logger.info(f"[ redis ingest chunks ] keys: {keys}")

comps/retrievers/src/integrations/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def get_boolean_env_var(var_name, default_value=False):
4646
LOCAL_EMBEDDING_MODEL = os.getenv("LOCAL_EMBEDDING_MODEL", "maidalun1020/bce-embedding-base_v1")
4747
TEI_EMBEDDING_ENDPOINT = os.getenv("TEI_EMBEDDING_ENDPOINT", "")
4848
BRIDGE_TOWER_EMBEDDING = os.getenv("BRIDGE_TOWER_EMBEDDING", False)
49+
4950
HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN", "")
51+
ENABLE_SCHEMA = get_boolean_env_var("ENABLE_SCHEMA", False)
5052

5153
# OpenAI
5254
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

comps/retrievers/src/integrations/redis.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .config import (
2727
BRIDGE_TOWER_EMBEDDING,
2828
EMBED_MODEL,
29+
ENABLE_SCHEMA,
2930
HF_TOKEN,
3031
INDEX_NAME,
3132
INDEX_SCHEMA,
@@ -100,7 +101,13 @@ async def _initialize_client(self, index_name=INDEX_NAME) -> Redis:
100101
client = Redis(
101102
embedding=self.embeddings, index_name=index_name, index_schema=INDEX_SCHEMA, redis_url=REDIS_URL
102103
)
104+
elif ENABLE_SCHEMA:
105+
logger.info(f"generate redis instance with index_schema:{INDEX_SCHEMA}")
106+
client = Redis(
107+
embedding=self.embeddings, index_name=index_name, index_schema=INDEX_SCHEMA, redis_url=REDIS_URL
108+
)
103109
else:
110+
logger.info(f"generate redis instance with index_name:{INDEX_NAME}")
104111
client = Redis(embedding=self.embeddings, index_name=index_name, redis_url=REDIS_URL)
105112
return client
106113
except Exception as e:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
vector:
5+
- name: content_vector
6+
algorithm: HNSW
7+
datatype: FLOAT32
8+
dims: 768
9+
distance_metric: COSINE

0 commit comments

Comments
 (0)