import streamlit as st import requests import json import sqlite3 import datetime import yaml chat_history = [] #"st.session_state object", st.session_state with open('config.yaml', 'r') as file: config = yaml.safe_load(file) docchat_engine_url = config['docchat-engine']['url'] conn = sqlite3.connect('message.db') c = conn.cursor() #Get unique id from database and set the session unique id if 'session_unique_id' not in st.session_state: #Insert timestamp of the request, the unique id is autogenerated at the table level. c.execute("INSERT INTO SESSION (TIMESTAMP) VALUES('{timestamp_value}')".format(timestamp_value=datetime.datetime.now())) conn.commit() #Fetch the autogenerated uniqueid from table (autoincremented number) c.execute("SELECT UNIQUE_ID FROM SESSION ORDER BY UNIQUE_ID DESC LIMIT 1") for tmp_select_return_msg in c.fetchone(): unique_id_from_db = tmp_select_return_msg st.session_state['session_unique_id'] = unique_id_from_db def call_web_service(message): response = requests.post('{value_url}/chat/'.format(value_url=docchat_engine_url), json={'text': message}) return response.json() st.title('Chat Interface') user_input = st.text_input("Type your message here:", key="user_input") if st.button("Send"): if user_input: response = call_web_service(user_input) chat_history.append("DocChat :" + " " + response["message"]) chat_history.append("You :" + " " + user_input) for message in chat_history: format_sql_insert = 'INSERT INTO messages (message, unique_id) VALUES ("{value}","{uniqueid}")'.format(value=message,uniqueid=st.session_state['session_unique_id']) print("DEBUG: " + format_sql_insert) c.execute(format_sql_insert) conn.commit() c.execute("Select message from messages where unique_id = {value_unique_id} order by id desc".format(value_unique_id=st.session_state['session_unique_id'])) msg_from_db = c.fetchall() for op_message in msg_from_db: st.write(op_message[0]) if 'button_clicked' not in st.session_state: st.session_state.button_clicked = False if st.sidebar.button('Load Chat'): st.session_state.button_clicked = True if st.session_state.button_clicked: response = requests.post('{value_url}/loadvector/'.format(value_url=docchat_engine_url), json={"text": "_chat"}) st.write('Context Loaded')