Skip to content

Commit 84a5a6d

Browse files
authored
fix swift app format (#3367)
1 parent bd69e46 commit 84a5a6d

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

swift/llm/app/build_ui.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111

1212
def clear_session():
13-
return '', []
13+
return '', [], []
1414

1515

1616
def modify_system_session(system: str):
1717
system = system or ''
18-
return system, '', []
18+
return system, '', [], []
1919

2020

2121
def _history_to_messages(history: History, system: Optional[str]):
@@ -43,12 +43,19 @@ def _history_to_messages(history: History, system: Optional[str]):
4343
return messages
4444

4545

46-
async def model_chat(history: History, system: Optional[str], *, client, model: str,
46+
def _parse_text(text: str) -> str:
47+
mapping = {'<': '&lt;', '>': '&gt;', '*': '&ast;'}
48+
for k, v in mapping.items():
49+
text = text.replace(k, v)
50+
return text
51+
52+
53+
async def model_chat(history: History, real_history: History, system: Optional[str], *, client, model: str,
4754
request_config: Optional['RequestConfig']):
4855
if history:
4956
from swift.llm import InferRequest
5057

51-
messages = _history_to_messages(history, system)
58+
messages = _history_to_messages(real_history, system)
5259
resp_or_gen = await client.infer_async(
5360
InferRequest(messages=messages), request_config=request_config, model=model)
5461
if request_config and request_config.stream:
@@ -57,28 +64,34 @@ async def model_chat(history: History, system: Optional[str], *, client, model:
5764
if resp is None:
5865
continue
5966
response += resp.choices[0].delta.content
60-
history[-1][1] = response
61-
yield history
67+
history[-1][1] = _parse_text(response)
68+
real_history[-1][-1] = response
69+
yield history, real_history
6270

6371
else:
6472
response = resp_or_gen.choices[0].message.content
65-
history[-1][1] = response
66-
yield history
73+
history[-1][1] = _parse_text(response)
74+
real_history[-1][-1] = response
75+
yield history, real_history
6776

6877
else:
69-
yield []
78+
yield [], []
7079

7180

72-
def add_text(history: History, query: str):
81+
def add_text(history: History, real_history: History, query: str):
7382
history = history or []
74-
history.append([query, None])
75-
return history, ''
83+
real_history = real_history or []
84+
history.append([_parse_text(query), None])
85+
real_history.append([query, None])
86+
return history, real_history, ''
7687

7788

78-
def add_file(history: History, file):
89+
def add_file(history: History, real_history: History, file):
7990
history = history or []
91+
real_history = real_history or []
8092
history.append([(file.name, ), None])
81-
return history
93+
real_history.append([(file.name, ), None])
94+
return history, real_history
8295

8396

8497
def build_ui(base_url: str,
@@ -110,14 +123,17 @@ def build_ui(base_url: str,
110123
clear_history = gr.Button(locale_mapping['clear_history'][lang])
111124

112125
system_state = gr.State(value=default_system)
126+
history_state = gr.State(value=[])
113127
model_chat_ = partial(model_chat, client=client, model=model, request_config=request_config)
114128

115-
upload.upload(add_file, [chatbot, upload], [chatbot])
116-
textbox.submit(add_text, [chatbot, textbox], [chatbot, textbox]).then(model_chat_, [chatbot, system_state],
117-
[chatbot])
118-
submit.click(add_text, [chatbot, textbox], [chatbot, textbox]).then(model_chat_, [chatbot, system_state],
119-
[chatbot])
120-
regenerate.click(model_chat_, [chatbot, system_state], [chatbot])
121-
clear_history.click(clear_session, [], [textbox, chatbot])
122-
modify_system.click(modify_system_session, [system_input], [system_state, textbox, chatbot])
129+
upload.upload(add_file, [chatbot, history_state, upload], [chatbot, history_state])
130+
textbox.submit(add_text, [chatbot, history_state, textbox],
131+
[chatbot, history_state, textbox]).then(model_chat_, [chatbot, history_state, system_state],
132+
[chatbot, history_state])
133+
submit.click(add_text, [chatbot, history_state, textbox],
134+
[chatbot, history_state, textbox]).then(model_chat_, [chatbot, history_state, system_state],
135+
[chatbot, history_state])
136+
regenerate.click(model_chat_, [chatbot, history_state, system_state], [chatbot, history_state])
137+
clear_history.click(clear_session, [], [textbox, chatbot, history_state])
138+
modify_system.click(modify_system_session, [system_input], [system_state, textbox, chatbot, history_state])
123139
return demo

swift/llm/infer/deploy.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,17 @@ def _post_process(self, request_info, response, return_cmpl_response: bool = Fal
121121
is_finished = all(response.choices[i].finish_reason for i in range(len(response.choices)))
122122
if return_cmpl_response:
123123
response = response.to_cmpl_response()
124+
if 'stream' in response.__class__.__name__.lower():
125+
request_info['response'] += response.choices[0].delta.content
126+
else:
127+
request_info['response'] = response.choices[0].message.content
124128
if is_finished:
125129
if args.log_interval > 0:
126130
self.infer_stats.update(response)
127-
data = {'response': asdict(response), **request_info}
128131
if self.jsonl_writer:
129-
self.jsonl_writer.append(data)
132+
self.jsonl_writer.append(request_info)
130133
if self.args.verbose:
131-
logger.info(data)
134+
logger.info(request_info)
132135
return response
133136

134137
def _set_request_config(self, request_config) -> None:
@@ -157,7 +160,7 @@ async def create_chat_completion(self,
157160

158161
infer_request, request_config = request.parse()
159162
self._set_request_config(request_config)
160-
request_info = {'infer_request': infer_request.to_printable()}
163+
request_info = {'response': '', 'infer_request': infer_request.to_printable()}
161164

162165
def pre_infer_hook(kwargs):
163166
request_info['generation_config'] = kwargs['generation_config']

0 commit comments

Comments
 (0)