", unsafe_allow_html=True)
if genparam.TOKEN_CAPTURE_ENABLED:
chat_number = len(chat_history) // 2
token_calculations = capture_tokens(prompt_data, response, chat_number)
if token_calculations:
st.sidebar.code(token_calculations)
return response
def capture_tokens(prompt_data, response, chat_number):
if not genparam.TOKEN_CAPTURE_ENABLED:
return
watsonx_llm = ModelInference(
api_client=client,
model_id=genparam.SELECTED_MODEL,
verify=genparam.VERIFY
)
input_tokens = watsonx_llm.tokenize(prompt=prompt_data)["result"]["token_count"]
output_tokens = watsonx_llm.tokenize(prompt=response)["result"]["token_count"]
total_tokens = input_tokens + output_tokens
st.session_state.token_capture.append(f"chat {chat_number}: {input_tokens} + {output_tokens} = {total_tokens}")
token_calculations = "\n".join(st.session_state.token_capture)
return token_calculations
def main():
initialize_session_state()
# Apply custom styles
st.markdown(three_column_style, unsafe_allow_html=True)
# Sidebar configuration
st.sidebar.header('The Solutioning Sages')
st.sidebar.write('')
# Display user chat history in sidebar
st.sidebar.subheader("Your Questions")
for i, message in enumerate(st.session_state.chat_history_1):
if message["role"] == "user":
st.sidebar.markdown(f"**Question {i//2 + 1}:** {message['content']}")
st.sidebar.write('')
st.sidebar.write('')
if not check_password():
st.stop()
# Get user input before column creation
user_input = st.chat_input("Ask your question here", key="user_input")
if user_input:
# Create three columns
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("", unsafe_allow_html=True)
st.subheader(f"{genparam.BOT_1_AVATAR} {genparam.BOT_1_NAME}")
st.markdown("
", unsafe_allow_html=True)
# Display only bot responses
for message in st.session_state.chat_history_1:
if message["role"] != "user": # Only show bot messages
with st.chat_message(message["role"], avatar=genparam.BOT_1_AVATAR):
st.markdown(message['content'])
# Add user message to history but don't display
st.session_state.chat_history_1.append({"role": "user", "content": user_input, "avatar": genparam.USER_AVATAR})
milvus_client, emb, vector_index_properties, vector_store_schema = setup_vector_index(
client,
wml_credentials,
get_active_vector_index()
)
system_prompt = genparam.BOT_1_PROMPT
response = fetch_response(
user_input,
milvus_client,
emb,
vector_index_properties,
vector_store_schema,
system_prompt,
st.session_state.chat_history_1
)
st.session_state.chat_history_1.append({"role": genparam.BOT_1_NAME, "content": response, "avatar": genparam.BOT_1_AVATAR})
st.markdown("
", unsafe_allow_html=True)
with col2:
st.markdown("", unsafe_allow_html=True)
st.subheader(f"{genparam.BOT_2_AVATAR} {genparam.BOT_2_NAME}")
st.markdown("
", unsafe_allow_html=True)
# Display only bot responses
for message in st.session_state.chat_history_2:
if message["role"] != "user": # Only show bot messages
with st.chat_message(message["role"], avatar=genparam.BOT_2_AVATAR):
st.markdown(message['content'])
# Add user message to history but don't display
st.session_state.chat_history_2.append({"role": "user", "content": user_input, "avatar": genparam.USER_AVATAR})
milvus_client, emb, vector_index_properties, vector_store_schema = setup_vector_index(
client,
wml_credentials,
get_active_vector_index()
)
system_prompt = genparam.BOT_2_PROMPT
response = fetch_response(
user_input,
milvus_client,
emb,
vector_index_properties,
vector_store_schema,
system_prompt,
st.session_state.chat_history_2
)
st.session_state.chat_history_2.append({"role": genparam.BOT_2_NAME, "content": response, "avatar": genparam.BOT_2_AVATAR})
st.markdown("
", unsafe_allow_html=True)
with col3:
st.markdown("", unsafe_allow_html=True)
st.subheader(f"{genparam.BOT_3_AVATAR} {genparam.BOT_3_NAME}")
st.markdown("
", unsafe_allow_html=True)
# Display only bot responses
for message in st.session_state.chat_history_3:
if message["role"] != "user": # Only show bot messages
with st.chat_message(message["role"], avatar=genparam.BOT_3_AVATAR):
st.markdown(message['content'])
# Add user message to history but don't display
st.session_state.chat_history_3.append({"role": "user", "content": user_input, "avatar": genparam.USER_AVATAR})
milvus_client, emb, vector_index_properties, vector_store_schema = setup_vector_index(
client,
wml_credentials,
st.secrets["vector_index_id_2"]
)
system_prompt = genparam.BOT_3_PROMPT
response = fetch_response(
user_input,
milvus_client,
emb,
vector_index_properties,
vector_store_schema,
system_prompt,
st.session_state.chat_history_3
)
st.session_state.chat_history_3.append({"role": genparam.BOT_3_NAME, "content": response, "avatar": genparam.BOT_3_AVATAR})
st.markdown("
", unsafe_allow_html=True)
# Update sidebar with new question
st.sidebar.markdown("---") # Add divider
st.sidebar.markdown("**Latest Question:**")
st.sidebar.markdown(f"_{user_input}_")
if __name__ == "__main__":
main()