1010
1111
1212def clear_session ():
13- return '' , []
13+ return '' , [], []
1414
1515
1616def modify_system_session (system : str ):
1717 system = system or ''
18- return system , '' , []
18+ return system , '' , [], []
1919
2020
2121def _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 = {'<' : '<' , '>' : '>' , '*' : '*' }
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
8497def 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
0 commit comments