How to detect prompt (Placeholder) in Real Time/Auto-Refresh of the chat_input?

Hi, I have a question about the chat_input. I want to make an approximate cost of the prompt in Real Time when the user is typing or when it’s not hovering on the input, make an action so, how can I do it with chat_input? Thx

Any solution??

I don’t think this is possible, unfortunately, because the chat_input doesn’t send any information back to Streamlit until you hit enter or press send. The best thing I can think of is that when they type in a prompt, and then hit enter/send, you can first show them a cost estimate, and ask if they want to go ahead.

Something close to this is GitHub - blackary/streamlit-keyup: Streamlit text input that returns value on keyup, but unfortunately it doesn’t automatically hover on the bottom of the page like st.chat_input does.

Just for fun, I used some extra components to build hacky version of this – I wouldn’t necessarily recommend this, but if you really need something like this, you could do something with st_keyup and some css.

import streamlit as st
from st_keyup import st_keyup
from streamlit_extras.stylable_container import stylable_container

if "value" not in st.session_state:
    st.session_state["value"] = ""

def send_data():
    st.session_state["value"] = st.session_state["value_dynamic"]

def calculate_cost(input: str) -> float:
    return len(input) * 0.01

with stylable_container(
        position: fixed;
        bottom: 50px;
    value = st_keyup("Enter a value", key="value_dynamic")

    col1, col2 = st.columns(2)
    if value:
        cost = calculate_cost(value)
        col1.write(f"Cost: ${cost:.2f}")
        col2.button("Submit", on_click=send_data)


Oh, thanks! That was definitely that I was looking for. :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.