@@ -84,18 +84,38 @@ def message_func(text, is_user=False, is_df=False):
84
84
85
85
class StreamlitUICallbackHandler (BaseCallbackHandler ):
86
86
def __init__ (self ):
87
- # Buffer to accumulate tokens
88
87
self .token_buffer = []
89
88
self .placeholder = st .empty ()
90
89
self .has_streaming_ended = False
90
+ self .has_streaming_started = False
91
+
92
+ def start_loading_message (self ):
93
+ loading_message_content = self ._get_bot_message_container ("Thinking..." )
94
+ self .placeholder .markdown (loading_message_content , unsafe_allow_html = True )
95
+
96
+ def on_llm_new_token (self , token , run_id , parent_run_id = None , ** kwargs ):
97
+ if not self .has_streaming_started :
98
+ self .has_streaming_started = True
99
+
100
+ self .token_buffer .append (token )
101
+ complete_message = "" .join (self .token_buffer )
102
+ container_content = self ._get_bot_message_container (complete_message )
103
+ self .placeholder .markdown (container_content , unsafe_allow_html = True )
104
+
105
+ def on_llm_end (self , response , run_id , parent_run_id = None , ** kwargs ):
106
+ self .token_buffer = []
107
+ self .has_streaming_ended = True
108
+ self .has_streaming_started = False
91
109
92
110
def _get_bot_message_container (self , text ):
93
111
"""Generate the bot's message container style for the given text."""
94
112
avatar_url = "https://avataaars.io/?avatarStyle=Transparent&topType=WinterHat2&accessoriesType=Kurt&hatColor=Blue01&facialHairType=MoustacheMagnum&facialHairColor=Blonde&clotheType=Overall&clotheColor=Gray01&eyeType=WinkWacky&eyebrowType=SadConcernedNatural&mouthType=Sad&skinColor=Light"
95
113
message_alignment = "flex-start"
96
114
message_bg_color = "#71797E"
97
115
avatar_class = "bot-avatar"
98
- formatted_text = format_message (text )
116
+ formatted_text = format_message (
117
+ text
118
+ ) # Ensure this handles "Thinking..." appropriately.
99
119
container_content = f"""
100
120
<div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: { message_alignment } ;">
101
121
<img src="{ avatar_url } " class="{ avatar_class } " alt="avatar" style="width: 50px; height: 50px;" />
@@ -105,17 +125,6 @@ def _get_bot_message_container(self, text):
105
125
"""
106
126
return container_content
107
127
108
- def on_llm_new_token (self , token , run_id , parent_run_id = None , ** kwargs ):
109
- """
110
- Handle the new token from the model. Accumulate tokens in a buffer and update the Streamlit UI.
111
- """
112
- self .token_buffer .append (token )
113
- complete_message = "" .join (self .token_buffer )
114
-
115
- # Update the placeholder content with the complete message
116
- container_content = self ._get_bot_message_container (complete_message )
117
- self .placeholder .markdown (container_content , unsafe_allow_html = True )
118
-
119
128
def display_dataframe (self , df ):
120
129
"""
121
130
Display the dataframe in Streamlit UI within the chat container.
@@ -134,12 +143,5 @@ def display_dataframe(self, df):
134
143
)
135
144
st .write (df )
136
145
137
- def on_llm_end (self , response , run_id , parent_run_id = None , ** kwargs ):
138
- """
139
- Reset the buffer when the LLM finishes running.
140
- """
141
- self .token_buffer = [] # Reset the buffer
142
- self .has_streaming_ended = True
143
-
144
146
def __call__ (self , * args , ** kwargs ):
145
147
pass
0 commit comments