import json import os import streamlit as st import streamlit.components.v1 as components from chain import get_chain from chat_history import insert_chat_history, insert_chat_history_articles from connection import connect from css import load_css from langchain.callbacks import get_openai_callback from message import Message st.set_page_config(layout="wide") st.title("Sorbobot - Le futur de la recherche scientifique interactive") chat_column, doc_column = st.columns([2, 1]) conn = connect() def initialize_session_state(): if "history" not in st.session_state: st.session_state.history = [] if "token_count" not in st.session_state: st.session_state.token_count = 0 if "conversation" not in st.session_state: st.session_state.conversation = get_chain(conn) def send_message_callback(): with st.spinner("Wait for it..."): with get_openai_callback() as cb: human_prompt = st.session_state.human_prompt.strip() if len(human_prompt) == 0: return llm_response = st.session_state.conversation(human_prompt) st.session_state.history.append(Message("human", human_prompt)) st.session_state.history.append( Message( "ai", llm_response["answer"], documents=llm_response["source_documents"], ) ) st.session_state.token_count += cb.total_tokens if os.environ.get("ENVIRONMENT") == "dev": history_id = insert_chat_history( conn, human_prompt, llm_response["answer"] ) insert_chat_history_articles( conn, history_id, llm_response["source_documents"] ) def exemple_message_callback_button(args): st.session_state.human_prompt = args send_message_callback() st.session_state.human_prompt = "" def clear_history(): st.session_state.history.clear() st.session_state.token_count = 0 st.session_state.conversation.memory.clear() load_css() initialize_session_state() exemples = [ "Who has published influential research on quantum computing?", "List any prominent authors in the field of artificial intelligence ethics?", "Who are the leading experts on climate change mitigation strategies?", ] with chat_column: chat_placeholder = st.container() prompt_placeholder = st.form("chat-form", clear_on_submit=True) information_placeholder = st.container() with chat_placeholder: div = f"""