MCPcopy
hub / github.com/lm-sys/FastChat / add_text

Function add_text

fastchat/serve/gradio_block_arena_named.py:146–208  ·  view source on GitHub ↗
(
    state0, state1, model_selector0, model_selector1, text, request: gr.Request
)

Source from the content-addressed store, hash-verified

144
145
146def add_text(
147 state0, state1, model_selector0, model_selector1, text, request: gr.Request
148):
149 ip = get_ip(request)
150 logger.info(f"add_text (named). ip: {ip}. len: {len(text)}")
151 states = [state0, state1]
152 model_selectors = [model_selector0, model_selector1]
153
154 # Init states if necessary
155 for i in range(num_sides):
156 if states[i] is None:
157 states[i] = State(model_selectors[i])
158
159 if len(text) <= 0:
160 for i in range(num_sides):
161 states[i].skip_next = True
162 return (
163 states
164 + [x.to_gradio_chatbot() for x in states]
165 + [""]
166 + [
167 no_change_btn,
168 ]
169 * 6
170 )
171
172 model_list = [states[i].model_name for i in range(num_sides)]
173 flagged = moderation_filter(text, model_list)
174 if flagged:
175 logger.info(f"violate moderation (named). ip: {ip}. text: {text}")
176 # overwrite the original text
177 text = MODERATION_MSG
178
179 conv = states[0].conv
180 if (len(conv.messages) - conv.offset) // 2 >= CONVERSATION_TURN_LIMIT:
181 logger.info(f"conversation turn limit. ip: {ip}. text: {text}")
182 for i in range(num_sides):
183 states[i].skip_next = True
184 return (
185 states
186 + [x.to_gradio_chatbot() for x in states]
187 + [CONVERSATION_LIMIT_MSG]
188 + [
189 no_change_btn,
190 ]
191 * 6
192 )
193
194 text = text[:INPUT_CHAR_LEN_LIMIT] # Hard cut-off
195 for i in range(num_sides):
196 states[i].conv.append_message(states[i].conv.roles[0], text)
197 states[i].conv.append_message(states[i].conv.roles[1], None)
198 states[i].skip_next = False
199
200 return (
201 states
202 + [x.to_gradio_chatbot() for x in states]
203 + [""]

Callers

nothing calls this directly

Calls 5

get_ipFunction · 0.90
StateClass · 0.90
moderation_filterFunction · 0.90
append_messageMethod · 0.80
to_gradio_chatbotMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…