Skip to content

Commit 8054bca

Browse files
committed
Add support for semantic search with the dense vector
1 parent 6225919 commit 8054bca

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

example-apps/chatbot-rag-app/api/chat.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from langchain_elasticsearch import ElasticsearchStore, SparseVectorStrategy
1+
from langchain_elasticsearch import ElasticsearchStore, DenseVectorStrategy, SparseVectorStrategy
22
from llm_integrations import get_llm
33
from elasticsearch_client import (
44
elasticsearch_client,
@@ -12,16 +12,33 @@
1212
INDEX_CHAT_HISTORY = os.getenv(
1313
"ES_INDEX_CHAT_HISTORY", "workplace-app-docs-chat-history"
1414
)
15-
ELSER_MODEL = os.getenv("ELSER_MODEL", ".elser_model_2")
15+
MODEL_ID = os.getenv("ES_MODEL_ID", ".elser_model_2")
16+
STRATEGY_TYPE = os.getenv("ES_STRATEGY_TYPE", "sparse")
17+
VECTOR_FIELD = os.getenv("ES_VECTOR_FIELD", "vector")
18+
QUERY_FIELD = os.getenv("ES_QUERY_FIELD", "text")
19+
1620
SESSION_ID_TAG = "[SESSION_ID]"
1721
SOURCE_TAG = "[SOURCE]"
1822
DONE_TAG = "[DONE]"
1923

20-
store = ElasticsearchStore(
21-
es_connection=elasticsearch_client,
22-
index_name=INDEX,
23-
strategy=SparseVectorStrategy(model_id=ELSER_MODEL),
24-
)
24+
if STRATEGY_TYPE == "sparse":
25+
strategy = SparseVectorStrategy(model_id=MODEL_ID)
26+
store = ElasticsearchStore(
27+
es_connection=elasticsearch_client,
28+
index_name=INDEX,
29+
strategy=strategy,
30+
)
31+
elif STRATEGY_TYPE == "dense":
32+
strategy = DenseVectorStrategy(model_id=MODEL_ID, hybrid=True)
33+
store = ElasticsearchStore(
34+
es_connection=elasticsearch_client,
35+
index_name=INDEX,
36+
vector_query_field=VECTOR_FIELD,
37+
query_field=QUERY_FIELD,
38+
strategy=strategy,
39+
)
40+
else:
41+
raise ValueError(f"Invalid strategy type: {STRATEGY_TYPE}")
2542

2643

2744
@stream_with_context
@@ -50,9 +67,10 @@ def ask_question(question, session_id):
5067
docs = store.as_retriever().invoke(condensed_question)
5168
for doc in docs:
5269
doc_source = {**doc.metadata, "page_content": doc.page_content}
53-
current_app.logger.debug(
54-
"Retrieved document passage from: %s", doc.metadata["name"]
55-
)
70+
if "name" in doc.metadata:
71+
current_app.logger.debug(
72+
"Retrieved document passage from: %s", doc.metadata["name"]
73+
)
5674
yield f"data: {SOURCE_TAG} {json.dumps(doc_source)}\n\n"
5775

5876
qa_prompt = render_template(

example-apps/chatbot-rag-app/env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ ELASTIC_API_KEY=
1111
ES_INDEX=workplace-app-docs
1212
ES_INDEX_CHAT_HISTORY=workplace-app-docs-chat-history
1313

14+
# For sparse vector (e.g. ELSER)
15+
ES_STRATEGY_TYPE=sparse
16+
ES_MODEL_ID=.elser_model_2
17+
# For dense vector (e.g. E5)
18+
# ES_STRATEGY_TYPE=dense
19+
# ES_MODEL_ID=.multilingual-e5-small_linux-x86_64
20+
# ES_VECTOR_FIELD=text_semantic.inference.chunks.embeddings
21+
# ES_QUERY_FIELD=text
22+
1423
# Uncomment and complete if you want to use OpenAI
1524
# LLM_TYPE=openai
1625
# OPENAI_API_KEY=

0 commit comments

Comments
 (0)