@@ -60,8 +60,8 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
6060 @param workflow: 工作流管理器
6161 """
6262 response = node_variable .get ("result" )
63- answer = ""
64- reasoning_content = ""
63+ answer_chunks = []
64+ reasoning_content_chunks = []
6565 model_setting = node .context .get (
6666 "model_setting" ,
6767 {"reasoning_content_enable" : False , "reasoning_content_end" : "</think>" , "reasoning_content_start" : "<think>" },
@@ -81,10 +81,10 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
8181 reasoning_content_chunk = chunk .additional_kwargs .get ("reasoning_content" , "" )
8282 else :
8383 reasoning_content_chunk = reasoning_chunk .get ("reasoning_content" )
84- answer += content_chunk
84+ answer_chunks . append ( content_chunk )
8585 if reasoning_content_chunk is None :
8686 reasoning_content_chunk = ""
87- reasoning_content += reasoning_content_chunk
87+ reasoning_content_chunks . append ( reasoning_content_chunk )
8888 yield {
8989 "content" : content_chunk ,
9090 "reasoning_content" : reasoning_content_chunk
@@ -93,14 +93,16 @@ def write_context_stream(node_variable: Dict, workflow_variable: Dict, node: INo
9393 }
9494
9595 reasoning_chunk = reasoning .get_end_reasoning_content ()
96- answer += reasoning_chunk .get ("content" )
96+ answer_chunks . append ( reasoning_chunk .get ("content" ) )
9797 reasoning_content_chunk = ""
9898 if not response_reasoning_content :
9999 reasoning_content_chunk = reasoning_chunk .get ("reasoning_content" )
100100 yield {
101101 "content" : reasoning_chunk .get ("content" ),
102102 "reasoning_content" : reasoning_content_chunk if model_setting .get ("reasoning_content_enable" , False ) else "" ,
103103 }
104+ answer = "" .join (answer_chunks )
105+ reasoning_content = "" .join (reasoning_content_chunks )
104106 _write_context (node_variable , workflow_variable , node , workflow , answer , reasoning_content )
105107
106108
@@ -525,12 +527,20 @@ def _process_videos(self, image, video_model):
525527 if isinstance (image , str ) and image .startswith ("http" ):
526528 videos .append ({"type" : "video_url" , "video_url" : {"url" : image }})
527529 elif image is not None and len (image ) > 0 :
530+ # 先批量获取数据
531+ file_ids = [img ["file_id" ] for img in image if "file_id" in img ]
532+ if file_ids :
533+ files_map = {str (f .id ): f for f in QuerySet (File ).filter (id__in = file_ids )}
534+ else :
535+ files_map = {}
536+ # 再循环从map中取数据进行处理
528537 for img in image :
529538 if "file_id" in img :
530539 file_id = img ["file_id" ]
531- file = QuerySet (File ).filter (id = file_id ).first ()
532- url = video_model .upload_file_and_get_url (file .get_bytes (), file .file_name )
533- videos .append ({"type" : "video_url" , "video_url" : {"url" : url }})
540+ file = files_map .get (str (file_id ))
541+ if file :
542+ url = video_model .upload_file_and_get_url (file .get_bytes (), file .file_name )
543+ videos .append ({"type" : "video_url" , "video_url" : {"url" : url }})
534544 elif "url" in img and img ["url" ].startswith ("http" ):
535545 videos .append ({"type" : "video_url" , "video_url" : {"url" : img ["url" ]}})
536546 return videos
@@ -543,16 +553,24 @@ def _process_images(self, image):
543553 if isinstance (image , str ) and image .startswith ("http" ):
544554 images .append ({"type" : "image_url" , "image_url" : {"url" : image }})
545555 elif image is not None and len (image ) > 0 :
556+ # 先批量获取数据
557+ file_ids = [img ["file_id" ] for img in image if "file_id" in img ]
558+ if file_ids :
559+ files_map = {str (f .id ): f for f in QuerySet (File ).filter (id__in = file_ids )}
560+ else :
561+ files_map = {}
562+ # 再循环从map中取数据进行处理
546563 for img in image :
547564 if "file_id" in img :
548565 file_id = img ["file_id" ]
549- file = QuerySet (File ).filter (id = file_id ).first ()
550- image_bytes = file .get_bytes ()
551- base64_image = base64 .b64encode (image_bytes ).decode ("utf-8" )
552- image_format = what (None , image_bytes )
553- images .append (
554- {"type" : "image_url" , "image_url" : {"url" : f"data:image/{ image_format } ;base64,{ base64_image } " }}
555- )
566+ file = files_map .get (str (file_id ))
567+ if file :
568+ image_bytes = file .get_bytes ()
569+ base64_image = base64 .b64encode (image_bytes ).decode ("utf-8" )
570+ image_format = what (None , image_bytes )
571+ images .append (
572+ {"type" : "image_url" , "image_url" : {"url" : f"data:image/{ image_format } ;base64,{ base64_image } " }}
573+ )
556574 elif "url" in img and img ["url" ].startswith ("http" ):
557575 images .append ({"type" : "image_url" , "image_url" : {"url" : img ["url" ]}})
558576 return images
0 commit comments