Skip to content

Commit

Permalink
examples: RAG Chat UI (#3108)
Browse files Browse the repository at this point in the history
This PR introduces a UI for a chat bot using the RAG service we
developed previously.

Testing done: ran locally

---------

Signed-off-by: Gabriel Georgiev <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
gabrielgeorgiev1 and pre-commit-ci[bot] authored Feb 14, 2024
1 parent 459d6fc commit 5aa78f1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
13 changes: 13 additions & 0 deletions examples/rag-chat-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ Alternatively, you can use the Python script for easier access + string formatti
```
python question.py "INPUT-QUESTION-HERE"
```

### UI

To run the UI, you need to install `streamlit`:
```
pip install streamlit
```
Note that newer versions of `streamlit` require Python 3.8+.

Then, run the app:
```
streamlit run chat_app.py
```
29 changes: 29 additions & 0 deletions examples/rag-chat-api/chat_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2021-2024 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import streamlit as st
from question import get_api_response

st.title("VDK Demo Chat Bot")

# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])

# React to user input
if prompt := st.chat_input("What is up?"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})

response = get_api_response(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
14 changes: 7 additions & 7 deletions examples/rag-chat-api/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import requests


def question():
if len(sys.argv) != 2:
print("Wrap your question in quotation marks")

def get_api_response(question):
headers = {"Content-Type": "application/json"}
data = {"question": sys.argv[1]}
data = {"question": question}
res = requests.post("http://127.0.0.1:8000/question/", headers=headers, json=data)

print(res.text.replace("\\n", "\n").replace("\\t", "\t"))
return res.text.replace("\\n", "\n").replace("\\t", "\t")


if __name__ == "__main__":
question()
if len(sys.argv) != 2:
print("Wrap your question in quotation marks")

print(get_api_response(sys.argv[1]))

0 comments on commit 5aa78f1

Please sign in to comment.