Update neo_sages.py
Browse files- neo_sages.py +61 -24
neo_sages.py
CHANGED
@@ -64,8 +64,8 @@ def initialize_session_state():
|
|
64 |
st.session_state.first_question = False
|
65 |
if "counter" not in st.session_state:
|
66 |
st.session_state["counter"] = 0
|
67 |
-
if '
|
68 |
-
st.session_state.
|
69 |
|
70 |
# three_column_style = """
|
71 |
# <style>
|
@@ -300,20 +300,18 @@ def fetch_response(user_input, milvus_client, emb, vector_index_properties, vect
|
|
300 |
bot_avatar = genparam.BOT_3_AVATAR
|
301 |
|
302 |
with st.chat_message(bot_name, avatar=bot_avatar):
|
303 |
-
if genparam.TOKEN_CAPTURE_ENABLED:
|
304 |
-
st.code(prompt_data, line_numbers=True, wrap_lines=True)
|
305 |
if chat_history != st.session_state.chat_history_1: # Only generate responses for columns 2 and 3
|
306 |
stream = generate_response(watsonx_llm, prompt_data, params)
|
307 |
response = st.write_stream(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
else:
|
309 |
response = grounding # For column 1, we already displayed the content
|
310 |
|
311 |
-
if genparam.TOKEN_CAPTURE_ENABLED:
|
312 |
-
chat_number = len(chat_history) // 2
|
313 |
-
token_calculations = capture_tokens(prompt_data, response, chat_number)
|
314 |
-
if token_calculations:
|
315 |
-
st.sidebar.code(token_calculations)
|
316 |
-
|
317 |
return response
|
318 |
|
319 |
def capture_tokens(prompt_data, response, chat_number):
|
@@ -330,10 +328,13 @@ def capture_tokens(prompt_data, response, chat_number):
|
|
330 |
output_tokens = watsonx_llm.tokenize(prompt=response)["result"]["token_count"]
|
331 |
total_tokens = input_tokens + output_tokens
|
332 |
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
|
|
|
|
|
|
337 |
|
338 |
def main():
|
339 |
initialize_session_state()
|
@@ -343,16 +344,53 @@ def main():
|
|
343 |
|
344 |
# Sidebar configuration
|
345 |
st.sidebar.header('The Solutioning Sages')
|
346 |
-
st.sidebar.
|
347 |
|
348 |
-
# Display
|
349 |
-
st.sidebar.subheader("
|
350 |
-
for i, message in enumerate(st.session_state.chat_history_1):
|
351 |
-
if message["role"] == "user":
|
352 |
-
st.sidebar.markdown(f"**Question {i//2 + 1}:** {message['content']}")
|
353 |
|
354 |
-
|
355 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
if not check_password():
|
358 |
st.stop()
|
@@ -364,7 +402,6 @@ def main():
|
|
364 |
# Create three columns
|
365 |
col1, col2, col3 = st.columns(3)
|
366 |
|
367 |
-
###-------------START
|
368 |
# First column - PATH-er B. (Document Display)
|
369 |
with col1:
|
370 |
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
@@ -469,7 +506,7 @@ def main():
|
|
469 |
)
|
470 |
st.session_state.chat_history_3.append({"role": genparam.BOT_3_NAME, "content": response, "avatar": genparam.BOT_3_AVATAR})
|
471 |
st.markdown("</div></div>", unsafe_allow_html=True)
|
472 |
-
|
473 |
|
474 |
# Update sidebar with new question
|
475 |
st.sidebar.markdown("---")
|
|
|
64 |
st.session_state.first_question = False
|
65 |
if "counter" not in st.session_state:
|
66 |
st.session_state["counter"] = 0
|
67 |
+
if 'token_statistics' not in st.session_state:
|
68 |
+
st.session_state.token_statistics = []
|
69 |
|
70 |
# three_column_style = """
|
71 |
# <style>
|
|
|
300 |
bot_avatar = genparam.BOT_3_AVATAR
|
301 |
|
302 |
with st.chat_message(bot_name, avatar=bot_avatar):
|
|
|
|
|
303 |
if chat_history != st.session_state.chat_history_1: # Only generate responses for columns 2 and 3
|
304 |
stream = generate_response(watsonx_llm, prompt_data, params)
|
305 |
response = st.write_stream(stream)
|
306 |
+
|
307 |
+
# Only capture tokens for MOD-ther S. and SYS-ter V.
|
308 |
+
if genparam.TOKEN_CAPTURE_ENABLED and chat_history != st.session_state.chat_history_1:
|
309 |
+
token_stats = capture_tokens(prompt_data, response, bot_name)
|
310 |
+
if token_stats:
|
311 |
+
st.session_state.token_statistics.append(token_stats)
|
312 |
else:
|
313 |
response = grounding # For column 1, we already displayed the content
|
314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
return response
|
316 |
|
317 |
def capture_tokens(prompt_data, response, chat_number):
|
|
|
328 |
output_tokens = watsonx_llm.tokenize(prompt=response)["result"]["token_count"]
|
329 |
total_tokens = input_tokens + output_tokens
|
330 |
|
331 |
+
return {
|
332 |
+
"bot_name": bot_name,
|
333 |
+
"input_tokens": input_tokens,
|
334 |
+
"output_tokens": output_tokens,
|
335 |
+
"total_tokens": total_tokens,
|
336 |
+
"timestamp": time.strftime("%H:%M:%S")
|
337 |
+
}
|
338 |
|
339 |
def main():
|
340 |
initialize_session_state()
|
|
|
344 |
|
345 |
# Sidebar configuration
|
346 |
st.sidebar.header('The Solutioning Sages')
|
347 |
+
st.sidebar.divider()
|
348 |
|
349 |
+
# Display token statistics in sidebar
|
350 |
+
st.sidebar.subheader("Token Usage Statistics")
|
|
|
|
|
|
|
351 |
|
352 |
+
# Group token statistics by interaction (for MOD-ther S. and SYS-ter V. only)
|
353 |
+
if st.session_state.token_statistics:
|
354 |
+
current_timestamp = None
|
355 |
+
interaction_count = 0
|
356 |
+
stats_by_time = {}
|
357 |
+
|
358 |
+
# Group stats by timestamp
|
359 |
+
for stat in st.session_state.token_statistics:
|
360 |
+
if stat["timestamp"] not in stats_by_time:
|
361 |
+
stats_by_time[stat["timestamp"]] = []
|
362 |
+
stats_by_time[stat["timestamp"]].append(stat)
|
363 |
+
|
364 |
+
# Display grouped stats
|
365 |
+
for timestamp, stats in stats_by_time.items():
|
366 |
+
interaction_count += 1
|
367 |
+
st.sidebar.markdown(f"**Interaction {interaction_count}** ({timestamp})")
|
368 |
+
|
369 |
+
# Calculate total tokens for this interaction
|
370 |
+
total_input = sum(stat['input_tokens'] for stat in stats)
|
371 |
+
total_output = sum(stat['output_tokens'] for stat in stats)
|
372 |
+
total = total_input + total_output
|
373 |
+
|
374 |
+
# Display individual bot statistics
|
375 |
+
for stat in stats:
|
376 |
+
st.sidebar.markdown(
|
377 |
+
f"_{stat['bot_name']}_ \n"
|
378 |
+
f"Input: {stat['input_tokens']} tokens \n"
|
379 |
+
f"Output: {stat['output_tokens']} tokens \n"
|
380 |
+
f"Total: {stat['total_tokens']} tokens"
|
381 |
+
)
|
382 |
+
|
383 |
+
# Display interaction totals
|
384 |
+
st.sidebar.markdown("**Interaction Totals:**")
|
385 |
+
st.sidebar.markdown(
|
386 |
+
f"Total Input: {total_input} tokens \n"
|
387 |
+
f"Total Output: {total_output} tokens \n"
|
388 |
+
f"Total Usage: {total} tokens"
|
389 |
+
)
|
390 |
+
st.sidebar.markdown("---")
|
391 |
+
|
392 |
+
st.sidebar.markdown("")
|
393 |
+
|
394 |
|
395 |
if not check_password():
|
396 |
st.stop()
|
|
|
402 |
# Create three columns
|
403 |
col1, col2, col3 = st.columns(3)
|
404 |
|
|
|
405 |
# First column - PATH-er B. (Document Display)
|
406 |
with col1:
|
407 |
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
|
|
506 |
)
|
507 |
st.session_state.chat_history_3.append({"role": genparam.BOT_3_NAME, "content": response, "avatar": genparam.BOT_3_AVATAR})
|
508 |
st.markdown("</div></div>", unsafe_allow_html=True)
|
509 |
+
|
510 |
|
511 |
# Update sidebar with new question
|
512 |
st.sidebar.markdown("---")
|