Skip to content

Commit cf0ec3e

Browse files
precommit
Signed-off-by: Adrian Cole <[email protected]>
1 parent e548e5e commit cf0ec3e

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
from elasticsearch import Elasticsearch
55
from flask import current_app, render_template, stream_with_context
6-
from langchain_elasticsearch import ElasticsearchChatMessageHistory, ElasticsearchStore, SparseVectorStrategy
6+
from langchain_elasticsearch import (
7+
ElasticsearchChatMessageHistory,
8+
ElasticsearchStore,
9+
SparseVectorStrategy,
10+
)
711
from langchain_openai import ChatOpenAI
812
from llm_integrations import get_llm
913
from elasticsearch_client import (
@@ -12,7 +16,9 @@
1216
)
1317

1418
INDEX = os.getenv("ES_INDEX", "workplace-app-docs")
15-
INDEX_CHAT_HISTORY = os.getenv("ES_INDEX_CHAT_HISTORY", "workplace-app-docs-chat-history")
19+
INDEX_CHAT_HISTORY = os.getenv(
20+
"ES_INDEX_CHAT_HISTORY", "workplace-app-docs-chat-history"
21+
)
1622
ELSER_MODEL = os.getenv("ELSER_MODEL", ".elser_model_2")
1723
SESSION_ID_TAG = "[SESSION_ID]"
1824
SOURCE_TAG = "[SOURCE]"
@@ -26,12 +32,15 @@
2632

2733
llm = get_llm()
2834

35+
2936
@stream_with_context
3037
def ask_question(question, session_id):
3138
yield f"data: {SESSION_ID_TAG} {session_id}\n\n"
3239
current_app.logger.debug("Chat session ID: %s", session_id)
3340

34-
chat_history = get_elasticsearch_chat_message_history(INDEX_CHAT_HISTORY, session_id)
41+
chat_history = get_elasticsearch_chat_message_history(
42+
INDEX_CHAT_HISTORY, session_id
43+
)
3544

3645
if len(chat_history.messages) > 0:
3746
# create a condensed question
@@ -50,7 +59,9 @@ def ask_question(question, session_id):
5059
docs = store.as_retriever().invoke(condensed_question)
5160
for doc in docs:
5261
doc_source = {**doc.metadata, "page_content": doc.page_content}
53-
current_app.logger.debug("Retrieved document passage from: %s", doc.metadata["name"])
62+
current_app.logger.debug(
63+
"Retrieved document passage from: %s", doc.metadata["name"]
64+
)
5465
yield f"data: {SOURCE_TAG} {json.dumps(doc_source)}\n\n"
5566

5667
qa_prompt = render_template(
@@ -62,7 +73,9 @@ def ask_question(question, session_id):
6273

6374
answer = ""
6475
for chunk in llm.stream(qa_prompt):
65-
content = chunk.content.replace("\n", " ") # the stream can get messed up with newlines
76+
content = chunk.content.replace(
77+
"\n", " "
78+
) # the stream can get messed up with newlines
6679
yield f"data: {content}\n\n"
6780
answer += chunk.content
6881

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
if ELASTICSEARCH_URL:
1313
elasticsearch_client = Elasticsearch(
1414
hosts=[ELASTICSEARCH_URL],
15-
basic_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD)
15+
basic_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
1616
)
1717
elif ELASTIC_CLOUD_ID:
1818
elasticsearch_client = Elasticsearch(

example-apps/chatbot-rag-app/data/index_data.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
if ELASTICSEARCH_URL:
2222
elasticsearch_client = Elasticsearch(
2323
hosts=[ELASTICSEARCH_URL],
24-
basic_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD)
24+
basic_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
2525
)
2626
elif ELASTIC_CLOUD_ID:
2727
elasticsearch_client = Elasticsearch(
@@ -39,16 +39,22 @@ def install_elser():
3939
print(f'"{ELSER_MODEL}" model is available')
4040
except NotFoundError:
4141
print(f'"{ELSER_MODEL}" model not available, downloading it now')
42-
elasticsearch_client.ml.put_trained_model(model_id=ELSER_MODEL, input={"field_names": ["text_field"]})
42+
elasticsearch_client.ml.put_trained_model(
43+
model_id=ELSER_MODEL, input={"field_names": ["text_field"]}
44+
)
4345
while True:
44-
status = elasticsearch_client.ml.get_trained_models(model_id=ELSER_MODEL, include="definition_status")
46+
status = elasticsearch_client.ml.get_trained_models(
47+
model_id=ELSER_MODEL, include="definition_status"
48+
)
4549
if status["trained_model_configs"][0]["fully_defined"]:
4650
# model is ready
4751
break
4852
time.sleep(1)
4953

5054
print("Model downloaded, starting deployment")
51-
elasticsearch_client.ml.start_trained_model_deployment(model_id=ELSER_MODEL, wait_for="fully_allocated")
55+
elasticsearch_client.ml.start_trained_model_deployment(
56+
model_id=ELSER_MODEL, wait_for="fully_allocated"
57+
)
5258

5359

5460
def main():
@@ -69,7 +75,9 @@ def main():
6975

7076
print(f"Loaded {len(workplace_docs)} documents")
7177

72-
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(chunk_size=512, chunk_overlap=256)
78+
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
79+
chunk_size=512, chunk_overlap=256
80+
)
7381

7482
docs = text_splitter.transform_documents(workplace_docs)
7583

0 commit comments

Comments
 (0)