24
24
from application .chat_pipeline .I_base_chat_pipeline import ParagraphPipelineModel
25
25
from application .chat_pipeline .pipeline_manage import PipelineManage
26
26
from application .chat_pipeline .step .chat_step .i_chat_step import IChatStep , PostResponseHandler
27
+ from application .flow .tools import Reasoning
27
28
from application .models .api_key_model import ApplicationPublicAccessClient
28
29
from common .constants .authentication_type import AuthenticationType
29
30
from setting .models_provider .tools import get_model_instance_by_model_user_id
@@ -63,17 +64,37 @@ def event_content(response,
63
64
problem_text : str ,
64
65
padding_problem_text : str = None ,
65
66
client_id = None , client_type = None ,
66
- is_ai_chat : bool = None ):
67
+ is_ai_chat : bool = None ,
68
+ model_setting = None ):
69
+ if model_setting is None :
70
+ model_setting = {}
71
+ reasoning_content_enable = model_setting .get ('reasoning_content_enable' , False )
72
+ reasoning_content_start = model_setting .get ('reasoning_content_start' , '<think>' )
73
+ reasoning_content_end = model_setting .get ('reasoning_content_end' , '</think>' )
74
+ reasoning = Reasoning (reasoning_content_start ,
75
+ reasoning_content_end )
67
76
all_text = ''
77
+ reasoning_content = ''
68
78
try :
69
79
for chunk in response :
70
- all_text += chunk .content
80
+ reasoning_chunk = reasoning .get_reasoning_content (chunk )
81
+ content_chunk = reasoning_chunk .get ('content' )
82
+ if 'reasoning_content' in chunk .additional_kwargs :
83
+ reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
84
+ else :
85
+ reasoning_content_chunk = reasoning_chunk .get ('reasoning_content' )
86
+ all_text += content_chunk
87
+ if reasoning_content_chunk is None :
88
+ reasoning_content_chunk = ''
89
+ reasoning_content += reasoning_content_chunk
71
90
yield manage .get_base_to_response ().to_stream_chunk_response (chat_id , str (chat_record_id ), 'ai-chat-node' ,
72
- [], chunk . content ,
91
+ [], content_chunk ,
73
92
False ,
74
93
0 , 0 , {'node_is_end' : False ,
75
94
'view_type' : 'many_view' ,
76
- 'node_type' : 'ai-chat-node' })
95
+ 'node_type' : 'ai-chat-node' ,
96
+ 'real_node_id' : 'ai-chat-node' ,
97
+ 'reasoning_content' : reasoning_content_chunk if reasoning_content_enable else '' })
77
98
# 获取token
78
99
if is_ai_chat :
79
100
try :
@@ -87,7 +108,8 @@ def event_content(response,
87
108
response_token = 0
88
109
write_context (step , manage , request_token , response_token , all_text )
89
110
post_response_handler .handler (chat_id , chat_record_id , paragraph_list , problem_text ,
90
- all_text , manage , step , padding_problem_text , client_id )
111
+ all_text , manage , step , padding_problem_text , client_id ,
112
+ reasoning_content = reasoning_content if reasoning_content_enable else '' )
91
113
yield manage .get_base_to_response ().to_stream_chunk_response (chat_id , str (chat_record_id ), 'ai-chat-node' ,
92
114
[], '' , True ,
93
115
request_token , response_token ,
@@ -122,17 +144,20 @@ def execute(self, message_list: List[BaseMessage],
122
144
client_id = None , client_type = None ,
123
145
no_references_setting = None ,
124
146
model_params_setting = None ,
147
+ model_setting = None ,
125
148
** kwargs ):
126
149
chat_model = get_model_instance_by_model_user_id (model_id , user_id ,
127
150
** model_params_setting ) if model_id is not None else None
128
151
if stream :
129
152
return self .execute_stream (message_list , chat_id , problem_text , post_response_handler , chat_model ,
130
153
paragraph_list ,
131
- manage , padding_problem_text , client_id , client_type , no_references_setting )
154
+ manage , padding_problem_text , client_id , client_type , no_references_setting ,
155
+ model_setting )
132
156
else :
133
157
return self .execute_block (message_list , chat_id , problem_text , post_response_handler , chat_model ,
134
158
paragraph_list ,
135
- manage , padding_problem_text , client_id , client_type , no_references_setting )
159
+ manage , padding_problem_text , client_id , client_type , no_references_setting ,
160
+ model_setting )
136
161
137
162
def get_details (self , manage , ** kwargs ):
138
163
return {
@@ -187,14 +212,15 @@ def execute_stream(self, message_list: List[BaseMessage],
187
212
manage : PipelineManage = None ,
188
213
padding_problem_text : str = None ,
189
214
client_id = None , client_type = None ,
190
- no_references_setting = None ):
215
+ no_references_setting = None ,
216
+ model_setting = None ):
191
217
chat_result , is_ai_chat = self .get_stream_result (message_list , chat_model , paragraph_list ,
192
218
no_references_setting , problem_text )
193
219
chat_record_id = uuid .uuid1 ()
194
220
r = StreamingHttpResponse (
195
221
streaming_content = event_content (chat_result , chat_id , chat_record_id , paragraph_list ,
196
222
post_response_handler , manage , self , chat_model , message_list , problem_text ,
197
- padding_problem_text , client_id , client_type , is_ai_chat ),
223
+ padding_problem_text , client_id , client_type , is_ai_chat , model_setting ),
198
224
content_type = 'text/event-stream;charset=utf-8' )
199
225
200
226
r ['Cache-Control' ] = 'no-cache'
@@ -230,7 +256,13 @@ def execute_block(self, message_list: List[BaseMessage],
230
256
paragraph_list = None ,
231
257
manage : PipelineManage = None ,
232
258
padding_problem_text : str = None ,
233
- client_id = None , client_type = None , no_references_setting = None ):
259
+ client_id = None , client_type = None , no_references_setting = None ,
260
+ model_setting = None ):
261
+ reasoning_content_enable = model_setting .get ('reasoning_content_enable' , False )
262
+ reasoning_content_start = model_setting .get ('reasoning_content_start' , '<think>' )
263
+ reasoning_content_end = model_setting .get ('reasoning_content_end' , '</think>' )
264
+ reasoning = Reasoning (reasoning_content_start ,
265
+ reasoning_content_end )
234
266
chat_record_id = uuid .uuid1 ()
235
267
# 调用模型
236
268
try :
@@ -243,14 +275,23 @@ def execute_block(self, message_list: List[BaseMessage],
243
275
request_token = 0
244
276
response_token = 0
245
277
write_context (self , manage , request_token , response_token , chat_result .content )
278
+ reasoning .get_reasoning_content (chat_result )
279
+ reasoning_result = reasoning .get_reasoning_content (chat_result )
280
+ content = reasoning_result .get ('content' )
281
+ if 'reasoning_content' in chat_result .response_metadata :
282
+ reasoning_content = chat_result .response_metadata .get ('reasoning_content' , '' )
283
+ else :
284
+ reasoning_content = reasoning_result .get ('reasoning_content' )
246
285
post_response_handler .handler (chat_id , chat_record_id , paragraph_list , problem_text ,
247
- chat_result .content , manage , self , padding_problem_text , client_id )
286
+ chat_result .content , manage , self , padding_problem_text , client_id ,
287
+ reasoning_content = reasoning_content if reasoning_content_enable else '' )
248
288
add_access_num (client_id , client_type , manage .context .get ('application_id' ))
249
289
return manage .get_base_to_response ().to_block_response (str (chat_id ), str (chat_record_id ),
250
- chat_result .content , True ,
251
- request_token , response_token )
290
+ content , True ,
291
+ request_token , response_token ,
292
+ {'reasoning_content' : reasoning_content })
252
293
except Exception as e :
253
- all_text = '异常 ' + str (e )
294
+ all_text = 'Exception: ' + str (e )
254
295
write_context (self , manage , 0 , 0 , all_text )
255
296
post_response_handler .handler (chat_id , chat_record_id , paragraph_list , problem_text ,
256
297
all_text , manage , self , padding_problem_text , client_id )
0 commit comments