From 8054bcac39121ac3297608ae4d68f07fe78ce879 Mon Sep 17 00:00:00 2001 From: Dai Sugimori Date: Sat, 23 Nov 2024 17:06:51 +0900 Subject: [PATCH 1/2] Add support for semantic search with the dense vector --- example-apps/chatbot-rag-app/api/chat.py | 38 +++++++++++++++++------- example-apps/chatbot-rag-app/env.example | 9 ++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/example-apps/chatbot-rag-app/api/chat.py b/example-apps/chatbot-rag-app/api/chat.py index 54380010..c03b00a4 100644 --- a/example-apps/chatbot-rag-app/api/chat.py +++ b/example-apps/chatbot-rag-app/api/chat.py @@ -1,4 +1,4 @@ -from langchain_elasticsearch import ElasticsearchStore, SparseVectorStrategy +from langchain_elasticsearch import ElasticsearchStore, DenseVectorStrategy, SparseVectorStrategy from llm_integrations import get_llm from elasticsearch_client import ( elasticsearch_client, @@ -12,16 +12,33 @@ INDEX_CHAT_HISTORY = os.getenv( "ES_INDEX_CHAT_HISTORY", "workplace-app-docs-chat-history" ) -ELSER_MODEL = os.getenv("ELSER_MODEL", ".elser_model_2") +MODEL_ID = os.getenv("ES_MODEL_ID", ".elser_model_2") +STRATEGY_TYPE = os.getenv("ES_STRATEGY_TYPE", "sparse") +VECTOR_FIELD = os.getenv("ES_VECTOR_FIELD", "vector") +QUERY_FIELD = os.getenv("ES_QUERY_FIELD", "text") + SESSION_ID_TAG = "[SESSION_ID]" SOURCE_TAG = "[SOURCE]" DONE_TAG = "[DONE]" -store = ElasticsearchStore( - es_connection=elasticsearch_client, - index_name=INDEX, - strategy=SparseVectorStrategy(model_id=ELSER_MODEL), -) +if STRATEGY_TYPE == "sparse": + strategy = SparseVectorStrategy(model_id=MODEL_ID) + store = ElasticsearchStore( + es_connection=elasticsearch_client, + index_name=INDEX, + strategy=strategy, + ) +elif STRATEGY_TYPE == "dense": + strategy = DenseVectorStrategy(model_id=MODEL_ID, hybrid=True) + store = ElasticsearchStore( + es_connection=elasticsearch_client, + index_name=INDEX, + vector_query_field=VECTOR_FIELD, + query_field=QUERY_FIELD, + strategy=strategy, + ) +else: + raise ValueError(f"Invalid strategy type: {STRATEGY_TYPE}") @stream_with_context @@ -50,9 +67,10 @@ def ask_question(question, session_id): docs = store.as_retriever().invoke(condensed_question) for doc in docs: doc_source = {**doc.metadata, "page_content": doc.page_content} - current_app.logger.debug( - "Retrieved document passage from: %s", doc.metadata["name"] - ) + if "name" in doc.metadata: + current_app.logger.debug( + "Retrieved document passage from: %s", doc.metadata["name"] + ) yield f"data: {SOURCE_TAG} {json.dumps(doc_source)}\n\n" qa_prompt = render_template( diff --git a/example-apps/chatbot-rag-app/env.example b/example-apps/chatbot-rag-app/env.example index 2dc651cc..5c6c5b0e 100644 --- a/example-apps/chatbot-rag-app/env.example +++ b/example-apps/chatbot-rag-app/env.example @@ -11,6 +11,15 @@ ELASTIC_API_KEY= ES_INDEX=workplace-app-docs ES_INDEX_CHAT_HISTORY=workplace-app-docs-chat-history +# For sparse vector (e.g. ELSER) +ES_STRATEGY_TYPE=sparse +ES_MODEL_ID=.elser_model_2 +# For dense vector (e.g. E5) +# ES_STRATEGY_TYPE=dense +# ES_MODEL_ID=.multilingual-e5-small_linux-x86_64 +# ES_VECTOR_FIELD=text_semantic.inference.chunks.embeddings +# ES_QUERY_FIELD=text + # Uncomment and complete if you want to use OpenAI # LLM_TYPE=openai # OPENAI_API_KEY= From a205ec91c5e93cc5c042004f9ddb93f906d5ae44 Mon Sep 17 00:00:00 2001 From: Dai Sugimori Date: Sat, 23 Nov 2024 17:22:12 +0900 Subject: [PATCH 2/2] apply formatter --- example-apps/chatbot-rag-app/api/chat.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/example-apps/chatbot-rag-app/api/chat.py b/example-apps/chatbot-rag-app/api/chat.py index c03b00a4..c7923b0d 100644 --- a/example-apps/chatbot-rag-app/api/chat.py +++ b/example-apps/chatbot-rag-app/api/chat.py @@ -1,4 +1,8 @@ -from langchain_elasticsearch import ElasticsearchStore, DenseVectorStrategy, SparseVectorStrategy +from langchain_elasticsearch import ( + ElasticsearchStore, + DenseVectorStrategy, + SparseVectorStrategy, +) from llm_integrations import get_llm from elasticsearch_client import ( elasticsearch_client,