Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: RAG Chat UI #3108

Merged
merged 7 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```
31 changes: 31 additions & 0 deletions examples/rag-chat-api/chat_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021-2024 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0

import requests

Check notice on line 4 in examples/rag-chat-api/chat_app.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

examples/rag-chat-api/chat_app.py#L4

'requests' imported but unused (F401)

Check warning on line 4 in examples/rag-chat-api/chat_app.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

examples/rag-chat-api/chat_app.py#L4

Unused import requests
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})
15 changes: 8 additions & 7 deletions examples/rag-chat-api/question.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# Copyright 2021-2024 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0

import sys

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]))
Loading