Spaces:
Sleeping
Sleeping
Update st_app.py
Browse files
st_app.py
CHANGED
|
@@ -3,24 +3,13 @@ import sys
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
-
from streamlit_pills import pills
|
| 7 |
-
from streamlit_feedback import streamlit_feedback
|
| 8 |
-
|
| 9 |
-
from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
|
| 10 |
|
| 11 |
from vectara_agentic.agent import AgentStatusType
|
| 12 |
-
from agent import initialize_agent
|
|
|
|
| 13 |
|
| 14 |
initial_prompt = "How can I help you today?"
|
| 15 |
|
| 16 |
-
def show_example_questions():
|
| 17 |
-
if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
|
| 18 |
-
selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
|
| 19 |
-
if selected_example:
|
| 20 |
-
st.session_state.ex_prompt = selected_example
|
| 21 |
-
st.session_state.first_turn = False
|
| 22 |
-
return True
|
| 23 |
-
return False
|
| 24 |
|
| 25 |
def format_log_msg(log_msg: str):
|
| 26 |
max_log_msg_size = 500
|
|
@@ -68,50 +57,19 @@ async def launch_bot():
|
|
| 68 |
cfg = get_agent_config()
|
| 69 |
st.session_state.cfg = cfg
|
| 70 |
st.session_state.ex_prompt = None
|
| 71 |
-
example_messages = [example.strip() for example in cfg.examples.split(";")] if cfg.examples else []
|
| 72 |
-
st.session_state.example_messages = [em for em in example_messages if len(em)>0]
|
| 73 |
reset()
|
| 74 |
|
| 75 |
cfg = st.session_state.cfg
|
|
|
|
| 76 |
|
| 77 |
# left side content
|
| 78 |
-
with st.sidebar:
|
| 79 |
-
image = Image.open('Vectara-logo.png')
|
| 80 |
-
st.image(image, width=175)
|
| 81 |
-
st.markdown(f"## {cfg['demo_welcome']}")
|
| 82 |
-
st.markdown(f"{cfg['demo_description']}")
|
| 83 |
-
|
| 84 |
-
st.markdown("\n\n")
|
| 85 |
-
bc1, bc2 = st.columns([1, 1])
|
| 86 |
-
with bc1:
|
| 87 |
-
if st.button('Start Over'):
|
| 88 |
-
reset()
|
| 89 |
-
st.rerun()
|
| 90 |
-
with bc2:
|
| 91 |
-
if st.button('Show Logs'):
|
| 92 |
-
show_modal()
|
| 93 |
-
|
| 94 |
-
st.divider()
|
| 95 |
-
st.markdown(
|
| 96 |
-
"## How this works?\n"
|
| 97 |
-
"This app was built with [Vectara](https://vectara.com).\n\n"
|
| 98 |
-
"It demonstrates the use of Agentic RAG functionality with Vectara"
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
if "messages" not in st.session_state.keys():
|
| 102 |
-
reset()
|
| 103 |
|
| 104 |
# Display chat messages
|
| 105 |
for message in st.session_state.messages:
|
|
|
|
| 106 |
with st.chat_message(message["role"], avatar=message["avatar"]):
|
| 107 |
st.write(message["content"])
|
| 108 |
|
| 109 |
-
example_container = st.empty()
|
| 110 |
-
with example_container:
|
| 111 |
-
if show_example_questions():
|
| 112 |
-
example_container.empty()
|
| 113 |
-
st.session_state.first_turn = False
|
| 114 |
-
st.rerun()
|
| 115 |
|
| 116 |
# User-provided prompt
|
| 117 |
if st.session_state.ex_prompt:
|
|
@@ -133,32 +91,16 @@ async def launch_bot():
|
|
| 133 |
with st.chat_message("assistant", avatar='π€'):
|
| 134 |
st.session_state.status = st.status('Processing...', expanded=False)
|
| 135 |
res = st.session_state.agent.chat(st.session_state.prompt)
|
| 136 |
-
res = escape_dollars_outside_latex(res)
|
|
|
|
| 137 |
message = {"role": "assistant", "content": res, "avatar": 'π€'}
|
| 138 |
st.session_state.messages.append(message)
|
| 139 |
st.markdown(res)
|
| 140 |
|
| 141 |
-
send_amplitude_data(
|
| 142 |
-
user_query=st.session_state.messages[-2]["content"],
|
| 143 |
-
bot_response=st.session_state.messages[-1]["content"],
|
| 144 |
-
demo_name=cfg['demo_name']
|
| 145 |
-
)
|
| 146 |
|
| 147 |
st.session_state.ex_prompt = None
|
| 148 |
st.session_state.prompt = None
|
| 149 |
st.session_state.first_turn = False
|
| 150 |
st.rerun()
|
| 151 |
|
| 152 |
-
|
| 153 |
-
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
|
| 154 |
-
if "feedback_key" not in st.session_state:
|
| 155 |
-
st.session_state.feedback_key = 0
|
| 156 |
-
streamlit_feedback(
|
| 157 |
-
feedback_type="thumbs", on_submit=thumbs_feedback, key=str(st.session_state.feedback_key),
|
| 158 |
-
kwargs={"user_query": st.session_state.messages[-2]["content"],
|
| 159 |
-
"bot_response": st.session_state.messages[-1]["content"],
|
| 160 |
-
"demo_name": cfg["demo_name"]}
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
sys.stdout.flush()
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from vectara_agentic.agent import AgentStatusType
|
| 8 |
+
from agent import initialize_agent
|
| 9 |
+
from config import get_agent_config
|
| 10 |
|
| 11 |
initial_prompt = "How can I help you today?"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def format_log_msg(log_msg: str):
|
| 15 |
max_log_msg_size = 500
|
|
|
|
| 57 |
cfg = get_agent_config()
|
| 58 |
st.session_state.cfg = cfg
|
| 59 |
st.session_state.ex_prompt = None
|
|
|
|
|
|
|
| 60 |
reset()
|
| 61 |
|
| 62 |
cfg = st.session_state.cfg
|
| 63 |
+
print(f'Configuration: {cfg}')
|
| 64 |
|
| 65 |
# left side content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Display chat messages
|
| 68 |
for message in st.session_state.messages:
|
| 69 |
+
print(f'Message: {message}')
|
| 70 |
with st.chat_message(message["role"], avatar=message["avatar"]):
|
| 71 |
st.write(message["content"])
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# User-provided prompt
|
| 75 |
if st.session_state.ex_prompt:
|
|
|
|
| 91 |
with st.chat_message("assistant", avatar='π€'):
|
| 92 |
st.session_state.status = st.status('Processing...', expanded=False)
|
| 93 |
res = st.session_state.agent.chat(st.session_state.prompt)
|
| 94 |
+
#res = escape_dollars_outside_latex(res)
|
| 95 |
+
res = str(res)
|
| 96 |
message = {"role": "assistant", "content": res, "avatar": 'π€'}
|
| 97 |
st.session_state.messages.append(message)
|
| 98 |
st.markdown(res)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
st.session_state.ex_prompt = None
|
| 102 |
st.session_state.prompt = None
|
| 103 |
st.session_state.first_turn = False
|
| 104 |
st.rerun()
|
| 105 |
|
| 106 |
+
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|