Skip to content

Commit

Permalink
Chatbot improvements (elastic#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg authored Feb 9, 2024
1 parent 39a8ef3 commit d4f3ca4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
10 changes: 7 additions & 3 deletions example-apps/chatbot-rag-app/api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ def ask_question(question, session_id):
condense_question_prompt = render_template(
'condense_question_prompt.txt', question=question,
chat_history=chat_history.messages)
question = get_llm().invoke(condense_question_prompt).content
condensed_question = get_llm().invoke(condense_question_prompt).content
else:
condensed_question = question

current_app.logger.debug('Condensed question: %s', condensed_question)
current_app.logger.debug('Question: %s', question)

docs = store.as_retriever().invoke(question)
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'])
yield f'data: {SOURCE_TAG} {json.dumps(doc_source)}\n\n'

qa_prompt = render_template('rag_prompt.txt', question=question, docs=docs)
qa_prompt = render_template('rag_prompt.txt', question=question, docs=docs,
chat_history=chat_history.messages)

answer = ''
for chunk in get_llm().stream(qa_prompt):
Expand Down
7 changes: 6 additions & 1 deletion example-apps/chatbot-rag-app/api/templates/rag_prompt.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Use the following passages to answer the user's question.
Use the following passages and chat history to answer the user's question.
Each passage has a NAME which is the title of the document. After your answer, leave a blank line and then give the source name of the passages you answered from. Put them in a comma separated list, prefixed with SOURCES:.

Example:
Expand All @@ -22,5 +22,10 @@ PASSAGE:

{% endfor -%}
----
Chat history:
{% for dialogue_turn in chat_history -%}
{% if dialogue_turn.type == 'human' %}Question: {{ dialogue_turn.content }}{% elif dialogue_turn.type == 'ai' %}Response: {{ dialogue_turn.content }}{% endif %}
{% endfor -%}

Question: {{ question }}
Response:
3 changes: 3 additions & 0 deletions example-apps/chatbot-rag-app/data/index_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def main():
es_connection=elasticsearch_client,
index_name=INDEX,
strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(model_id=ELSER_MODEL),
bulk_kwargs={
'request_timeout': 60,
},
)


Expand Down
6 changes: 3 additions & 3 deletions example-apps/chatbot-rag-app/frontend/src/store/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export const thunkActions = {
dispatch(
actions.updateMessage({
id: conversationId,
content: message.replace(/SOURCES: (.+)+/, ''),
content: message.replace(/SOURCES:(.+)*/, ''),
})
)
}
Expand Down Expand Up @@ -300,8 +300,8 @@ const parseSources = (
message: string
) => {
message = message.replaceAll("\"", "");
const match = message.match(/SOURCES: (.+)+/)
if (match) {
const match = message.match(/SOURCES:(.+)*/)
if (match && match[1]) {
return match[1].split(',').map(element => {
return element.trim();
});
Expand Down

0 comments on commit d4f3ca4

Please sign in to comment.