11import json
22from pathlib import Path
3-
3+ import base64
44from openai import OpenAI
55from app .models .next_console .next_console_model import NextConsoleMessage
66from app .services .next_console .llm import NextConsoleLLMClient , LLMInstance
@@ -43,6 +43,8 @@ def llm_node_execute(params, task_record, global_params):
4343 msg_reasoning_content = ""
4444 msg_token_used = 0
4545 answer_msg = None
46+ task_status = '执行中'
47+ task_trace_log = ''
4648 if workflow_node_llm_params .get ("stream" , False ):
4749 all_message_format = [msg_schema .get ("schema_type" ) for msg_schema in task_record .workflow_node_message_schema ]
4850 output_flag = task_record .workflow_node_enable_message and global_params ["stream" ] and 'messageFlow' in all_message_format
@@ -101,30 +103,33 @@ def llm_node_execute(params, task_record, global_params):
101103 pass
102104 except Exception as e3 :
103105 app .logger .error (f"调用基模型异常:{ str (e3 )} " )
106+ task_status = "异常"
107+ task_trace_log = str (e3 )
104108 msg_content += "\n \n **对不起,模型服务正忙,请稍等片刻后重试,或者可以试试切换其他模型~**"
105- if task_record .workflow_node_enable_message and global_params ["stream" ]:
106- if task_record .workflow_node_message_schema_type == "messageFlow" :
107- except_result = {
108- "id" : "" ,
109- "session_id" : task_record .session_id ,
110- "qa_id" : task_record .qa_id ,
111- "msg_parent_id" : task_record .msg_id ,
112- "created" : 0 ,
113- "model" : '' ,
114- "object" : "chat.completion" ,
115- "choices" : [
116- {
117- "finish_reason" : "error" ,
118- "index" : 0 ,
119- "delta" : {
120- "content" : msg_content ,
121- "role" : "assistant"
122- },
109+ if task_record .workflow_node_enable_message and global_params ["stream" ] and output_flag :
110+ except_result = {
111+ "id" : "" ,
112+ "session_id" : task_record .session_id ,
113+ "qa_id" : task_record .qa_id ,
114+ "msg_parent_id" : task_record .msg_id ,
115+ 'msg_id' : answer_msg .msg_id ,
116+ "created" : 0 ,
117+ "model" : '' ,
118+ "object" : "chat.completion" ,
119+ "choices" : [
120+ {
121+ "finish_reason" : "error" ,
122+ "index" : 0 ,
123+ "delta" : {
124+ "reasoning_content" : "" ,
125+ "content" : msg_content ,
126+ "role" : "assistant"
127+ },
123128
124- }
125- ]
126- }
127- global_params ["message_queue" ].put (except_result )
129+ }
130+ ]
131+ }
132+ global_params ["message_queue" ].put (except_result )
128133 finally :
129134 # 更新llm节点记录
130135 # 如果有RAG引用,则添加到消息中
@@ -155,7 +160,10 @@ def llm_node_execute(params, task_record, global_params):
155160 "reasoning_content" : msg_reasoning_content ,
156161 })
157162 task_record .end_time = datetime .now ()
158- task_record .task_status = "已完成"
163+ if task_status == '执行中' :
164+ task_status = "已完成"
165+ task_record .task_status = task_status
166+ task_record .task_trace_log = task_trace_log
159167 task_record .task_token_used = msg_token_used
160168 db .session .add (task_record )
161169 db .session .commit ()
@@ -178,24 +186,24 @@ def llm_node_execute(params, task_record, global_params):
178186 task_record .task_token_used = msg_token_used
179187 except Exception as e :
180188 task_record .task_status = "异常"
189+ task_record .task_trace_log = str (e )
181190 app .logger .error (f"workflow_chat error: { e } " )
182191 msg_content = '对不起,模型服务正忙,请稍等片刻后重试,或者可以试试切换其他模型~'
183- task_record . task_trace_log = str ( e )
192+
184193 if task_record .workflow_node_rag_ref_show :
185194 reference_md = add_reference_md (task_record , global_params )
186195 msg_content += reference_md
187196 task_record .task_result = json .dumps ({
188197 "content" : msg_content ,
189198 "reasoning_content" : msg_reasoning_content ,
190199 })
191- task_record .task_status = "已完成"
192200 task_record .end_time = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )
193201 db .session .add (task_record )
194202 db .session .commit ()
195203 return True
196204
197205
198- def load_llm_prams (task_params , task_record , global_params ):
206+ def load_llm_prams (task_params , task_record , global_params , imgUrl = 'url' ):
199207 """
200208 载入大模型的所有参数
201209 """
@@ -244,10 +252,20 @@ def load_llm_prams(task_params, task_record, global_params):
244252 ).all ()
245253 for resource in target_attachments :
246254 if resource .resource_type in ("image" , "media" ) and resource .resource_download_url :
255+ url = app .config .get ("domain" ) + "/next_console/knowledge_center/resource_images?resource_id={}" .format (resource .id )
256+ if imgUrl == 'base64' :
257+ # 如果是base64编码的图片
258+ try :
259+ with open (resource .resource_path , "rb" ) as image_file :
260+ encoded_string = base64 .b64encode (image_file .read ()).decode ('utf-8' )
261+ url = "data:image/png;base64,{}" .format (encoded_string )
262+ except Exception as e :
263+ app .logger .error (f"读取图片文件失败:{ e } " )
264+ continue
247265 image_list .append ({
248266 "type" : "image_url" ,
249267 "image_url" : {
250- "url" : app . config . get ( "domain" ) + "/next_console/knowledge_center/resource_images?resource_id={}" . format ( resource . id ) ,
268+ "url" : url ,
251269 "detail" : "auto"
252270 }
253271 })
0 commit comments