Skip to content

Commit

Permalink
Add StartConversation and Chat REST API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lmitlaender committed Nov 8, 2023
1 parent 6a89acf commit 7fb21a4
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHAT_URL=host.docker.internal
CHAT_PORT=8001
143 changes: 143 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

.DS_Store
# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
.vscode/
data/
logs/
qdrant_storage/
tmp*/
htmlcov/
vector_db/

# Not needed yml files
docker-compose-hub.yml

#IDEA Files
.idea/
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.10

WORKDIR /app

COPY . /app
COPY ./requirements.txt /app

RUN apt-get update && apt-get install -y \
build-essential \
Expand All @@ -13,6 +13,8 @@ RUN apt-get update && apt-get install -y \

RUN pip3 install -r requirements.txt

COPY . /app

# Expose the ports that your app uses
EXPOSE 8501

Expand Down
63 changes: 58 additions & 5 deletions frontend/streamlit_gui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
"""The main gui."""
import streamlit as st
from loguru import logger
import dotenv
import requests
import os

dotenv.load_dotenv()

CHAT_URL = os.getenv("CHAT_URL")
CHAT_PORT = os.getenv("CHAT_PORT")

# Constants
PDF_FILE_TYPE = "pdf"
Expand All @@ -16,8 +24,49 @@
# Create title
st.title("💬 OneCX Chatbot")

def get_conversation_id(conv_type = "Q_AND_A", return_sys_message = False):
if "conversation_id" in st.session_state:
return st.session_state.conversation_id

body = {"conversation_type": conv_type}

response = requests.post(url="http://" + CHAT_URL + ":" + CHAT_PORT + "/startConversation", json=body)
response.raise_for_status()
response_json = response.json()
st.session_state.conversation_id = response_json["conversationId"]
logger.info(f"Started conversation: {st.session_state.conversation_id}")

if return_sys_message:
return st.session_state.conversation_id, response_json["history"][0]["message"]
return st.session_state.conversation_id

def send_chat(message):
try:
conversation_id = get_conversation_id()

body = {
"conversationId": conversation_id,
"correlationId": "StreamlitUI",
"message": message,
"type": "user",
"creationDate": 0
}

response = requests.post(url="http://" + CHAT_URL + ":" + CHAT_PORT + "/chat", json=body)
response.raise_for_status()
response_json = response.json()

return True, response_json["message"]
except Exception as e:
return False, e


if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "Wie kann ich dir helfen?"}]
try:
conversation_id, first_message = get_conversation_id(return_sys_message=True)
st.session_state["messages"] = [{"role": "assistant", "content": first_message}]
except:
st.session_state["messages"] = [{"role": "assistant", "content": "Wie kann ich dir helfen?"}]

for msg in st.session_state.messages:
#if len(st.session_state.messages) == 1:
Expand All @@ -27,8 +76,12 @@
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
#send request
#
success, response = send_chat(prompt)

#check response
msg = "BOT RESPONSE"
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)

st.session_state.messages.append({"role": "assistant", "content": response})
if success:
st.chat_message("assistant").write(response)
else:
st.chat_message("assistant").error(response)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
loguru==0.7.0
streamlit==1.26.0
python-dotenv==1.0.0

0 comments on commit 7fb21a4

Please sign in to comment.